Class: AdminApi::AuthConfiguration

Inherits:
Configuration show all
Defined in:
lib/admin_api/auth_configuration.rb

Instance Attribute Summary collapse

Attributes inherited from Configuration

#access_token, #api_key, #api_key_prefix, #base_path, #cert_file, #client_side_validation, #debugging, #force_ending_format, #host, #inject_format, #key_file, #logger, #params_encoding, #scheme, #ssl_ca_cert, #temp_folder_path, #timeout, #verify_ssl, #verify_ssl_host

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Configuration

#api_key_with_prefix, #auth_settings, #base_url, #basic_auth_token

Constructor Details

#initialize {|_self| ... } ⇒ AuthConfiguration

Returns a new instance of AuthConfiguration.

Yields:

  • (_self)

Yield Parameters:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/admin_api/auth_configuration.rb', line 32

def initialize
  super
  @authorization = 'Authorization'
  @client_token = 'client_token'
  @grant_type_key = 'grant_type'
  @client_credentials = 'client_credentials'
  @password = 'password'
  @username = 'username'
  @application_json_value = 'application/json'
  @auth_uri = '/authorization/v1/oauth/token'
  @client_token_auth_uri = '/authorization/v1/client-token'
  @bearer = 'Bearer '
  @config = Configuration.default
  yield(self) if block_given?
end

Instance Attribute Details

#application_json_valueObject

Returns the value of attribute application_json_value.



26
27
28
# File 'lib/admin_api/auth_configuration.rb', line 26

def application_json_value
  @application_json_value
end

#auth_uriObject

Returns the value of attribute auth_uri.



27
28
29
# File 'lib/admin_api/auth_configuration.rb', line 27

def auth_uri
  @auth_uri
end

#authorizationObject

Returns the value of attribute authorization.



20
21
22
# File 'lib/admin_api/auth_configuration.rb', line 20

def authorization
  @authorization
end

#bearerObject

Returns the value of attribute bearer.



30
31
32
# File 'lib/admin_api/auth_configuration.rb', line 30

def bearer
  @bearer
end

#client_credentialsObject

Returns the value of attribute client_credentials.



23
24
25
# File 'lib/admin_api/auth_configuration.rb', line 23

def client_credentials
  @client_credentials
end

#client_tokenObject

Returns the value of attribute client_token.



21
22
23
# File 'lib/admin_api/auth_configuration.rb', line 21

def client_token
  @client_token
end

#client_token_auth_uriObject

Returns the value of attribute client_token_auth_uri.



28
29
30
# File 'lib/admin_api/auth_configuration.rb', line 28

def client_token_auth_uri
  @client_token_auth_uri
end

#configObject

Returns the value of attribute config.



29
30
31
# File 'lib/admin_api/auth_configuration.rb', line 29

def config
  @config
end

#grant_type_keyObject

Returns the value of attribute grant_type_key.



22
23
24
# File 'lib/admin_api/auth_configuration.rb', line 22

def grant_type_key
  @grant_type_key
end

#passwordObject

Returns the value of attribute password.



24
25
26
# File 'lib/admin_api/auth_configuration.rb', line 24

def password
  @password
end

#usernameObject

Returns the value of attribute username.



25
26
27
# File 'lib/admin_api/auth_configuration.rb', line 25

def username
  @username
end

Class Method Details

.defaultObject



48
49
50
# File 'lib/admin_api/auth_configuration.rb', line 48

def self.default
  @@default ||= AuthConfiguration.new
end

Instance Method Details

#auth_urlObject



56
57
58
59
# File 'lib/admin_api/auth_configuration.rb', line 56

def auth_url
  url = "#{scheme}://#{[@config.host, auth_uri].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '')
  URI.encode(url)
end

#client_token_auth_urlObject



61
62
63
64
# File 'lib/admin_api/auth_configuration.rb', line 61

def client_token_auth_url
  url = "#{scheme}://#{[@config.host, client_token_auth_uri].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '')
  URI.encode(url)
end

#configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



52
53
54
# File 'lib/admin_api/auth_configuration.rb', line 52

def configure
  yield(self) if block_given?
end

#create_client_credential(client_id, client_secret) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/admin_api/auth_configuration.rb', line 100

def create_client_credential(client_id, client_secret)
  basic_cred = 'Basic ' + ["#{client_id}:#{client_secret}"].pack('m').delete("\r\n")
  # Request Params
  params = {}
  params[@grant_type_key] = @client_credentials
  # Header parameters
  header_params = {}
  header_params['Accept'] = '*/*'
  header_params['Content-Type'] = 'application/json'
  header_params[@authorization] = basic_cred
  response = Typhoeus::Request.new(
      auth_url,
      :method => :post,
      :headers => header_params,
      :params => params
  ).run

  if @debugging
    @logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
  end
  body = JSON.parse(response.body)
  unless response.success?
    if response.timed_out?
      raise ApiError.new('Connection timed out')
    elsif response.code == 0
      # Errors from libcurl will be made visible here
      raise ApiError.new(:code => 0,
                         :message => response.return_message)
    else
      raise ApiError.new(:code => response.code,
                         :response_headers => response.headers,
                         :response_body => response.body), response.body
    end
  end
  @config.access_token = body['access_token']
end

#create_client_credential_return(client_id, client_secret) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/admin_api/auth_configuration.rb', line 137

def create_client_credential_return(client_id, client_secret)
  @logger.debug auth_url
  basic_cred = 'Basic ' + ["#{client_id}:#{client_secret}"].pack('m').delete("\r\n")
  # Request Params
  params = {}
  params[@grant_type_key] = @client_credentials
  # Header parameters
  header_params = {}
  header_params['Accept'] = '*/*'
  header_params['Content-Type'] = 'application/json'
  header_params[@authorization] = basic_cred
  response = Typhoeus::Request.new(
      auth_url,
      :method => :post,
      :headers => header_params,
      :params => params
  ).run

 
  if @debugging
    @logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
  end
  body = JSON.parse(response.body)
  unless response.success?
    if response.timed_out?
      raise ApiError.new('Connection timed out')
    elsif response.code == 0
      # Errors from libcurl will be made visible here
      raise ApiError.new(:code => 0,
                         :message => response.return_message)
    else
      raise ApiError.new(:code => response.code,
                         :response_headers => response.headers,
                         :response_body => response.body), response.body
    end
  end
  return body['access_token']
end

#create_client_token_credential(client_id, client_secret, client_token) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/admin_api/auth_configuration.rb', line 66

def create_client_token_credential(client_id, client_secret, client_token)
  self.create_client_credential(client_id, client_secret)
  access_token = @config.access_token
  @config.access_token = nil
  header_params = {}
  header_params[@authorization] = @bearer + access_token;
  header_params[@client_token] = @bearer + client_token;
  response = Typhoeus::Request.new(
      client_token_auth_url,
      :method => :post,
      :headers => header_params,
      :params => nil
  ).run

  if @debugging
    @logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
  end
  body = JSON.parse(response.body)
  unless response.success?
    if response.timed_out?
      raise ApiError.new('Connection timed out')
    elsif response.code == 0
      # Errors from libcurl will be made visible here
      raise ApiError.new(:code => 0,
                         :message => response.return_message)
    else
      raise ApiError.new(:code => response.code,
                         :response_headers => response.headers,
                         :response_body => response.body), response.body
    end
  end
  @config.access_token = body['access_token']
end

#create_password_credential(client_id, client_secret, username, password) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/admin_api/auth_configuration.rb', line 180

def create_password_credential(client_id, client_secret, username, password)
  basic_cred = 'Basic ' + ["#{client_id}:#{client_secret}"].pack('m').delete("\r\n")
  # Request Params
  params = {}
  params[@grant_type_key] = @password
  params[@username] = username
  params[@password] = password
  # header parameters
  header_params = {}
  header_params['Accept'] = '*/*'
  header_params['Content-Type'] = 'application/json'
  header_params[@authorization] = basic_cred
  response = Typhoeus::Request.new(
      auth_url,
      :method => :post,
      :headers => header_params,
      :params => params
  ).run
  if @debugging
    @logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
  end
  body = JSON.parse(response.body)
  unless response.success?
    if response.timed_out?
      raise ApiError.new('Connection timed out')
    elsif response.code == 0
      # Errors from libcurl will be made visible here
      raise ApiError.new(:code => 0,
                         :message => response.return_message)
    else
      raise ApiError.new(:code => response.code,
                         :response_headers => response.headers,
                         :response_body => response.body), body
    end
  end
  @config.access_token = body['access_token']
end

#create_password_credential_return(client_id, client_secret, username, password) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/admin_api/auth_configuration.rb', line 218

def create_password_credential_return(client_id, client_secret, username, password)
  basic_cred = 'Basic ' + ["#{client_id}:#{client_secret}"].pack('m').delete("\r\n")
  # Request Params
  params = {}
  params[@grant_type_key] = @password
  params[@username] = username
  params[@password] = password
  # header parameters
  header_params = {}
  header_params['Accept'] = '*/*'
  header_params['Content-Type'] = 'application/json'
  header_params[@authorization] = basic_cred
  response = Typhoeus::Request.new(
      auth_url,
      :method => :post,
      :headers => header_params,
      :params => params
  ).run
  if @debugging
    @logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
  end
  body = JSON.parse(response.body)
  unless response.success?
    if response.timed_out?
      raise ApiError.new('Connection timed out')
    elsif response.code == 0
      # Errors from libcurl will be made visible here
      raise ApiError.new(:code => 0,
                         :message => response.return_message)
    else
      raise ApiError.new(:code => response.code,
                         :response_headers => response.headers,
                         :response_body => response.body), body
    end
  end
  return body['access_token']
end

#set_access_token(token) ⇒ Object



176
177
178
# File 'lib/admin_api/auth_configuration.rb', line 176

def set_access_token(token)
  @config.access_token = token
end