Module: Trubl::OAuth

Included in:
Client
Defined in:
lib/trubl/oauth.rb

Instance Method Summary collapse

Instance Method Details

#client_authObject

implements client_credentials access_token retrieval



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/trubl/oauth.rb', line 8

def client_auth()
  url = URI.join(@auth_site, @token_url).to_s
  body = {
    client_id:     @client_id,
    client_secret: @client_secret,
    grant_type:    "client_credentials"
  }
  
  Trubl.logger.info("Trubl::OAuth   post-ing #{url} with params #{{body: body, headers: headers}}")
  response = HTTParty.send(:post, url, body: body, headers: headers)
  Trubl.logger.debug("Trubl::OAuth   #{url} response: #{response.code} #{response.body}")

  if response.code == 200 and JSON.parse(response.body)["access_token"].present?
    @access_token = JSON.parse(response.body)["access_token"]
  else
    raise "Client failed to get an auth token, response was: " + response.body
  end
end

#password_auth(opts = {}) ⇒ Object Also known as: user_auth

ToDo: add some param checking logic



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/trubl/oauth.rb', line 28

def password_auth(opts={})
  url = URI.join(@auth_site, @token_url).to_s
  body = {
    client_id:     @client_id,
    client_secret: @client_secret,
    email:         @email,
    password:      @password,
    # scope:       "read write share",
    scope:         "read write share update_auth",
    grant_type:    "password"
  }.merge(opts)

  Trubl.logger.info("Trubl::OAuth   post-ing #{url} with params #{{body: body, headers: headers}}")
  response = HTTParty.send(:post, url, body: body, headers: headers)
  Trubl.logger.debug("Trubl::OAuth   #{url} response: #{response.code} #{response.body}")

  if response.code == 200 and JSON.parse(response.body)["access_token"].present?
    @access_token = JSON.parse(response.body)["access_token"]
  else
    raise "Client failed to get an auth token, response was: " + response.body
  end
end