Class: Wor::Authentication::TokenManager

Inherits:
Object
  • Object
show all
Defined in:
lib/wor/authentication/token_manager.rb

Constant Summary collapse

ENCODING_ALGORITHM =
'HS256'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ TokenManager

Returns a new instance of TokenManager.



8
9
10
# File 'lib/wor/authentication/token_manager.rb', line 8

def initialize(key)
  @key = key
end

Instance Method Details

#decode(token) ⇒ Object



16
17
18
19
20
21
# File 'lib/wor/authentication/token_manager.rb', line 16

def decode(token)
  payload = JWT.decode(token, @key, true, algorithm: ENCODING_ALGORITHM)[0]
  Wor::Authentication::DecodedToken.new(payload)
rescue StandardError
  raise Wor::Authentication::Exceptions::InvalidAuthorizationToken
end

#encode(payload) ⇒ Object



12
13
14
# File 'lib/wor/authentication/token_manager.rb', line 12

def encode(payload)
  JWT.encode(payload, @key, ENCODING_ALGORITHM)
end