Class: JsonWebToken

Inherits:
Object
  • Object
show all
Defined in:
lib/json_web_token.rb

Class Method Summary collapse

Class Method Details

.decode(token, refresh: false) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/json_web_token.rb', line 9

def decode(token, refresh: false)
  secret = extract_secret(token, refresh: refresh)
  body = JWT.decode(token, secret)[0]
  HashWithIndifferentAccess.new body
rescue
  nil
end

.encode(payload, exp:, jti:, secret:) ⇒ Object



3
4
5
6
7
# File 'lib/json_web_token.rb', line 3

def encode(payload, exp:, jti:, secret:)
  payload[:exp] = exp.to_i
  payload[:jti] = jti
  JWT.encode(payload, secret)
end

.extract_secret(token, refresh: false) ⇒ Object



17
18
19
20
21
22
# File 'lib/json_web_token.rb', line 17

def extract_secret(token, refresh: false)
  key = JSON.parse(Base64.decode64 token.split('.')[1])['jti']
  $redis.get("token:#{key}").tap do
    $redis.del("token:#{key}") if refresh
  end
end