gitlab-org--gitlab-foss/lib/json_web_token/token.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

54 lines
1.1 KiB
Ruby
Raw Normal View History

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