Class: IntegrationApi::AuthConfiguration

Inherits:
Configuration show all
Defined in:
lib/integration_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, #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/integration_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/integration_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/integration_api/auth_configuration.rb', line 27

def auth_uri
  @auth_uri
end

#authorizationObject

Returns the value of attribute authorization.



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

def authorization
  @authorization
end

#base_urlObject

Returns the value of attribute base_url.



31
32
33
# File 'lib/integration_api/auth_configuration.rb', line 31

def base_url
  @base_url
end

#bearerObject

Returns the value of attribute bearer.



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

def bearer
  @bearer
end

#client_credentialsObject

Returns the value of attribute client_credentials.



23
24
25
# File 'lib/integration_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/integration_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/integration_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/integration_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/integration_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/integration_api/auth_configuration.rb', line 24

def password
  @password
end

#usernameObject

Returns the value of attribute username.



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

def username
  @username
end

Class Method Details

.defaultObject



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

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

Instance Method Details

#auth_url(host) ⇒ Object



60
61
62
63
# File 'lib/integration_api/auth_configuration.rb', line 60

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

#client_token_auth_url(host) ⇒ Object



65
66
67
68
# File 'lib/integration_api/auth_configuration.rb', line 65

def client_token_auth_url(host)
  url = "#{scheme}://#{[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/integration_api/auth_configuration.rb', line 52

def configure
  yield(self) if block_given?
end

#create_client_credential(client_id, client_secret) ⇒ Object



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
136
137
# File 'lib/integration_api/auth_configuration.rb', line 102

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(@config.host),
      :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_token_credential(client_id, client_secret, client_token) ⇒ Object



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
99
100
# File 'lib/integration_api/auth_configuration.rb', line 70

def create_client_token_credential(client_id, client_secret, client_token)
  basic_cred = 'Basic ' + ["#{client_id}:#{client_secret}"].pack('m').delete("\r\n")
  header_params = {}
  header_params[@authorization] = basic_cred;
  header_params[@client_token] = @bearer + client_token;
  response = Typhoeus::Request.new(
      client_token_auth_url(@config.host),
      :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



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
175
176
177
178
179
# File 'lib/integration_api/auth_configuration.rb', line 143

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(@config.host),
      :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

#set_access_token(token) ⇒ Object



139
140
141
# File 'lib/integration_api/auth_configuration.rb', line 139

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

#set_base_url(base_url) ⇒ Object



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

def set_base_url(base_url)
  @config.host = base_url
end