class Compeon::Token::Base

Attributes

audience[RW]
expires_at[RW]
issued_at[RW]
issuer[RW]
not_before[RW]
subject[RW]

Public Class Methods

attributes() click to toggle source
# File lib/compeon/token/base.rb, line 9
def attributes
  @attributes ||= attributes_mapping.keys.freeze
end
attributes_mapping() click to toggle source
# File lib/compeon/token/base.rb, line 13
def attributes_mapping
  required_attributes_mapping.merge(optional_attributes_mapping).freeze
end
decode(claim_verifications: {}, encoded_token:, key:) click to toggle source
# File lib/compeon/token/base.rb, line 36
def decode(claim_verifications: {}, encoded_token:, key:)
  Compeon::Token::Decoder.new(
    claim_verifications: claim_verifications,
    encoded_token: encoded_token,
    key: key,
    token_klass: self
  ).decode
end
new(audience: nil, expires_at: nil, issued_at: nil, issuer: nil, not_before: nil, subject: nil) click to toggle source
# File lib/compeon/token/base.rb, line 46
def initialize(audience: nil, expires_at: nil, issued_at: nil, issuer: nil, not_before: nil, subject: nil)
  @audience = audience
  @expires_at = expires_at
  @issued_at = issued_at
  @issuer = issuer
  @not_before = not_before
  @subject = subject
end
optional_attributes() click to toggle source
# File lib/compeon/token/base.rb, line 21
def optional_attributes
  @optional_attributes ||= optional_attributes_mapping.keys.freeze
end
registered_claims_mapping() click to toggle source
# File lib/compeon/token/base.rb, line 25
def registered_claims_mapping
  {
    audience: :aud,
    expires_at: :exp,
    issued_at: :iat,
    issuer: :iss,
    not_before: :nbf,
    subject: :sub
  }.freeze
end
required_attributes() click to toggle source
# File lib/compeon/token/base.rb, line 17
def required_attributes
  @required_attributes ||= required_attributes_mapping.keys.freeze
end

Public Instance Methods

attributes_valid?() click to toggle source
# File lib/compeon/token/base.rb, line 79
def attributes_valid?
  self.class.required_attributes.none? { |accessor| public_send(accessor).nil? }
end
encode(key:) click to toggle source
# File lib/compeon/token/base.rb, line 55
def encode(key:)
  Compeon::Token::Encoder.new(
    key: key,
    token: self
  ).encode
end
expires_at_valid?() click to toggle source
# File lib/compeon/token/base.rb, line 75
def expires_at_valid?
  !expires_at.nil? && expires_at > Time.now.to_i
end
registered_claims() click to toggle source
# File lib/compeon/token/base.rb, line 62
def registered_claims
  self
    .class
    .registered_claims_mapping
    .invert
    .transform_values { |claim| public_send(claim) }
    .compact
end
valid?() click to toggle source
# File lib/compeon/token/base.rb, line 71
def valid?
  expires_at_valid? && attributes_valid?
end