Class: Redox::Authentication

Inherits:
Connection show all
Defined in:
lib/redox/authentication.rb

Constant Summary collapse

BASE_ENDPOINT =
'/auth'.freeze
AUTH_ENDPOINT =
"#{BASE_ENDPOINT}/authenticate".freeze
REFRESH_ENDPOINT =
"#{BASE_ENDPOINT}/refreshToken".freeze
@@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

#initializeAuthentication

Returns a new instance of Authentication.



16
17
18
# File 'lib/redox/authentication.rb', line 16

def initialize
  @response = nil
end

Class Attribute Details

.token_expiry_paddingObject

Returns the value of attribute token_expiry_padding.



11
12
13
# File 'lib/redox/authentication.rb', line 11

def token_expiry_padding
  @token_expiry_padding
end

Instance Attribute Details

#responseObject

Returns the value of attribute response.



3
4
5
# File 'lib/redox/authentication.rb', line 3

def response
  @response
end

Instance Method Details

#access_headerObject



67
68
69
70
71
# File 'lib/redox/authentication.rb', line 67

def access_header
  return {
    'Authorization' => "Bearer #{self.access_token}",
  }
end

#access_tokenObject



47
48
49
# File 'lib/redox/authentication.rb', line 47

def access_token
  return @response['accessToken'] if @response
end

#authenticateObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/redox/authentication.rb', line 20

def authenticate
  if (self.expires?)
    if (self.refresh_token)
      request = {
        body: { apiKey: Redox.configuration.api_key, refreshToken: self.refresh_token },
        endpoint: REFRESH_ENDPOINT
      }
    else
      request = {
        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

  return self
end

#expire!Object



73
74
75
# File 'lib/redox/authentication.rb', line 73

def expire!
  @response = nil
end

#expires?(seconds_from_now = Authentication.token_expiry_padding) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
# File 'lib/redox/authentication.rb', line 59

def expires?(seconds_from_now = Authentication.token_expiry_padding)
  if (self.expiry)
    return DateTime.strptime(self.expiry, Models::Meta::FROM_DATETIME_FORMAT).to_time.utc <= (Time.now + seconds_from_now).utc
  else
    return true
  end
end

#expiryObject



51
52
53
# File 'lib/redox/authentication.rb', line 51

def expiry
  return @response['expires'] if @response
end

#refresh_tokenObject



55
56
57
# File 'lib/redox/authentication.rb', line 55

def refresh_token
  return @response['refreshToken'] if @response
end