Class: Redox::Authentication

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

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

#initializeAuthentication

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_paddingObject

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

#responseObject

Returns the value of attribute response.



8
9
10
# File 'lib/redox/authentication.rb', line 8

def response
  @response
end

Instance Method Details

#access_headerObject



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

def access_header
  {
    'Authorization' => "Bearer #{access_token}"
  }
end

#access_tokenObject



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

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

#authenticateObject



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

Returns:

  • (Boolean)


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

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

#expiryObject



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

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

#refresh_tokenObject



60
61
62
# File 'lib/redox/authentication.rb', line 60

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