Class: Mobius::Client::Auth::Jwt
- Inherits:
-
Object
- Object
- Mobius::Client::Auth::Jwt
- Extended by:
- Dry::Initializer
- Defined in:
- lib/mobius/client/auth/jwt.rb
Overview
Generates JWT token based on valid token transaction signed by both parties and decodes JWT token into hash.
Constant Summary collapse
- ALG =
Used JWT algorithm
"HS512".freeze
Instance Method Summary collapse
-
#decode!(jwt) ⇒ Hash
Returns decoded JWT token.
-
#encode(token, options = {}) ⇒ String
Returns JWT token.
- #initialize(secret) ⇒ Object constructor
Constructor Details
#initialize(secret) ⇒ Object
8 |
# File 'lib/mobius/client/auth/jwt.rb', line 8 param :secret |
Instance Method Details
#decode!(jwt) ⇒ Hash
Returns decoded JWT token.
27 28 29 30 31 |
# File 'lib/mobius/client/auth/jwt.rb', line 27 def decode!(jwt) OpenStruct.new(JWT.decode(jwt, secret, true, algorithm: ALG).first) rescue JWT::ExpiredSignature => _ raise TokenExpired end |
#encode(token, options = {}) ⇒ String
Returns JWT token.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/mobius/client/auth/jwt.rb', line 13 def encode(token, = {}) payload = { jti: token.hash(:hex), sub: token.address, iat: token.time_bounds.min_time, exp: token.time_bounds.max_time }.merge() JWT.encode(payload, secret, ALG) end |