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 whitelist 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`.

:reek:LongParameterList :reek:ManualDispatch

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



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/devise/jwt/test_helpers.rb', line 26

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
  )
  if user.respond_to?(:on_jwt_dispatch)
    user.on_jwt_dispatch(token, payload)
  end
  Warden::JWTAuth::HeaderParser.to_headers(headers, token)
end