Module: Warden::JWTAuth::PayloadUserHelper

Defined in:
lib/warden/jwt_auth/payload_user_helper.rb

Overview

Helper functions to deal with user info present in a decode payload

Class Method Summary collapse

Class Method Details

.aud_matches?(payload, aud) ⇒ Boolean

Returns whether given aud matches with the one encoded in the payload

Parameters:

  • payload (Hash)

    JWT payload

Returns:

  • (Boolean)


28
29
30
# File 'lib/warden/jwt_auth/payload_user_helper.rb', line 28

def self.aud_matches?(payload, aud)
  payload['aud'] == aud
end

.find_user(payload) ⇒ Interfaces::User

Returns user encoded in given payload

Parameters:

  • payload (Hash)

    JWT payload

Returns:



11
12
13
14
15
16
# File 'lib/warden/jwt_auth/payload_user_helper.rb', line 11

def self.find_user(payload)
  config = JWTAuth.config
  scope = payload['scp'].to_sym
  user_repo = config.mappings[scope]
  user_repo.find_for_jwt_authentication(payload['sub'])
end

.payload_for_user(user, scope) ⇒ Hash

Returns the payload to encode for a given user in a scope

:reek:ManualDispatch

Parameters:

  • user (Interfaces::User)

    an user, whatever it is

  • scope (Symbol)

    A Warden scope

Returns:

  • (Hash)

    payload to encode



38
39
40
41
42
43
44
# File 'lib/warden/jwt_auth/payload_user_helper.rb', line 38

def self.payload_for_user(user, scope)
  sub = user.jwt_subject
  payload = { 'sub' => String(sub), 'scp' => scope.to_s }
  return payload unless user.respond_to?(:jwt_payload)

  user.jwt_payload.merge(payload)
end

.scope_matches?(payload, scope) ⇒ Boolean

Returns whether given scope matches with the one encoded in the payload

Parameters:

  • payload (Hash)

    JWT payload

Returns:

  • (Boolean)


21
22
23
# File 'lib/warden/jwt_auth/payload_user_helper.rb', line 21

def self.scope_matches?(payload, scope)
  payload['scp'] == scope.to_s
end