Class: Compeon::Token::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/compeon/token/base.rb

Direct Known Subclasses

Access

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(audience: nil, expires_at: nil, issued_at: nil, issuer: nil, not_before: nil, subject: nil) ⇒ Base



34
35
36
37
38
39
40
41
# File 'lib/compeon/token/base.rb', line 34

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

Instance Attribute Details

#audienceObject

Returns the value of attribute audience.



6
7
8
# File 'lib/compeon/token/base.rb', line 6

def audience
  @audience
end

#expires_atObject

Returns the value of attribute expires_at.



6
7
8
# File 'lib/compeon/token/base.rb', line 6

def expires_at
  @expires_at
end

#issued_atObject

Returns the value of attribute issued_at.



6
7
8
# File 'lib/compeon/token/base.rb', line 6

def issued_at
  @issued_at
end

#issuerObject

Returns the value of attribute issuer.



6
7
8
# File 'lib/compeon/token/base.rb', line 6

def issuer
  @issuer
end

#not_beforeObject

Returns the value of attribute not_before.



6
7
8
# File 'lib/compeon/token/base.rb', line 6

def not_before
  @not_before
end

#subjectObject

Returns the value of attribute subject.



6
7
8
# File 'lib/compeon/token/base.rb', line 6

def subject
  @subject
end

Class Method Details

.attributesObject



9
10
11
# File 'lib/compeon/token/base.rb', line 9

def attributes
  @attributes ||= attributes_mapping.keys.freeze
end

.decode(claim_verifications: {}, encoded_token:, key:) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/compeon/token/base.rb', line 24

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

.registered_claims_mappingObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/compeon/token/base.rb', line 13

def registered_claims_mapping
  {
    audience: :aud,
    expires_at: :exp,
    issued_at: :iat,
    issuer: :iss,
    not_before: :nbf,
    subject: :sub
  }.freeze
end

Instance Method Details

#attributes_valid?Boolean



67
68
69
# File 'lib/compeon/token/base.rb', line 67

def attributes_valid?
  self.class.attributes.none? { |accessor| public_send(accessor).nil? }
end

#encode(key:) ⇒ Object



43
44
45
46
47
48
# File 'lib/compeon/token/base.rb', line 43

def encode(key:)
  Compeon::Token::Encoder.new(
    key: key,
    token: self
  ).encode
end

#expires_at_valid?Boolean



63
64
65
# File 'lib/compeon/token/base.rb', line 63

def expires_at_valid?
  !expires_at.nil? && expires_at > Time.now.to_i
end

#registered_claimsObject



50
51
52
53
54
55
56
57
# File 'lib/compeon/token/base.rb', line 50

def registered_claims
  self
    .class
    .registered_claims_mapping
    .invert
    .transform_values { |claim| public_send(claim) }
    .compact
end

#valid?Boolean



59
60
61
# File 'lib/compeon/token/base.rb', line 59

def valid?
  expires_at_valid? && attributes_valid?
end