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.

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

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



21
22
23
24
25
26
27
28
# File 'lib/devise/jwt/test_helpers.rb', line 21

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
  )
  Warden::JWTAuth::HeaderParser.to_headers(headers, token)
end