Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6409213

Browse files
author
Knut Eirik Leira Hjelle
committed
Added support for making explicit grant requests, added support for client credential grant, made it easier to re-use some of the code.
1 parent 175de0a commit 6409213

File tree

1 file changed

+109
-55
lines changed

1 file changed

+109
-55
lines changed

src/client/OAuth2BaseClient.php

Lines changed: 109 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -338,15 +338,49 @@ public function getSession()
338338
return $this->getVariable('_session');
339339
}
340340

341-
public function refreshToken($token)
341+
public function makeAuthorizationCodeRequest($auth_code)
342342
{
343-
$access_token = $this->getAccessTokenFromRefreshToken($token);
344-
$session = $this->getSessionObject($access_token);
343+
$access_token = $this->getAccessTokenFromAuthorizationCode($auth_code);
344+
return $this->validateAndSaveAccessToken($access_token);
345+
}
346+
347+
public function makePasswordRequest($username, $password)
348+
{
349+
$access_token = $this->getAccessTokenFromPassword($username, $password);
350+
return $this->validateAndSaveAccessToken($access_token);
351+
}
352+
353+
public function makeRefreshTokenRequest($refresh_token)
354+
{
355+
$access_token = $this->getAccessTokenFromRefreshToken($refresh_token);
356+
return $this->validateAndSaveAccessToken($access_token);
357+
}
358+
359+
public function makeClientCredentialsRequest()
360+
{
361+
$access_token = $this->getAccessTokenFromClientCredentials();
362+
return $this->validateAndSaveAccessToken($access_token);
363+
}
364+
365+
public function makeImplicitRequest()
366+
{
367+
// not implemented
368+
}
369+
370+
public function validateAndSaveAccessToken($token)
371+
{
372+
$session = $this->getSessionObject($token);
345373
$session = $this->validateSessionObject($session);
346374
$this->setSession($session, true);
347375
return $session;
348376
}
349377

378+
public function refreshToken($refresh_token)
379+
{
380+
$access_token = $this->getAccessTokenFromRefreshToken($refresh_token);
381+
return $this->validateAndSaveAccessToken($access_token);
382+
}
383+
350384
private function hasPersistenceSupport()
351385
{
352386
$isPersistent = ($this instanceof IOAuth2Persistent);
@@ -368,6 +402,39 @@ public function getAccessToken()
368402
return isset($session['access_token']) ? $session['access_token'] : NULL;
369403
}
370404

405+
private function makeTokenRequest($params)
406+
{
407+
if ($this->getVariable('access_token_uri') && $this->getVariable('client_id') && $this->getVariable('client_secret'))
408+
{
409+
return json_decode($this->makeRequest(
410+
$this->getVariable('access_token_uri'),
411+
'POST',
412+
$params
413+
), TRUE);
414+
}
415+
return NULL;
416+
}
417+
418+
/**
419+
* Get access token from OAuth2.0 token endpoint with client credentials.
420+
*
421+
* This function will only be activated if both access token URI, client
422+
* identifier and client secret are setup correctly.
423+
*
424+
* @return
425+
* A valid OAuth2.0 JSON decoded access token in associative array, and
426+
* NULL if not enough parameters or JSON decode failed.
427+
*/
428+
private function getAccessTokenFromClientCredentials()
429+
{
430+
$params = array(
431+
'grant_type' => 'client_credentials',
432+
'client_id' => $this->getVariable('client_id'),
433+
'client_secret' => $this->getVariable('client_secret'),
434+
);
435+
return $this->makeTokenRequest($params);
436+
}
437+
371438
/**
372439
* Get access token from OAuth2.0 token endpoint with authorization code.
373440
*
@@ -384,40 +451,40 @@ public function getAccessToken()
384451
*/
385452
private function getAccessTokenFromAuthorizationCode($code)
386453
{
387-
if ($this->getVariable('access_token_uri') && $this->getVariable('client_id') && $this->getVariable('client_secret'))
388-
{
389-
return json_decode($this->makeRequest(
390-
$this->getVariable('access_token_uri'),
391-
'POST',
392-
array(
393-
'grant_type' => 'authorization_code',
394-
'client_id' => $this->getVariable('client_id'),
395-
'client_secret' => $this->getVariable('client_secret'),
396-
'code' => $code,
397-
'redirect_uri' => $this->getCurrentUri()
398-
)
399-
), TRUE);
400-
}
401-
return NULL;
454+
$params = array(
455+
'grant_type' => 'authorization_code',
456+
'client_id' => $this->getVariable('client_id'),
457+
'client_secret' => $this->getVariable('client_secret'),
458+
'code' => $code,
459+
'redirect_uri' => $this->getCurrentUri()
460+
);
461+
return $this->makeTokenRequest($params);
402462
}
403463

464+
/**
465+
* Get access token from OAuth2.0 token endpoint with refresh token.
466+
*
467+
* This function will only be activated if both access token URI, client
468+
* identifier and client secret are setup correctly.
469+
*
470+
* @param $refresh_token
471+
* Refresh token issued by authorization server's authorization
472+
* endpoint.
473+
*
474+
* @return
475+
* A valid OAuth2.0 JSON decoded access token in associative array, and
476+
* NULL if not enough parameters or JSON decode failed.
477+
*/
404478
private function getAccessTokenFromRefreshToken($refresh_token)
405479
{
406-
if ($this->getVariable('access_token_uri') && $this->getVariable('client_id') && $this->getVariable('client_secret'))
407-
{
408-
return json_decode($this->makeRequest(
409-
$this->getVariable('access_token_uri'),
410-
'POST',
411-
array(
412-
'grant_type' => 'refresh_token',
413-
'client_id' => $this->getVariable('client_id'),
414-
'client_secret' => $this->getVariable('client_secret'),
415-
'refresh_token' => $refresh_token,
416-
'redirect_uri' => $this->getCurrentUri()
417-
)
418-
), TRUE);
419-
}
420-
return NULL;
480+
$params = array(
481+
'grant_type' => 'refresh_token',
482+
'client_id' => $this->getVariable('client_id'),
483+
'client_secret' => $this->getVariable('client_secret'),
484+
'refresh_token' => $refresh_token,
485+
'redirect_uri' => $this->getCurrentUri()
486+
);
487+
return $this->makeTokenRequest($params);
421488
}
422489

423490
/**
@@ -438,21 +505,14 @@ private function getAccessTokenFromRefreshToken($refresh_token)
438505
*/
439506
private function getAccessTokenFromPassword($username, $password)
440507
{
441-
if ($this->getVariable('access_token_uri') && $this->getVariable('client_id') && $this->getVariable('client_secret'))
442-
{
443-
return json_decode($this->makeRequest(
444-
$this->getVariable('access_token_uri'),
445-
'POST',
446-
array(
447-
'grant_type' => 'password',
448-
'client_id' => $this->getVariable('client_id'),
449-
'client_secret' => $this->getVariable('client_secret'),
450-
'username' => $username,
451-
'password' => $password
452-
)
453-
), TRUE);
454-
}
455-
return NULL;
508+
$params = array(
509+
'grant_type' => 'password',
510+
'client_id' => $this->getVariable('client_id'),
511+
'client_secret' => $this->getVariable('client_secret'),
512+
'username' => $username,
513+
'password' => $password
514+
);
515+
return $this->makeTokenRequest($params);
456516
}
457517

458518
/**
@@ -504,6 +564,7 @@ protected function makeOAuth2Request($path, $method = 'GET', $params = array())
504564
*/
505565
protected function makeRequest($path, $method = 'GET', $params = array(), $ch = NULL)
506566
{
567+
Yii::log("Making request to $path through $method with params ".var_export($params, true));
507568
if (!$ch)
508569
{
509570
$ch = curl_init();
@@ -547,13 +608,6 @@ protected function makeRequest($path, $method = 'GET', $params = array(), $ch =
547608
curl_setopt_array($ch, $opts);
548609
$result = curl_exec($ch);
549610

550-
if (curl_errno($ch) == 60)
551-
{ // CURLE_SSL_CACERT
552-
error_log('Invalid or no certificate authority found, using bundled information');
553-
curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/fb_ca_chain_bundle.crt');
554-
$result = curl_exec($ch);
555-
}
556-
557611
if ($result === FALSE)
558612
{
559613
$e = new OAuth2Exception(array('code' => curl_errno($ch),

0 commit comments

Comments
 (0)