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) ⇒ 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 32 33 |
# File 'lib/mobius/client/auth/jwt.rb', line 27 def decode!(jwt) OpenStruct.new( JWT.decode(jwt, secret, true, algorithm: ALG).first ).tap do |payload| raise TokenExpired unless (payload.min_time..payload.max_time).cover?(Time.now.to_i) end end |
#encode(token) ⇒ 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 = { hash: token.hash(:hex), public_key: token.address, min_time: token.time_bounds.min_time, max_time: token.time_bounds.max_time } JWT.encode(payload, secret, ALG) end |