Class: Mobius::Client::Auth::Jwt

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(secret) ⇒ Object

Parameters:

  • secret (String)

    JWT secret



8
# File 'lib/mobius/client/auth/jwt.rb', line 8

param :secret

Instance Method Details

#decode!(jwt) ⇒ Hash

Returns decoded JWT token.

Parameters:

  • jwt (String)

    JWT token

Returns:

  • (Hash)

    Decoded token params



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.

Parameters:

Returns:

  • (String)

    JWT token



13
14
15
16
17
18
19
20
21
22
# File 'lib/mobius/client/auth/jwt.rb', line 13

def encode(token, options = {})
  payload = {
    jti: token.hash(:hex),
    sub: token.address,
    iat: token.time_bounds.min_time,
    exp: token.time_bounds.max_time
  }.merge(options)

  JWT.encode(payload, secret, ALG)
end