Class: NoyoAccounts::Client::Services::AuthService

Inherits:
NoyoApi::Client::Services::BaseService show all
Includes:
HTTParty, NoyoApi::Client::UserAgent
Defined in:
lib/noyo_accounts/client/services/auth_service.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NoyoApi::Client::UserAgent

included

Methods inherited from NoyoApi::Client::Services::BaseService

resource, resource_path, root_path

Constructor Details

#initializeAuthService

Returns a new instance of AuthService.



11
12
13
14
# File 'lib/noyo_accounts/client/services/auth_service.rb', line 11

def initialize
  # Get the latest config value, in case it changed
  self.class.base_uri(NoyoApi::Api.config.accounts_base_uri)
end

Class Method Details

.authorize_pathObject



16
17
18
# File 'lib/noyo_accounts/client/services/auth_service.rb', line 16

def self.authorize_path
  '/auth/public/token'
end

Instance Method Details

#authorize(client_id, client_secret) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/noyo_accounts/client/services/auth_service.rb', line 20

def authorize(client_id, client_secret)
  auth = {
    username: client_id,
    password: client_secret,
  }
  body = {
    grant_type: 'client_credentials',
  }
  options = {
    body: body.to_json,
    basic_auth: auth,
    headers: {
      'Content-Type' => 'application/json',
      'Accept' => 'application/json',
      'User-Agent' => self.class.user_agent,
    },
  }
  result = self.class.post(self.class.authorize_path, options)
  raise 'Bad credentials.' if result.code == 401

  parse_httparty_response(result)&.[]('access_token')
end