appwrite/src/Appwrite/Auth/OAuth2/Google.php

145 lines
3.1 KiB
PHP
Raw Normal View History

2019-09-28 09:27:21 +00:00
<?php
namespace Appwrite\Auth\OAuth2;
2019-09-28 09:27:21 +00:00
use Appwrite\Auth\OAuth2;
2019-09-28 09:27:21 +00:00
2020-06-24 21:05: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
2020-02-16 11:41:03 +00:00
class Google extends OAuth2
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',
2020-05-20 20:51:21 +00:00
'https://www.googleapis.com/auth/userinfo.profile',
'openid'
2020-01-18 20:12:41 +00:00
];
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
{
return 'https://accounts.google.com/o/oauth2/v2/auth?'. \http_build_query([
2020-01-18 15:27:48 +00:00
'client_id' => $this->appID,
'redirect_uri' => $this->callback,
'scope' => \implode(' ', $this->getScopes()),
'state' => \json_encode($this->state),
2020-01-18 15:27:48 +00:00
'response_type' => 'code'
]);
2019-09-28 09:27:21 +00:00
}
/**
* @param string $code
*
2022-01-31 20:20:17 +00:00
* @return array
2019-09-28 09:27:21 +00:00
*/
2022-01-31 20:20:17 +00:00
public function getTokens(string $code): array
2019-09-28 09:27:21 +00:00
{
2022-01-31 20:39:14 +00:00
$result = $this->request(
'POST',
'https://oauth2.googleapis.com/token?'.\http_build_query([
'code' => $code,
'client_id' => $this->appID,
'client_secret' => $this->appSecret,
'redirect_uri' => $this->callback,
'scope' => null,
'grant_type' => 'authorization_code'
])
);
$result = \json_decode($result, true);
2019-09-28 09:27:21 +00:00
2022-01-31 20:20:17 +00:00
return [
2022-01-31 20:39:14 +00:00
'access' => $result['access_token'],
'refresh' => $result['refresh_token']
2022-01-31 20:20:17 +00:00
];
2019-09-28 09:27:21 +00:00
}
/**
* @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)) {
$user = $this->request('GET', 'https://www.googleapis.com/oauth2/v2/userinfo?access_token='.\urlencode($accessToken));
$this->user = \json_decode($user, true);
2019-09-28 09:27:21 +00:00
}
2019-10-01 04:34:01 +00:00
2019-09-28 09:27:21 +00:00
return $this->user;
}
}