Sha256: 6b66e7afca82fce57def60f01a657b6c88d2ebc04235ce80d5b5663782de2a2a

Contents?: true

Size: 1.37 KB

Versions: 7

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module Devise
  module JWT
    # Helpers to make testing authorization through JWT easier
    module TestHelpers
      # Returns headers with a valid token in the `Authorization` header
      # added.
      #
      # Side effects could happen if you have implemented
      # `on_jwt_dispatch` method on the user model (as it happens in
      # the allowlist revocation strategy).
      #
      # Be aware that a fresh copy of `headers` is returned with the new
      # key/value pair added, instead of modifying given argument.
      #
      # @param headers [Hash] Headers to which add the `Authorization` item.
      # @param user [ActiveRecord::Base] The user to authenticate.
      # @param scope [Symbol] The warden scope. If `nil` it will be
      # autodetected.
      # @param aud [String] The aud claim. If `nil` it will be autodetected from
      # the header name configured in `Devise::JWT.config.aud_header`.
      def self.auth_headers(headers, user, scope: nil, aud: nil)
        scope ||= Devise::Mapping.find_scope!(user)
        aud ||= headers[Warden::JWTAuth.config.aud_header]
        token, payload = Warden::JWTAuth::UserEncoder.new.call(
          user, scope, aud
        )
        user.on_jwt_dispatch(token, payload) if user.respond_to?(:on_jwt_dispatch)
        Warden::JWTAuth::HeaderParser.to_headers(headers, token)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
devise-jwt-0.12.1 lib/devise/jwt/test_helpers.rb
devise-jwt-0.12.0 lib/devise/jwt/test_helpers.rb
devise-jwt-0.11.0 lib/devise/jwt/test_helpers.rb
devise-jwt-0.10.0 lib/devise/jwt/test_helpers.rb
devise-jwt-0.9.0 lib/devise/jwt/test_helpers.rb
devise-jwt-0.8.1 lib/devise/jwt/test_helpers.rb
devise-jwt-0.8.0 lib/devise/jwt/test_helpers.rb