Module: SyncAttrWithAuth0::Auth0

Defined in:
lib/sync_attr_with_auth0/auth0.rb

Class Method Summary collapse

Class Method Details

.get_access_tokenObject



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/sync_attr_with_auth0/auth0.rb', line 4

def self.get_access_token
  payload = {
    "client_id" =>        ENV['AUTH0_CLIENT_ID'],
    "client_secret" =>    ENV['AUTH0_CLIENT_SECRET'],
    "grant_type" =>       "client_credentials"
  }

  response = SyncAttrWithAuth0::Auth0.make_request(nil, 'post', '/oauth/token', payload)

  response = JSON.parse( response.to_s ) unless response.nil? or response.to_s.empty?

  response['access_token']
end

.make_request(access_token, method, path, payload = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sync_attr_with_auth0/auth0.rb', line 18

def self.make_request(access_token, method, path, payload=nil)
  args = [method, "https://#{ENV['AUTH0_DOMAIN']}#{path}"]

  # The post body wedges in between the request url
  # and the request headers for POST and PUT methods
  args << payload if payload

  if access_token
    args << { content_type: :json, authorization: "Bearer #{access_token}", accept: "application/json" }

  else
    args << { content_type: :json, accept: "application/json" }

  end

  # Handle variable length arg lists
  _response = RestClient.send(*args)
end