Module: Ultradns::Api::Authentication

Included in:
Client
Defined in:
lib/ultradns/api/authentication.rb

Overview

Copyright 2000-2014 NeuStar, Inc. All rights reserved. NeuStar, the Neustar logo and related names and logos are registered trademarks, service marks or tradenames of NeuStar, Inc. All other product names, company names, marks, logos and symbols may be trademarks of their respective owners.

Instance Method Summary collapse

Instance Method Details

#access_tokenObject



106
107
108
# File 'lib/ultradns/api/authentication.rb', line 106

def access_token
  @auth[:access_token]
end

#add_auth_header!(params) ⇒ Object



36
37
38
39
40
# File 'lib/ultradns/api/authentication.rb', line 36

def add_auth_header!(params)
  refresh if params[:force_refresh]
  (params[:headers] ||= {})['Authorization'] = "Bearer #{access_token}"
  params
end

#auth(username, password, base_uri) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ultradns/api/authentication.rb', line 42

def auth(username, password, base_uri)
  @auth = {}
  @auth[:requested_at] = Time.now.to_i
  response = self.class.post('/authorization/token',
    body: {
      grant_type: 'password',
      username: username,
      password: password
    },
    headers: {
      'Content-Type' => 'application/x-www-form-urlencoded'
    },
    base_uri: base_uri
  )

  body = response.parsed_response

  logger.debug "Auth Response: #{response.inspect}"
  raise "Authentication Error: #{response.body}" unless response.code == 200

  # temp
  @auth[:username] = username
  @auth[:password] = password
  @auth[:base_url] = base_uri

  @auth[:access_token] = body['accessToken']
  @auth[:refresh_token] = body['refreshToken']
  @auth[:expires_in] = body['expiresIn']
end

#auth_failure?(response) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
# File 'lib/ultradns/api/authentication.rb', line 31

def auth_failure?(response)
  error_code = response.parsed_response["errorCode"] rescue nil
  (response.code == 400 || response.code == 401) && error_code == 60001
end

#no_expiry?Boolean

Returns:

  • (Boolean)


72
73
74
75
# File 'lib/ultradns/api/authentication.rb', line 72

def no_expiry?
  # no expires provided yet - TBD
  @auth[:expires_in] == nil || @auth[:expires_in] == ''
end

#refreshObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ultradns/api/authentication.rb', line 81

def refresh
  # expires not available, then reauth
  return auth(@auth[:username], @auth[:password], @auth[:base_url]) if no_expiry?

  # not expired yet.
  return unless refresh?

  requested_at = Time.now.to_i
  response = @client.post('/authorization/token') do |request|
    request.params[:grant_type] = 'refresh_token'
    request.params[:refreshToken] = @auth[:refresh_token]
  end

  logger.debug "Auth Refresh Response: #{response.inspect}"
  raise "Token Refresh Error: #{response.body}" unless response.code == 200

  body = response.parsed_response

  @auth.clear # reset everything
  @auth[:requested_at] = requested_at
  @auth[:access_token] = body['accessToken']
  @auth[:refresh_token] = body['refreshToken']
  @auth[:expires_in] = body['expiresIn']
end

#refresh?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/ultradns/api/authentication.rb', line 77

def refresh?
  (@auth[:requested_at] + @auth[:expires_in]) >= Time.now.to_i
end

#with_auth_retry(&block) ⇒ Object

Try a request and if there is an authentication failure, retry after refreshing the tokens.

yields to the block with the class so that HTTParty class methods can be used. Example: with_auth_retry {|c| c.get ‘/’ }



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ultradns/api/authentication.rb', line 15

def with_auth_retry(&block)
  retries = 3
  response = nil
  while retries > 0
    response = yield(self.class)
    if auth_failure?(response)
      refresh
      retries = retries - 1
      logger.info "authentication failure, retrying..." if retries > 0
    else
      retries = 0
    end
  end
  response
end