Module: AuthTool::OAuth2

Defined in:
lib/auth_tool/oauth_2.rb

Class Method Summary collapse

Class Method Details

.call(client, http_verb = 'get', uri, params) ⇒ Hash

Makes an authenticated call to the API resource. Called by AuthTool::call.

Parameters:

  • client (AuthTool::Client)

    The client containing the API information.

  • uri (String)

    The API endpoint to hit.

  • params (Hash)

    Hash of additional parameters for the call.

Returns:

  • (Hash)

    The endpoint’s response.



51
52
53
54
55
56
57
58
# File 'lib/auth_tool/oauth_2.rb', line 51

def self.call(client,http_verb = 'get', uri, params)
  header = params.delete('header') if params.has_key? 'header'
  body = params.delete('body') if params.has_key? 'body'
  conn = Faraday.new(:params => params)
  options = {:method => http_verb, :header => header, :body => body, :uri => uri, :connection => conn}
  response = client.signet.fetch_protected_resource(options)
  return JSON.parse(response.body)
end

.receive(client, response) ⇒ Object

Handles OAuth 2.0 callback procedure Called by AuthTool::receive.

Parameters:

  • client (AuthTool::Client)

    The client containing the API information.

  • response (Hash)

    The response to the callback url (authentication token).



27
28
29
30
31
32
33
34
# File 'lib/auth_tool/oauth_2.rb', line 27

def self.receive(client, response)
  client.signet.code = response["code"] if response.has_key?("code")
  client.signet.code = response[:code] if response.has_key?(:code)
  credentials = {}
  credentials[:access] = (client.signet.fetch_access_token!)["access_token"]
  credentials[:refresh] = client.signet.refresh_token
  client.token = credentials
end

.redirect_url(client) ⇒ String

Returns redirect url for user authentication with the service

Parameters:

Returns:

  • (String)

    The url to redirect to.



13
14
15
16
# File 'lib/auth_tool/oauth_2.rb', line 13

def self.redirect_url client
  url = client.signet.authorization_uri
  return url
end