Sha256: a0f2f99fbf1bed3fcbe22bfb4896ef6fc69fbfeaf81e64ca40af7116bb7a2488

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module AuthRails
  module Strategies
    class AllowedTokenStrategy < BaseStrategy
      class << self
        def retrieve_resource(payload:)
          symbolized_payload = payload.symbolize_keys

          AuthRails.resource_class
                   .joins(:allowed_tokens)
                   .where(allowed_tokens: symbolized_payload.slice(:jti, :aud))
                   .where('allowed_tokens.exp > ?', Time.current)
                   .find_by(AuthRails.identifier_name => symbolized_payload[:sub])
        end

        def gen_token(resource:, payload:, exp: nil, secret_key: nil, algorithm: nil)
          jti = SecureRandom.hex(20)

          resource.allowed_tokens
                  .create!(
                    jti: jti,
                    aud: payload[:aud],
                    exp: Time.zone.at(exp)
                  )

          super(
            jti: jti,
            exp: exp,
            payload: payload,
            algorithm: algorithm,
            secret_key: secret_key
          )
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
auth_rails-1.1.2 lib/auth_rails/strategies/allowed_token_strategy.rb
auth_rails-1.1.1 lib/auth_rails/strategies/allowed_token_strategy.rb
auth_rails-1.1.0 lib/auth_rails/strategies/allowed_token_strategy.rb