Module: JwtBouncer::Token

Defined in:
lib/jwt_bouncer/token.rb

Constant Summary collapse

ALGORITHM =
'HS256'

Class Method Summary collapse

Class Method Details

.decode(token, shared_secret) ⇒ Object



17
18
19
# File 'lib/jwt_bouncer/token.rb', line 17

def self.decode(token, shared_secret)
  JWT.decode(token, shared_secret, true, algorithm: ALGORITHM, verify_iat: true).dig(0, 'data')
end

.encode(data, shared_secret, expiry: nil) ⇒ Object



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

def self.encode(data, shared_secret, expiry: nil)
  # setup our base payload
  payload = { data: data, iat: Time.now.utc.to_i }
  # apply expiry, if necessary
  payload[:exp] = expiry.to_i if expiry
  # build the JWT
  JWT.encode(payload, shared_secret, ALGORITHM)
end