2013-03-20 15:37:07 +04:00
|
|
|
<?php
|
2013-01-29 20:06:24 +04:00
|
|
|
|
2013-05-28 14:00:24 +04:00
|
|
|
namespace League\OAuth2\Client\Provider;
|
2013-03-20 15:37:07 +04:00
|
|
|
|
2014-04-28 22:00:26 +04:00
|
|
|
use League\OAuth2\Client\Entity\User;
|
|
|
|
|
2014-04-28 19:44:39 +04:00
|
|
|
class Google extends AbstractProvider
|
2013-01-29 20:06:24 +04:00
|
|
|
{
|
2014-02-02 02:59:40 +04:00
|
|
|
public $scopeSeparator = ' ';
|
2013-01-29 20:06:24 +04:00
|
|
|
|
2014-11-09 00:30:40 +03:00
|
|
|
public $scopes = [
|
2014-12-18 06:15:34 +03:00
|
|
|
'profile',
|
|
|
|
'email',
|
2014-11-09 00:30:40 +03:00
|
|
|
];
|
2013-01-29 20:06:24 +04:00
|
|
|
|
2014-12-15 20:57:30 +03:00
|
|
|
/**
|
|
|
|
* @var string If set, this will be sent to google as the "hd" parameter.
|
|
|
|
* @link https://developers.google.com/accounts/docs/OAuth2Login#hd-param
|
|
|
|
*/
|
2014-06-30 21:30:31 +04:00
|
|
|
public $hostedDomain = '';
|
|
|
|
|
|
|
|
public function setHostedDomain($hd)
|
|
|
|
{
|
|
|
|
$this->hostedDomain = $hd;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getHostedDomain()
|
|
|
|
{
|
|
|
|
return $this->hostedDomain;
|
|
|
|
}
|
|
|
|
|
2015-01-20 15:49:06 +03:00
|
|
|
/**
|
|
|
|
* @var string If set, this will be sent to google as the "access_type" parameter.
|
|
|
|
* @link https://developers.google.com/accounts/docs/OAuth2WebServer#offline
|
|
|
|
*/
|
2015-01-20 15:31:49 +03:00
|
|
|
public $accessType = '';
|
|
|
|
|
|
|
|
public function setAccessType($accessType)
|
|
|
|
{
|
|
|
|
$this->accessType = $accessType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAccessType()
|
|
|
|
{
|
|
|
|
return $this->accessType;
|
|
|
|
}
|
|
|
|
|
2013-03-20 15:37:07 +04:00
|
|
|
public function urlAuthorize()
|
2013-01-29 20:08:47 +04:00
|
|
|
{
|
|
|
|
return 'https://accounts.google.com/o/oauth2/auth';
|
|
|
|
}
|
2013-01-29 20:06:24 +04:00
|
|
|
|
2013-03-20 15:37:07 +04:00
|
|
|
public function urlAccessToken()
|
2013-01-29 20:08:47 +04:00
|
|
|
{
|
|
|
|
return 'https://accounts.google.com/o/oauth2/token';
|
|
|
|
}
|
2013-01-29 20:06:24 +04:00
|
|
|
|
2013-08-18 15:21:48 +04:00
|
|
|
public function urlUserDetails(\League\OAuth2\Client\Token\AccessToken $token)
|
2013-01-29 20:08:47 +04:00
|
|
|
{
|
2014-12-18 06:27:33 +03:00
|
|
|
return
|
2014-12-22 08:02:09 +03:00
|
|
|
'https://www.googleapis.com/plus/v1/people/me?'.
|
2014-12-31 01:24:54 +03:00
|
|
|
'fields=id%2Cname(familyName%2CgivenName)%2CdisplayName%2C'.
|
2014-12-22 08:02:09 +03:00
|
|
|
'emails%2Fvalue%2Cimage%2Furl&alt=json&access_token='.$token;
|
2013-01-29 20:08:47 +04:00
|
|
|
}
|
|
|
|
|
2013-08-18 15:21:48 +04:00
|
|
|
public function userDetails($response, \League\OAuth2\Client\Token\AccessToken $token)
|
2013-01-29 20:08:47 +04:00
|
|
|
{
|
2013-03-25 15:00:49 +04:00
|
|
|
$response = (array) $response;
|
2014-04-28 22:00:26 +04:00
|
|
|
|
2014-11-09 00:30:40 +03:00
|
|
|
$user = new User();
|
2014-04-28 22:00:26 +04:00
|
|
|
|
2014-12-18 06:27:33 +03:00
|
|
|
$imageUrl = (isset($response['image']) &&
|
2014-12-22 08:02:09 +03:00
|
|
|
$response['image']->url) ? $response['image']->url : null;
|
2014-12-18 06:27:33 +03:00
|
|
|
$email =
|
2014-12-22 08:02:09 +03:00
|
|
|
(isset($response['emails']) &&
|
|
|
|
count($response['emails']) &&
|
|
|
|
$response['emails'][0]->value)? $response['emails'][0]->value : null;
|
2014-04-28 22:00:26 +04:00
|
|
|
|
2014-11-09 00:30:40 +03:00
|
|
|
$user->exchangeArray([
|
2014-04-28 22:00:26 +04:00
|
|
|
'uid' => $response['id'],
|
2014-12-18 06:15:34 +03:00
|
|
|
'name' => $response['displayName'],
|
|
|
|
'firstname' => $response['name']->givenName,
|
|
|
|
'lastName' => $response['name']->familyName,
|
|
|
|
'email' => $email,
|
2014-04-28 22:00:26 +04:00
|
|
|
'imageUrl' => $imageUrl,
|
2014-11-09 00:30:40 +03:00
|
|
|
]);
|
2014-04-24 08:34:10 +04:00
|
|
|
|
2013-03-20 15:37:07 +04:00
|
|
|
return $user;
|
2013-01-29 20:08:47 +04:00
|
|
|
}
|
2013-09-14 06:38:58 +04:00
|
|
|
|
|
|
|
public function userUid($response, \League\OAuth2\Client\Token\AccessToken $token)
|
|
|
|
{
|
|
|
|
return $response->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function userEmail($response, \League\OAuth2\Client\Token\AccessToken $token)
|
|
|
|
{
|
2014-12-18 06:27:33 +03:00
|
|
|
return ($response->emails &&
|
2014-12-22 08:02:09 +03:00
|
|
|
count($response->emails) &&
|
|
|
|
$response->emails[0]->value) ? $response->emails[0]->value : null;
|
2013-09-14 06:38:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function userScreenName($response, \League\OAuth2\Client\Token\AccessToken $token)
|
|
|
|
{
|
2014-12-18 06:15:34 +03:00
|
|
|
return [$response->name->givenName, $response->name->familyName];
|
2013-09-14 06:38:58 +04:00
|
|
|
}
|
2014-06-30 21:30:31 +04:00
|
|
|
|
|
|
|
public function getAuthorizationUrl($options = array())
|
|
|
|
{
|
|
|
|
$url = parent::getAuthorizationUrl($options);
|
|
|
|
|
|
|
|
if (!empty($this->hostedDomain)) {
|
|
|
|
$url .= '&' . $this->httpBuildQuery(['hd' => $this->hostedDomain]);
|
|
|
|
}
|
|
|
|
|
2015-01-20 15:31:49 +03:00
|
|
|
if (!empty($this->accessType)) {
|
|
|
|
$url .= '&' . $this->httpBuildQuery(['access_type'=> $this->accessType]);
|
|
|
|
}
|
|
|
|
|
2014-06-30 21:30:31 +04:00
|
|
|
return $url;
|
|
|
|
}
|
2013-01-29 20:06:24 +04:00
|
|
|
}
|