Class: AvalaraSdk::Auth::OAuthHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/avalara_sdk/auth/oauth_helper.rb

Class Method Summary collapse

Class Method Details

.get_access_token_for_device_flow(device_authorization_code, configuration) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/avalara_sdk/auth/oauth_helper.rb', line 31

def self.get_access_token_for_device_flow(device_authorization_code, configuration)
  configuration.populate_token_url

  data = { "client_id"=>"#{configuration.client_id}", "device_code"=>"#{device_authorization_code}", "grant_type"=>"urn:ietf:params:oauth:grant-type:device_code" }

  begin
    response = Faraday.post(configuration.token_url) do |req|
      req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
      req.headers['Accept'] = 'application/json'
      req.body = URI.encode_www_form(data)
    end
    json = JSON.parse(response.body)

    if (response.status > 199 && response.status < 300)
      TokenResponse.new(json['access_token'], json['token_type'], json['expires_in'], json['refresh_token'], response.status, nil)
    else
      TokenResponse.new(nil, nil, nil, nil, response.status, json['error'])
    end
  rescue Exception => e
    puts "Exception when calling token endpoint. Error: #{e.message}"
    raise "Exception when calling token endpoint. Error: #{e.message}"
  end
end

.initiate_device_authorization_flow(scopes, configuration) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/avalara_sdk/auth/oauth_helper.rb', line 7

def self.initiate_device_authorization_flow(scopes, configuration)
  configuration.populate_token_url

  data = { "client_id"=>"#{configuration.client_id}", "scope"=>"#{scopes}" }

  begin
    response = Faraday.post(configuration.device_authorization_url) do |req|
      req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
      req.headers['Accept'] = 'application/json'
      req.body = URI.encode_www_form(data)
    end
    json = JSON.parse(response.body)
    DeviceAuthorizationResponse.new(json['device_code'],
                                    json['user_code'],
                                    json['verification_uri'],
                                    json['verification_uri_complete'],
                                    json['expires_in'],
                                    json['interval'])
  rescue Exception => e
    puts "Exception when calling device authorization endpoint. Error: #{e.message}"
    raise "Exception when calling device authorization endpoint. Error: #{e.message}"
  end
end