Class: Warden::JWTAuth::UserEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/warden/jwt_auth/user_encoder.rb

Overview

Layer above token encoding which directly encodes a user to a JWT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUserEncoder

Returns a new instance of UserEncoder.



11
12
13
# File 'lib/warden/jwt_auth/user_encoder.rb', line 11

def initialize
  @helper = PayloadUserHelper
end

Instance Attribute Details

#helperObject (readonly)

Returns the value of attribute helper.



9
10
11
# File 'lib/warden/jwt_auth/user_encoder.rb', line 9

def helper
  @helper
end

Instance Method Details

#call(user, scope, aud) ⇒ String

Encodes a user for given scope into a JWT.

Payload generated includes:

  • a ‘sub` claim which is build calling `jwt_subject` in `user`

  • an ‘aud` claim taken as it is in the `aud` parameter

  • a custom ‘scp` claim taken as the value of the `scope` parameter

as a string.

The result of calling ‘jwt_payload` in user is also merged into the payload.

Parameters:

  • user (Interfaces::User)

    an user, whatever it is

  • scope (Symbol)

    Warden scope

  • aud (String)

    JWT aud claim

Returns:

  • (String, String)

    encoded JWT and decoded payload



31
32
33
34
35
# File 'lib/warden/jwt_auth/user_encoder.rb', line 31

def call(user, scope, aud)
  payload = helper.payload_for_user(user, scope).merge('aud' => aud)
  token = TokenEncoder.new.call(payload)
  [token, payload]
end