Class: Knock::AuthToken

Inherits:
Object
  • Object
show all
Defined in:
app/model/knock/auth_token.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload: {}, token: nil) ⇒ AuthToken

Returns a new instance of AuthToken.



8
9
10
11
12
13
14
15
16
17
18
# File 'app/model/knock/auth_token.rb', line 8

def initialize payload: {}, token: nil
  if token.present?
    @payload, _ = JWT.decode token, decode_key, true, options
    @token = token
  else
    @payload = payload
    @token = JWT.encode claims.merge(payload),
      secret_key,
      Knock.token_signature_algorithm
  end
end

Instance Attribute Details

#payloadObject (readonly)

Returns the value of attribute payload.



6
7
8
# File 'app/model/knock/auth_token.rb', line 6

def payload
  @payload
end

#tokenObject (readonly)

Returns the value of attribute token.



5
6
7
# File 'app/model/knock/auth_token.rb', line 5

def token
  @token
end

Instance Method Details

#entity_for(entity_class) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/model/knock/auth_token.rb', line 20

def entity_for entity_class
  if entity_class.respond_to? :from_token_payload
    entity_class.from_token_payload @payload
  else
    if entity_class.to_s == "User" && Knock.respond_to?(:current_user_from_token)
      warn "[DEPRECATION]: `Knock.current_user_from_token` is deprecated. Please implement `User.from_token_payload` instead."
      Knock.current_user_from_token.call @payload
    else
      entity_class.find @payload['sub']
    end
  end
end

#to_json(options = {}) ⇒ Object



33
34
35
# File 'app/model/knock/auth_token.rb', line 33

def to_json options = {}
  {jwt: @token}.to_json
end