appwrite/src/Auth/OAuth/Google.php

133 lines
3 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';
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
{
2019-10-01 04:34:01 +00:00
return 'https://accounts.google.com/o/oauth2/v2/auth?'.
'client_id='.urlencode($this->appID).
'&redirect_uri='.urlencode($this->callback).
'&scope=https://www.googleapis.com/auth/userinfo.email+https://www.googleapis.com/auth/userinfo.profile'.
'&state='.urlencode(json_encode($this->state)).
2019-09-29 22:03:22 +00:00
'&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',
2019-10-01 04:34:01 +00:00
'https://www.googleapis.com/oauth2/'.$this->version.'/token?'.
'code='.urlencode($code).
'&client_id='.urlencode($this->appID).
'&client_secret='.urlencode($this->appSecret).
'&redirect_uri='.urlencode($this->callback).
'&scope='.
2019-09-29 22:03:22 +00:00
'&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;
}
}