Class: ApiPack::JsonWebToken

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

Class Method Summary collapse

Class Method Details

.decode(token) ⇒ Object



14
15
16
17
18
# File 'lib/api_pack/json_web_token.rb', line 14

def self.decode(token)
  JWT.decode(token, ApiPack.hmac_secret).first
rescue JWT::DecodeError => e
  raise ApiPack::Errors::Auth::InvalidToken, e.message
end

.encode(payload, exp: (Time.now + 1 * 86_400).to_i) ⇒ Object

exp: 24 hours from now



4
5
6
7
8
9
10
11
12
# File 'lib/api_pack/json_web_token.rb', line 4

def self.encode(payload, exp: (Time.now + 1 * 86_400).to_i)
  raise ApiPack::Errors::Auth::MissingHmacSecret if ApiPack.hmac_secret.nil?

  payload[:exp] = exp.to_i

  JWT.encode(payload, ApiPack.hmac_secret)
rescue ApiPack::Errors::Auth::MissingHmacSecret
  raise ApiPack::Errors::Auth::MissingHmacSecret, 'ApiPach.hmac_secret is missing'
end