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
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.

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)
  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