Module: BWAPI::Client::OAuth

Included in:
BWAPI::Client
Defined in:
lib/bwapi/client/oauth.rb

Overview

OAuth module for oauth/token endpoint

Instance Method Summary collapse

Instance Method Details

#determine_grant_typeString

Determines grant-type used for client

Returns:

  • (String)

    relevant grant type for client



54
55
56
57
# File 'lib/bwapi/client/oauth.rb', line 54

def determine_grant_type
  return grant_type unless grant_type.nil?
  api_client? ? 'api-password' : 'password'
end

#oauth_refresh_token(opts = {}) ⇒ Object Also known as: refresh

Refresh a authenticated users oauth_token

Parameters:

  • opts (Hash) (defaults to: {})

    options hash of parameters

Options Hash (opts):

  • username (String)

    User name of user

  • password (String)

    Password of the user

  • grant_type (String)

    Grant type of user

  • refresh_token (String)

    Users refresh token

  • client_id (String)

    Client id

  • force_urlencoded (String)

    Force urlencoded



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bwapi/client/oauth.rb', line 37

def oauth_refresh_token(opts = {})
  opts = {
    username: username,
    password: password,
    refresh_token: refresh_token,
    grant_type: 'refresh_token',
    client_id: client_id,
    force_urlencoded: true
  }.merge opts

  oauth_request opts
end

#oauth_request(opts = {}) ⇒ Object

Sends a oauth request

Parameters:

  • opts (Hash) (defaults to: {})

    options hash of parameters



62
63
64
65
66
67
68
# File 'lib/bwapi/client/oauth.rb', line 62

def oauth_request(opts = {})
  response = post('oauth/token', opts)
  self.access_token = response['access_token']
  self.access_token_expiry = Time.at(Time.now.to_i + response['expires_in']).iso8601
  self.refresh_token = response['refresh_token'] if application_client?
  response
end

#oauth_token(opts = {}) ⇒ Object Also known as: login

Authenticate a user

Parameters:

  • opts (Hash) (defaults to: {})

    options hash of parameters

Options Hash (opts):

  • username (String)

    User name of user

  • password (String)

    Password of the user

  • grant_type (String)

    Grant type of user

  • client_secret (String)

    Client secret

  • client_id (String)

    Client id

  • force_urlencoded (String)

    Force urlencoded



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/bwapi/client/oauth.rb', line 14

def oauth_token(opts = {})
  opts = {
    username: username,
    password: password,
    grant_type: determine_grant_type,
    client_secret: client_secret,
    client_id: client_id,
    force_urlencoded: true
  }.merge opts

  oauth_request opts
end