BigW Consortium Gitlab

token.rb 967 Bytes
Newer Older
1
module JSONWebToken
2 3 4 5 6 7 8
  class Token
    attr_accessor :issuer, :subject, :audience, :id
    attr_accessor :issued_at, :not_before, :expire_time

    def initialize
      @id = SecureRandom.uuid
      @issued_at = Time.now
9
      # we give a few seconds for time shift
10
      @not_before = issued_at - 5.seconds
11
      # default 60 seconds should be more than enough for this authentication token
12
      @expire_time = issued_at + 1.minute
13
      @custom_payload = {}
14 15 16
    end

    def [](key)
17
      @custom_payload[key]
18 19 20
    end

    def []=(key, value)
21
      @custom_payload[key] = value
22 23 24 25 26 27 28
    end

    def encoded
      raise NotImplementedError
    end

    def payload
29
      @custom_payload.merge(default_payload)
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    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
Kamil Trzcinski committed
46
end