Class: LeanTesting::OAuth2Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/Handler/Auth/OAuth2Handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(origin) ⇒ OAuth2Handler

Constructs an OAuth2Handler instance

Arguments: origin Client – Originating client reference



15
16
17
# File 'lib/Handler/Auth/OAuth2Handler.rb', line 15

def initialize(origin)
  @origin = origin
end

Instance Method Details

#exchangeAuthCode(clientID, clientSecret, grantType, code, redirectURI) ⇒ Object

Exceptions: SDKInvalidArgException if provided clientID param is not a string SDKInvalidArgException if provided clientSecret param is not a string SDKInvalidArgException if provided grantType param is not a string SDKInvalidArgException if provided code param is not a string SDKInvalidArgException if provided redirectURI param is not a string

Returns: String – returns obtained access token string



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/Handler/Auth/OAuth2Handler.rb', line 84

def exchangeAuthCode(clientID, clientSecret, grantType, code, redirectURI)
  if !clientID.is_a? String
    raise SDKInvalidArgException, '`clientID` must be a string'
  elsif !clientSecret.is_a? String
    raise SDKInvalidArgException, '`clientSecret` must be a string'
  elsif !grantType.is_a? String
    raise SDKInvalidArgException, '`grantType` must be a string'
  elsif !code.is_a? String
    raise SDKInvalidArgException, '`code` must be a string'
  elsif !redirectURI.is_a? String
    raise SDKInvalidArgException, '`redirectURI` must be a string'
  end

  params = {
    'grant_type'  => grantType,
    'client_id'   => clientID,
    'client_secret' => clientSecret,
    'redirect_uri'  => redirectURI,
    'code'      => code
  }

  req = APIRequest.new(
    @origin,
    '/login/oauth/access_token',
    'POST',
    {
      'base_uri' => 'https://app.leantesting.com',
      'params' => params
    }
  )

  resp = req.exec
  resp['access_token']
end

Function that generates link for user to follow in order to request authorization code

Arguments: clientID String – client ID given at application registration redirectURI String – URL to be redirected to after authorization scope String – (optional) comma-separated list of requested scopes (default: ‘read’) state String – (optional) random string for MITM attack prevention

Exceptions: SDKInvalidArgException if provided clientID param is not a string SDKInvalidArgException if provided redirectURI param is not a string SDKInvalidArgException if provided scope param is not a string SDKInvalidArgException if provided state param is not a string

Returns: String – returns URL to follow for authorization code request



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/Handler/Auth/OAuth2Handler.rb', line 37

def generateAuthLink(clientID, redirectURI, scope = 'read', state = nil)
  if !clientID.is_a? String
    raise SDKInvalidArgException, '`clientID` must be a string'
  elsif !redirectURI.is_a? String
    raise SDKInvalidArgException, '`redirectURI` must be a string'
  elsif !scope.is_a? String
    raise SDKInvalidArgException, '`scope` must be a string'
  elsif state && !state.is_a?(String)
    raise SDKInvalidArgException, '`state` must be a string'
  end

  baseURL = 'https://app.leantesting.com/login/oauth/authorize'

  params = {
    'client_id'   => clientID,
    'redirect_uri'  => redirectURI,
    'scope'     => scope
  }

  if state
    params['state'] = state
  end

  baseURL += '?' + Curl::postalize(params)
  baseURL
end