Resolve Scope issues which results in auth failure
This commit is contained in:
parent
7c8b786228
commit
2116208ab0
|
|
@ -27,8 +27,7 @@
|
|||
"phpunit/phpunit": "4.7.*"
|
||||
},
|
||||
"suggest": {
|
||||
"league/oauth2-client": "Needed for XOAUTH2 authentication",
|
||||
"league/oauth2-google": "Needed for Gmail XOAUTH2"
|
||||
"league/oauth2-client": "Needed for XOAUTH2 authentication"
|
||||
},
|
||||
"autoload": {
|
||||
"classmap": [
|
||||
|
|
|
|||
|
|
@ -12,9 +12,21 @@
|
|||
* This script requires PHP 5.4 or later
|
||||
* PHP Version 5.4
|
||||
*/
|
||||
/**
|
||||
* Added a new class for getting the Refresh Token with right scopes
|
||||
* as the OAuth-Client for Google from GitHub didnot provide setting the
|
||||
* scope out of the box
|
||||
*/
|
||||
|
||||
namespace League\OAuth2\Client\Provider;
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
|
||||
use League\OAuth2\Client\Token\AccessToken;
|
||||
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
session_start();
|
||||
|
||||
//If this automatic URL doesn't work, set it yourself manually
|
||||
|
|
@ -25,14 +37,109 @@ $redirectUri = isset($_SERVER['HTTPS']) ? 'https://' : 'http://' . $_SERVER['HTT
|
|||
$clientId = 'RANDOMCHARS-----duv1n2.apps.googleusercontent.com';
|
||||
$clientSecret = 'RANDOMCHARS-----lGyjPcRtvP';
|
||||
|
||||
class Google extends AbstractProvider
|
||||
{
|
||||
use BearerAuthorizationTrait;
|
||||
|
||||
const ACCESS_TOKEN_RESOURCE_OWNER_ID = 'id';
|
||||
|
||||
/**
|
||||
* @var string If set, this will be sent to google as the "access_type" parameter.
|
||||
* @link https://developers.google.com/accounts/docs/OAuth2WebServer#offline
|
||||
*/
|
||||
protected $accessType;
|
||||
|
||||
/**
|
||||
* @var string If set, this will be sent to google as the "hd" parameter.
|
||||
* @link https://developers.google.com/accounts/docs/OAuth2Login#hd-param
|
||||
*/
|
||||
protected $hostedDomain;
|
||||
|
||||
/**
|
||||
* @var string If set, this will be sent to google as the "scope" parameter.
|
||||
* @link https://developers.google.com/gmail/api/auth/scopes
|
||||
*/
|
||||
protected $scope;
|
||||
|
||||
public function getBaseAuthorizationUrl()
|
||||
{
|
||||
return 'https://accounts.google.com/o/oauth2/auth';
|
||||
}
|
||||
|
||||
public function getBaseAccessTokenUrl(array $params)
|
||||
{
|
||||
return 'https://accounts.google.com/o/oauth2/token';
|
||||
}
|
||||
|
||||
public function getResourceOwnerDetailsUrl(AccessToken $token)
|
||||
{
|
||||
return ' ';
|
||||
}
|
||||
|
||||
protected function getAuthorizationParameters(array $options)
|
||||
{
|
||||
if (is_array($this->scope)) {
|
||||
$separator = $this->getScopeSeparator();
|
||||
$this->scope = implode($separator, $this->scope);
|
||||
}
|
||||
|
||||
$params = array_merge(
|
||||
parent::getAuthorizationParameters($options),
|
||||
array_filter([
|
||||
'hd' => $this->hostedDomain,
|
||||
'access_type' => $this->accessType,
|
||||
'scope' => $this->scope,
|
||||
// if the user is logged in with more than one account ask which one to use for the login!
|
||||
'authuser' => '-1'
|
||||
])
|
||||
);
|
||||
return $params;
|
||||
}
|
||||
|
||||
protected function getDefaultScopes()
|
||||
{
|
||||
return [
|
||||
'email',
|
||||
'openid',
|
||||
'profile',
|
||||
];
|
||||
}
|
||||
|
||||
protected function getScopeSeparator()
|
||||
{
|
||||
return ' ';
|
||||
}
|
||||
|
||||
protected function checkResponse(ResponseInterface $response, $data)
|
||||
{
|
||||
if (!empty($data['error'])) {
|
||||
$code = 0;
|
||||
$error = $data['error'];
|
||||
|
||||
if (is_array($error)) {
|
||||
$code = $error['code'];
|
||||
$error = $error['message'];
|
||||
}
|
||||
|
||||
throw new IdentityProviderException($error, $code, $data);
|
||||
}
|
||||
}
|
||||
|
||||
protected function createResourceOwner(array $response, AccessToken $token)
|
||||
{
|
||||
return new GoogleUser($response);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Set Redirect URI in Developer Console as [https/http]://<yourdomain>/<folder>/get_oauth_token.php
|
||||
$provider = new League\OAuth2\Client\Provider\Google(
|
||||
$provider = new Google(
|
||||
array(
|
||||
'clientId' => $clientId,
|
||||
'clientSecret' => $clientSecret,
|
||||
'redirectUri' => $redirectUri,
|
||||
'scopes' => array('https://mail.google.com/'),
|
||||
'accessType' => 'offline'
|
||||
'scope' => array('https://mail.google.com/'),
|
||||
'accessType' => 'offline'
|
||||
)
|
||||
);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue