appwrite/src/Auth/OAuth/Google.php

144 lines
3.1 KiB
PHP
Raw Normal View History

2019-09-28 09:27:21 +00:00
<?php
namespace Auth\OAuth;
use Auth\OAuth;
2019-10-04 15:09:16 +00:00
// Reference Material
2019-10-13 18:06:20 +00:00
// https://developers.google.com/oauthplayground/
// https://developers.google.com/identity/protocols/OAuth2
// https://developers.google.com/identity/protocols/OAuth2WebServer
class Google extends OAuth
2019-09-28 09:27:21 +00:00
{
2019-10-13 18:06:20 +00:00
/**
* @var string
*/
protected $version = 'v4';
2020-01-18 15:27:48 +00:00
/**
* @var array
*/
2020-01-18 20:12:41 +00:00
protected $scopes = [
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile'
];
2020-01-18 15:27:48 +00:00
2019-09-28 09:27:21 +00:00
/**
* @var array
*/
protected $user = [];
/**
* @return string
*/
2019-09-29 22:03:22 +00:00
public function getName(): string
2019-09-28 09:27:21 +00:00
{
2019-10-13 18:06:20 +00:00
return 'google';
2019-09-28 09:27:21 +00:00
}
/**
* @return string
*/
2019-09-29 22:03:22 +00:00
public function getLoginURL(): string
2019-09-28 09:27:21 +00:00
{
2020-01-18 15:27:48 +00:00
return 'https://accounts.google.com/o/oauth2/v2/auth?'. http_build_query([
'client_id' => $this->appID,
'redirect_uri' => $this->callback,
'scope' => implode(' ', $this->getScopes()),
'state' => json_encode($this->state),
'response_type' => 'code'
]);
2019-09-28 09:27:21 +00:00
}
/**
* @param string $code
*
* @return string
*/
2019-09-29 22:03:22 +00:00
public function getAccessToken(string $code): string
2019-09-28 09:27:21 +00:00
{
2019-09-29 22:03:22 +00:00
$accessToken = $this->request(
'POST',
2020-01-18 15:27:48 +00:00
'https://www.googleapis.com/oauth2/'.$this->version.'/token?'.http_build_query([
'code' => $code,
'client_id' => $this->appID,
'client_secret' => $this->appSecret,
'redirect_uri' => $this->callback,
'scope' => null,
'grant_type' => 'authorization_code'
])
2019-09-28 09:27:21 +00:00
);
2019-09-29 22:03:22 +00:00
$accessToken = json_decode($accessToken, true);
2019-09-28 09:27:21 +00:00
if (isset($accessToken['access_token'])) {
return $accessToken['access_token'];
}
return '';
}
/**
* @param string $accessToken
*
* @return string
*/
2019-09-29 22:03:22 +00:00
public function getUserID(string $accessToken): string
2019-09-28 09:27:21 +00:00
{
$user = $this->getUser($accessToken);
if (isset($user['id'])) {
return $user['id'];
}
return '';
}
/**
* @param string $accessToken
*
* @return string
*/
2019-09-29 22:03:22 +00:00
public function getUserEmail(string $accessToken): string
2019-09-28 09:27:21 +00:00
{
$user = $this->getUser($accessToken);
if (isset($user['email'])) {
return $user['email'];
}
return '';
}
/**
* @param string $accessToken
*
* @return string
*/
2019-09-29 22:03:22 +00:00
public function getUserName(string $accessToken): string
2019-09-28 09:27:21 +00:00
{
$user = $this->getUser($accessToken);
if (isset($user['name'])) {
return $user['name'];
}
return '';
}
/**
* @param string $accessToken
*
* @return array
*/
2019-09-29 22:03:22 +00:00
protected function getUser(string $accessToken): array
2019-09-28 09:27:21 +00:00
{
if (empty($this->user)) {
2019-10-01 04:34:01 +00:00
$user = $this->request('GET', 'https://www.googleapis.com/oauth2/v2/userinfo?access_token='.urlencode($accessToken));
2019-09-28 09:27:21 +00:00
$this->user = json_decode($user, true);
}
2019-10-01 04:34:01 +00:00
2019-09-28 09:27:21 +00:00
return $this->user;
}
}