Class: Redox::Authentication
Constant Summary
collapse
- BASE_ENDPOINT =
'/auth'
- AUTH_ENDPOINT =
"#{BASE_ENDPOINT}/authenticate"
- REFRESH_ENDPOINT =
"#{BASE_ENDPOINT}/refreshToken"
- @@token_expiry_padding =
0
Constants inherited
from Connection
Connection::DEFAULT_ENDPOINT
Class Attribute Summary collapse
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods inherited from Connection
#request
Constructor Details
Returns a new instance of Authentication.
21
22
23
|
# File 'lib/redox/authentication.rb', line 21
def initialize
@response = nil
end
|
Class Attribute Details
.token_expiry_padding ⇒ Object
Returns the value of attribute token_expiry_padding.
16
17
18
|
# File 'lib/redox/authentication.rb', line 16
def token_expiry_padding
@token_expiry_padding
end
|
Instance Attribute Details
#response ⇒ Object
Returns the value of attribute response.
8
9
10
|
# File 'lib/redox/authentication.rb', line 8
def response
@response
end
|
Instance Method Details
73
74
75
76
77
|
# File 'lib/redox/authentication.rb', line 73
def
{
'Authorization' => "Bearer #{access_token}"
}
end
|
#access_token ⇒ Object
52
53
54
|
# File 'lib/redox/authentication.rb', line 52
def access_token
@response['accessToken'] if @response
end
|
#authenticate ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/redox/authentication.rb', line 25
def authenticate
if expires?
request = if refresh_token
{
body: { apiKey: Redox.configuration.api_key, refreshToken: refresh_token },
endpoint: REFRESH_ENDPOINT
}
else
{
body: { apiKey: Redox.configuration.api_key, secret: Redox.configuration.secret },
endpoint: AUTH_ENDPOINT
}
end
response = self.request(**request, auth: false)
if false == response.ok?
@response = nil
raise RedoxException.from_response(response, msg: 'Authentication')
else
@response = response
end
end
self
end
|
#expire! ⇒ Object
79
80
81
|
# File 'lib/redox/authentication.rb', line 79
def expire!
@response = nil
end
|
#expires?(seconds_from_now = Authentication.token_expiry_padding) ⇒ Boolean
#expiry ⇒ Object
56
57
58
|
# File 'lib/redox/authentication.rb', line 56
def expiry
@response['expires'] if @response
end
|
#refresh_token ⇒ Object
60
61
62
|
# File 'lib/redox/authentication.rb', line 60
def refresh_token
@response['refreshToken'] if @response
end
|