Sha256: 1ce7ca4ad55e15f114a441a0f9282e1a886c904aa84508d382c171c70ce67b6a

Contents?: true

Size: 1.64 KB

Versions: 8

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

require 'active_support/concern'

module Devise
  module JWT
    module RevocationStrategies
      # This strategy must be included in the user model.
      #
      # The JwtWhitelist table must include `jti`, `aud`, `exp` and `user_id`
      # columns
      #
      # In order to tell whether a token is revoked, it just tries to find the
      # `jti` and `aud` values from the token on the `whitelisted_jwts`
      # table for the respective user.
      #
      # If the values don't exist means the token was revoked.
      # On revocation, it deletes the matching record from the
      # `whitelisted_jwts` table.
      #
      # On sign in, it creates a new record with the `jti` and `aud` values.
      module Whitelist
        extend ActiveSupport::Concern

        included do
          has_many :whitelisted_jwts, dependent: :destroy

          # @see Warden::JWTAuth::Interfaces::RevocationStrategy#jwt_revoked?
          def self.jwt_revoked?(payload, user)
            !user.whitelisted_jwts.exists?(payload.slice('jti', 'aud'))
          end

          # @see Warden::JWTAuth::Interfaces::RevocationStrategy#revoke_jwt
          def self.revoke_jwt(payload, user)
            jwt = user.whitelisted_jwts.find_by(payload.slice('jti', 'aud'))
            jwt.destroy! if jwt
          end
        end

        # Warden::JWTAuth::Interfaces::User#on_jwt_dispatch
        # :reek:FeatureEnvy
        def on_jwt_dispatch(_token, payload)
          whitelisted_jwts.create!(
            jti: payload['jti'],
            aud: payload['aud'],
            exp: Time.at(payload['exp'].to_i)
          )
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
devise-jwt-0.6.0 lib/devise/jwt/revocation_strategies/whitelist.rb
devise-jwt-0.5.9 lib/devise/jwt/revocation_strategies/whitelist.rb
devise-jwt-0.5.8 lib/devise/jwt/revocation_strategies/whitelist.rb
devise-jwt-0.5.7 lib/devise/jwt/revocation_strategies/whitelist.rb
devise-jwt-0.5.6 lib/devise/jwt/revocation_strategies/whitelist.rb
devise-jwt-0.5.5 lib/devise/jwt/revocation_strategies/whitelist.rb
devise-jwt-0.5.4 lib/devise/jwt/revocation_strategies/whitelist.rb
devise-jwt-0.5.3 lib/devise/jwt/revocation_strategies/whitelist.rb