2018-10-08 10:50:39 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-08 04:28:13 -05:00
|
|
|
require 'securerandom'
|
|
|
|
|
2016-05-14 19:23:31 -04:00
|
|
|
module JSONWebToken
|
2016-05-02 08:32:16 -04:00
|
|
|
class Token
|
|
|
|
attr_accessor :issuer, :subject, :audience, :id
|
|
|
|
attr_accessor :issued_at, :not_before, :expire_time
|
|
|
|
|
2018-11-08 04:28:13 -05:00
|
|
|
DEFAULT_NOT_BEFORE_TIME = 5
|
|
|
|
DEFAULT_EXPIRE_TIME = 60
|
|
|
|
|
2016-05-02 08:32:16 -04:00
|
|
|
def initialize
|
|
|
|
@id = SecureRandom.uuid
|
|
|
|
@issued_at = Time.now
|
2016-05-09 15:04:42 -04:00
|
|
|
# we give a few seconds for time shift
|
2018-11-08 04:28:13 -05:00
|
|
|
@not_before = issued_at - DEFAULT_NOT_BEFORE_TIME
|
2016-05-09 15:04:42 -04:00
|
|
|
# default 60 seconds should be more than enough for this authentication token
|
2018-11-08 04:28:13 -05:00
|
|
|
@expire_time = issued_at + DEFAULT_EXPIRE_TIME
|
2016-05-09 15:04:42 -04:00
|
|
|
@custom_payload = {}
|
2016-05-02 08:32:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def [](key)
|
2016-05-09 15:04:42 -04:00
|
|
|
@custom_payload[key]
|
2016-05-02 08:32:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def []=(key, value)
|
2016-05-09 15:04:42 -04:00
|
|
|
@custom_payload[key] = value
|
2016-05-02 08:32:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def encoded
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def payload
|
2016-05-09 15:04:42 -04:00
|
|
|
@custom_payload.merge(default_payload)
|
2016-05-02 08:32:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def default_payload
|
|
|
|
{
|
|
|
|
jti: id,
|
|
|
|
aud: audience,
|
|
|
|
sub: subject,
|
|
|
|
iss: issuer,
|
|
|
|
iat: issued_at.to_i,
|
|
|
|
nbf: not_before.to_i,
|
|
|
|
exp: expire_time.to_i
|
|
|
|
}.compact
|
|
|
|
end
|
|
|
|
end
|
2016-05-12 13:47:55 -04:00
|
|
|
end
|