Class: JWTSessions::Token

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

Constant Summary collapse

DECODE_ERROR =
"cannot decode the token"

Class Method Summary collapse

Class Method Details

.decode(token, claims = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/jwt_sessions/token.rb', line 15

def decode(token, claims = {})
  decode_options = { algorithm: JWTSessions.algorithm }.merge(JWTSessions.jwt_options).merge(claims)
  JWT.decode(token, JWTSessions.public_key, JWTSessions.validate?, decode_options)
rescue JWT::ExpiredSignature => e
  raise Errors::Expired, e.message
rescue JWT::InvalidIssuerError, JWT::InvalidIatError, JWT::InvalidAudError, JWT::InvalidSubError, JWT::InvalidJtiError => e
  raise Errors::ClaimsVerification, e.message
rescue JWT::DecodeError => e
  raise Errors::Unauthorized, e.message
rescue StandardError
  raise Errors::Unauthorized, DECODE_ERROR
end

.decode!(token) ⇒ Object



28
29
30
31
32
33
# File 'lib/jwt_sessions/token.rb', line 28

def decode!(token)
  decode_options = { algorithm: JWTSessions.algorithm }
  JWT.decode(token, JWTSessions.public_key, false, decode_options)
rescue StandardError
  raise Errors::Unauthorized, DECODE_ERROR
end

.encode(payload) ⇒ Object



10
11
12
13
# File 'lib/jwt_sessions/token.rb', line 10

def encode(payload)
  exp_payload = meta.merge(payload)
  JWT.encode(exp_payload, JWTSessions.private_key, JWTSessions.algorithm)
end

.metaObject



35
36
37
# File 'lib/jwt_sessions/token.rb', line 35

def meta
  { "exp" => JWTSessions.access_expiration }
end