Module: Bearcat::Client::OAuth2

Defined in:
lib/bearcat/client/o_auth2.rb

Instance Method Summary collapse

Instance Method Details

#auth_redirect_url(client_id, redirect_uri, opts = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/bearcat/client/o_auth2.rb', line 5

def auth_redirect_url(client_id, redirect_uri, opts = {})
  opts[:response_type] ||= 'code'

  fullpath('login/oauth2/auth')
  uri = URI.parse(fullpath('login/oauth2/auth'))
  query = [
    ['client_id', client_id],
    ['redirect_uri', redirect_uri],
    ['response_type', opts[:response_type]]
  ]
  query << ['state', opts[:state]] if opts[:state]
  query << ['scopes', opts[:scope]] if opts[:scope]
  uri.query = URI.encode_www_form(query)
  uri.to_s
end

#retrieve_token(client_id, redirect_url, client_secret, code, grant_type = 'authorization_code') ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bearcat/client/o_auth2.rb', line 21

def retrieve_token(client_id, redirect_url, client_secret, code, grant_type = 'authorization_code')
  token_params = {
    client_id: client_id,
    redirect_url: redirect_url,
    client_secret: client_secret,
    grant_type: grant_type
  }
  if grant_type == 'authorization_code'
    token_params[:code] = code
  elsif grant_type == 'refresh_token'
    token_params[:refresh_token] = code
  end
  body = post('/login/oauth2/token', token_params)
  config[:token] = body['access_token']
  set_connection(config)
  body
end