Class: Fridge::AccessToken

Inherits:
Object
  • Object
show all
Defined in:
lib/fridge/access_token.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jwt_or_options = nil) ⇒ AccessToken

rubocop:disable MethodLength



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fridge/access_token.rb', line 9

def initialize(jwt_or_options = nil)
  options = case jwt_or_options
            when String
              self.jwt = jwt_or_options
              validate_public_key!
              decode_and_verify(jwt_or_options)
            when Hash then jwt_or_options
            else {}
            end
  [:id, :issuer, :subject, :scope, :expires_at].each do |key|
    send "#{key}=", options.delete(key)
  end
  self.attributes = options.reject { |k, v| v.nil? }
  self.attributes = Hash[attributes.map { |k, v| [k.to_sym, v] }]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (protected)



100
101
102
103
104
105
106
# File 'lib/fridge/access_token.rb', line 100

def method_missing(method, *args, &block)
  if attributes.key?(method)
    attributes[method]
  else
    super
  end
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



5
6
7
# File 'lib/fridge/access_token.rb', line 5

def attributes
  @attributes
end

#expires_atObject

Returns the value of attribute expires_at.



5
6
7
# File 'lib/fridge/access_token.rb', line 5

def expires_at
  @expires_at
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/fridge/access_token.rb', line 5

def id
  @id
end

#issuerObject

Returns the value of attribute issuer.



5
6
7
# File 'lib/fridge/access_token.rb', line 5

def issuer
  @issuer
end

#jwtObject

Returns the value of attribute jwt.



5
6
7
# File 'lib/fridge/access_token.rb', line 5

def jwt
  @jwt
end

#scopeObject

Returns the value of attribute scope.



5
6
7
# File 'lib/fridge/access_token.rb', line 5

def scope
  @scope
end

#subjectObject

Returns the value of attribute subject.



5
6
7
# File 'lib/fridge/access_token.rb', line 5

def subject
  @subject
end

Instance Method Details

#algorithmObject



90
91
92
# File 'lib/fridge/access_token.rb', line 90

def algorithm
  config.signing_algorithm
end

#configObject



94
95
96
# File 'lib/fridge/access_token.rb', line 94

def config
  Fridge.configuration
end

#decode_and_verify(jwt) ⇒ Object

rubocop:disable MethodLength



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fridge/access_token.rb', line 50

def decode_and_verify(jwt)
  hash = JWT.decode(jwt, public_key)
  base = {
    id: hash.delete('id'),
    issuer: hash.delete('iss'),
    subject: hash.delete('sub'),
    scope: hash.delete('scope'),
    expires_at: Time.at(hash.delete('exp'))
  }
  base.merge(hash)
rescue JWT::DecodeError
  raise InvalidToken, 'Invalid access token'
end

#encode_and_signObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fridge/access_token.rb', line 37

def encode_and_sign
  JWT.encode({
    id: id,
    iss: issuer,
    sub: subject,
    scope: scope,
    exp: expires_at.to_i
  }.merge(attributes), private_key, algorithm)
rescue
  raise SerializationError, 'Invalid private key or signing algorithm'
end

#expired?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/fridge/access_token.rb', line 69

def expired?
  expires_at.nil? || expires_at < Time.now
end

#private_keyObject



73
74
75
76
77
78
# File 'lib/fridge/access_token.rb', line 73

def private_key
  return unless config.private_key
  @private_key ||= OpenSSL::PKey::RSA.new(config.private_key)
rescue
  nil
end

#public_keyObject



80
81
82
83
84
85
86
87
88
# File 'lib/fridge/access_token.rb', line 80

def public_key
  if config.private_key
    @public_key ||= OpenSSL::PKey::RSA.new(config.private_key).public_key
  elsif config.public_key
    @public_key ||= OpenSSL::PKey::RSA.new(config.public_key)
  end
rescue
  nil
end

#serializeObject



30
31
32
33
34
35
# File 'lib/fridge/access_token.rb', line 30

def serialize
  return jwt if jwt
  validate_parameters!
  validate_private_key!
  encode_and_sign
end

#to_sObject

rubocop:enable MethodLength



26
27
28
# File 'lib/fridge/access_token.rb', line 26

def to_s
  serialize
end

#valid?Boolean

rubocop:enable MethodLength

Returns:

  • (Boolean)


65
66
67
# File 'lib/fridge/access_token.rb', line 65

def valid?
  !expired?
end