Class: Fridge::AccessToken
- Inherits:
-
Object
- Object
- Fridge::AccessToken
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
Returns a new instance of AccessToken.
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/fridge/access_token.rb', line 8
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, :actor].each do |key|
send "#{key}=", options.delete(key)
end
self.attributes = options
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
97
98
99
100
101
102
103
|
# File 'lib/fridge/access_token.rb', line 97
def method_missing(method, *args, &block)
if attributes.key?(method)
attributes[method]
else
super
end
end
|
Instance Attribute Details
#actor ⇒ Object
Returns the value of attribute actor.
5
6
7
|
# File 'lib/fridge/access_token.rb', line 5
def actor
@actor
end
|
#attributes ⇒ Object
Returns the value of attribute attributes.
5
6
7
|
# File 'lib/fridge/access_token.rb', line 5
def attributes
@attributes
end
|
#expires_at ⇒ Object
Returns the value of attribute expires_at.
5
6
7
|
# File 'lib/fridge/access_token.rb', line 5
def expires_at
@expires_at
end
|
#id ⇒ Object
Returns the value of attribute id.
5
6
7
|
# File 'lib/fridge/access_token.rb', line 5
def id
@id
end
|
#issuer ⇒ Object
Returns the value of attribute issuer.
5
6
7
|
# File 'lib/fridge/access_token.rb', line 5
def issuer
@issuer
end
|
#jwt ⇒ Object
Returns the value of attribute jwt.
5
6
7
|
# File 'lib/fridge/access_token.rb', line 5
def jwt
@jwt
end
|
#scope ⇒ Object
Returns the value of attribute scope.
5
6
7
|
# File 'lib/fridge/access_token.rb', line 5
def scope
@scope
end
|
#subject ⇒ Object
Returns the value of attribute subject.
5
6
7
|
# File 'lib/fridge/access_token.rb', line 5
def subject
@subject
end
|
Instance Method Details
#algorithm ⇒ Object
87
88
89
|
# File 'lib/fridge/access_token.rb', line 87
def algorithm
config.signing_algorithm
end
|
#config ⇒ Object
91
92
93
|
# File 'lib/fridge/access_token.rb', line 91
def config
Fridge.configuration
end
|
#decode_and_verify(jwt) ⇒ Object
48
49
50
51
52
53
54
55
|
# File 'lib/fridge/access_token.rb', line 48
def decode_and_verify(jwt)
payload, = JWT.decode(jwt, public_key, true, algorithm: algorithm)
decode_from_jwt(payload)
rescue JWT::ExpiredSignature => e
raise ExpiredToken, e.message
rescue JWT::DecodeError => e
raise InvalidToken, e.message
end
|
#downgrade ⇒ Object
57
58
59
|
# File 'lib/fridge/access_token.rb', line 57
def downgrade
self.scope = 'read'
end
|
#encode_and_sign ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/fridge/access_token.rb', line 36
def encode_and_sign
h = {}
[:id, :issuer, :subject, :scope, :expires_at, :actor].each do |key|
h[key] = send(key)
end
h.merge!(attributes)
h = encode_for_jwt(h)
JWT.encode(h, private_key, algorithm)
rescue StandardError
raise SerializationError, 'Invalid private key or signing algorithm'
end
|
#expired? ⇒ Boolean
65
66
67
|
# File 'lib/fridge/access_token.rb', line 65
def expired?
expires_at.nil? || expires_at < Time.now
end
|
#private_key ⇒ Object
69
70
71
72
73
74
75
|
# File 'lib/fridge/access_token.rb', line 69
def private_key
return unless config.private_key
@private_key ||= OpenSSL::PKey::RSA.new(config.private_key)
rescue StandardError
nil
end
|
#public_key ⇒ Object
77
78
79
80
81
82
83
84
85
|
# File 'lib/fridge/access_token.rb', line 77
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 StandardError
nil
end
|
#serialize ⇒ Object
28
29
30
31
32
33
34
|
# File 'lib/fridge/access_token.rb', line 28
def serialize
return jwt if jwt
validate_parameters!
validate_private_key!
encode_and_sign
end
|
#to_s ⇒ Object
24
25
26
|
# File 'lib/fridge/access_token.rb', line 24
def to_s
serialize
end
|
#valid? ⇒ Boolean
61
62
63
|
# File 'lib/fridge/access_token.rb', line 61
def valid?
!expired?
end
|