debian-mirror-gitlab/lib/json_web_token/token.rb

54 lines
1.1 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2018-12-23 12:14:25 +05:30
require 'securerandom'
2016-06-02 11:05:42 +05:30
module JSONWebToken
class Token
attr_accessor :issuer, :subject, :audience, :id
attr_accessor :issued_at, :not_before, :expire_time
2018-12-23 12:14:25 +05:30
DEFAULT_NOT_BEFORE_TIME = 5
DEFAULT_EXPIRE_TIME = 60
2016-06-02 11:05:42 +05:30
def initialize
@id = SecureRandom.uuid
@issued_at = Time.now
# we give a few seconds for time shift
2018-12-23 12:14:25 +05:30
@not_before = issued_at - DEFAULT_NOT_BEFORE_TIME
2016-06-02 11:05:42 +05:30
# default 60 seconds should be more than enough for this authentication token
2018-12-23 12:14:25 +05:30
@expire_time = issued_at + DEFAULT_EXPIRE_TIME
2016-06-02 11:05:42 +05:30
@custom_payload = {}
end
def [](key)
@custom_payload[key]
end
def []=(key, value)
@custom_payload[key] = value
end
def encoded
raise NotImplementedError
end
def payload
@custom_payload.merge(default_payload)
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
end