Module: Devise::JWT::TestHelpers

Defined in:
lib/devise/jwt/test_helpers.rb

Overview

Helpers to make testing authorization through JWT easier

Class Method Summary collapse

Class Method Details

.auth_headers(headers, user, scope: nil, aud: nil) ⇒ Object

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.

autodetected. the header name configured in ‘Devise::JWT.config.aud_header`.

Parameters:

  • headers (Hash)

    Headers to which add the ‘Authorization` item.

  • user (ActiveRecord::Base)

    The user to authenticate.

  • scope (Symbol) (defaults to: nil)

    The warden scope. If ‘nil` it will be

  • aud (String) (defaults to: nil)

    The aud claim. If ‘nil` it will be autodetected from



23
24
25
26
27
28
29
30
31
# File 'lib/devise/jwt/test_helpers.rb', line 23

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