2019-05-09 18:54:39 +12:00
|
|
|
<?php
|
|
|
|
|
2020-03-25 06:56:32 +13:00
|
|
|
namespace Appwrite\Auth;
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2020-02-17 00:41:03 +13:00
|
|
|
abstract class OAuth2
|
2019-05-09 18:54:39 +12:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2022-05-13 03:56:20 +12:00
|
|
|
protected string $appID;
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2022-05-13 03:56:20 +12:00
|
|
|
protected string $appSecret;
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2022-05-13 03:56:20 +12:00
|
|
|
protected string $callback;
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
/**
|
2020-10-27 13:08:29 +13:00
|
|
|
* @var array
|
2019-05-09 18:54:39 +12:00
|
|
|
*/
|
2022-05-13 03:56:20 +12:00
|
|
|
protected array $state;
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2020-01-12 08:09:22 +13:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2022-05-13 03:56:20 +12:00
|
|
|
protected array $scopes;
|
2020-01-12 08:09:22 +13:00
|
|
|
|
2019-05-09 18:54:39 +12:00
|
|
|
/**
|
2020-02-17 00:41:03 +13:00
|
|
|
* OAuth2 constructor.
|
2019-05-09 18:54:39 +12:00
|
|
|
*
|
|
|
|
* @param string $appId
|
|
|
|
* @param string $appSecret
|
|
|
|
* @param string $callback
|
2019-09-07 05:04:26 +12:00
|
|
|
* @param array $state
|
2020-01-13 19:31:36 +13:00
|
|
|
* @param array $scopes
|
2019-05-09 18:54:39 +12:00
|
|
|
*/
|
2020-10-27 13:08:29 +13:00
|
|
|
public function __construct(string $appId, string $appSecret, string $callback, array $state = [], array $scopes = [])
|
2019-05-09 18:54:39 +12:00
|
|
|
{
|
2019-09-07 05:04:26 +12:00
|
|
|
$this->appID = $appId;
|
|
|
|
$this->appSecret = $appSecret;
|
|
|
|
$this->callback = $callback;
|
|
|
|
$this->state = $state;
|
2020-06-25 09:02:27 +12:00
|
|
|
foreach ($scopes as $scope) {
|
2020-01-13 19:31:36 +13:00
|
|
|
$this->addScope($scope);
|
|
|
|
}
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-04-28 07:08:32 +12:00
|
|
|
abstract public function getName(): string;
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-04-28 07:08:32 +12:00
|
|
|
abstract public function getLoginURL(): string;
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $code
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2022-02-01 09:20:17 +13:00
|
|
|
* @return array
|
2019-05-09 18:54:39 +12:00
|
|
|
*/
|
2022-04-28 07:08:32 +12:00
|
|
|
abstract protected function getTokens(string $code): array;
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2022-02-02 04:54:20 +13:00
|
|
|
/**
|
|
|
|
* @param string $refreshToken
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2022-04-28 07:08:32 +12:00
|
|
|
abstract public function refreshTokens(string $refreshToken): array;
|
2022-02-02 04:54:20 +13:00
|
|
|
|
2019-05-09 18:54:39 +12:00
|
|
|
/**
|
2022-05-13 03:56:20 +12:00
|
|
|
* @param string $accessToken
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return string
|
|
|
|
*/
|
2022-04-28 07:08:32 +12:00
|
|
|
abstract public function getUserEmail(string $accessToken): string;
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
/**
|
2022-04-28 07:14:09 +12:00
|
|
|
* Check if the OAuth email is verified
|
|
|
|
*
|
2022-05-13 03:56:20 +12:00
|
|
|
* @param string $accessToken
|
2022-04-28 07:08:32 +12:00
|
|
|
*
|
|
|
|
* @return bool
|
2019-05-09 18:54:39 +12:00
|
|
|
*/
|
2022-04-28 08:27:21 +12:00
|
|
|
abstract public function isEmailVerified(string $accessToken): bool;
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
/**
|
2022-05-13 03:56:20 +12:00
|
|
|
* @param string $accessToken
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return string
|
|
|
|
*/
|
2022-04-28 07:08:32 +12:00
|
|
|
abstract public function getUserName(string $accessToken): string;
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2020-01-12 08:29:22 +13:00
|
|
|
/**
|
|
|
|
* @param $scope
|
2020-06-25 09:05:16 +12:00
|
|
|
*
|
2020-01-14 03:13:08 +13:00
|
|
|
* @return $this
|
2020-01-12 08:29:22 +13:00
|
|
|
*/
|
2022-04-28 07:08:32 +12:00
|
|
|
protected function addScope(string $scope): OAuth2
|
2020-01-19 10:08:28 +13:00
|
|
|
{
|
2020-06-25 09:05:16 +12:00
|
|
|
// Add a scope to the scopes array if it isn't already present
|
2020-06-25 09:02:27 +12:00
|
|
|
if (!\in_array($scope, $this->scopes)) {
|
|
|
|
$this->scopes[] = $scope;
|
2020-01-12 08:29:22 +13:00
|
|
|
}
|
2022-05-13 03:56:20 +12:00
|
|
|
|
2020-01-14 03:12:09 +13:00
|
|
|
return $this;
|
2020-01-12 08:29:22 +13:00
|
|
|
}
|
|
|
|
|
2020-06-25 09:02:27 +12:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2022-04-28 07:08:32 +12:00
|
|
|
protected function getScopes(): array
|
2020-01-19 10:08:28 +13:00
|
|
|
{
|
2020-01-13 19:31:36 +13:00
|
|
|
return $this->scopes;
|
2020-01-12 08:29:22 +13:00
|
|
|
}
|
2019-10-08 08:39:01 +13:00
|
|
|
|
2022-02-01 23:42:11 +13:00
|
|
|
/**
|
|
|
|
* @param string $code
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-04-28 07:08:32 +12:00
|
|
|
public function getAccessToken(string $code): string
|
2022-02-01 23:42:11 +13:00
|
|
|
{
|
|
|
|
$tokens = $this->getTokens($code);
|
2022-05-13 03:56:20 +12:00
|
|
|
|
2022-02-04 00:57:04 +13:00
|
|
|
return $tokens['access_token'] ?? '';
|
2022-02-01 23:42:11 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $code
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-04-28 07:08:32 +12:00
|
|
|
public function getRefreshToken(string $code): string
|
2022-02-01 23:42:11 +13:00
|
|
|
{
|
|
|
|
$tokens = $this->getTokens($code);
|
2022-05-13 03:56:20 +12:00
|
|
|
|
2022-02-04 00:57:04 +13:00
|
|
|
return $tokens['refresh_token'] ?? '';
|
2022-02-01 23:42:11 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $code
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-04-28 07:08:32 +12:00
|
|
|
public function getAccessTokenExpiry(string $code): string
|
2022-02-01 23:42:11 +13:00
|
|
|
{
|
|
|
|
$tokens = $this->getTokens($code);
|
2022-05-13 03:56:20 +12:00
|
|
|
|
2022-02-04 00:57:04 +13:00
|
|
|
return $tokens['expires_in'] ?? '';
|
2022-02-01 23:42:11 +13:00
|
|
|
}
|
2020-01-13 00:51:30 +13:00
|
|
|
|
2020-02-17 00:41:03 +13:00
|
|
|
// The parseState function was designed specifically for Amazon OAuth2 Adapter to override.
|
2019-10-07 01:58:01 +13:00
|
|
|
// The response from Amazon is html encoded and hence it needs to be html_decoded before
|
2019-10-07 01:56:44 +13:00
|
|
|
// json_decoding
|
|
|
|
/**
|
|
|
|
* @param $state
|
|
|
|
*
|
2020-10-27 13:08:29 +13:00
|
|
|
* @return array
|
2019-10-07 01:56:44 +13:00
|
|
|
*/
|
2019-10-14 07:25:39 +13:00
|
|
|
public function parseState(string $state)
|
2019-10-07 01:58:01 +13:00
|
|
|
{
|
2020-06-20 23:05:43 +12:00
|
|
|
return \json_decode($state, true);
|
2019-10-07 01:56:44 +13:00
|
|
|
}
|
|
|
|
|
2019-05-09 18:54:39 +12:00
|
|
|
/**
|
|
|
|
* @param string $method
|
|
|
|
* @param string $url
|
2019-09-07 05:04:26 +12:00
|
|
|
* @param array $headers
|
2019-05-09 18:54:39 +12:00
|
|
|
* @param string $payload
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return string
|
|
|
|
*/
|
2022-04-28 07:08:32 +12:00
|
|
|
protected function request(string $method, string $url = '', array $headers = [], string $payload = ''): string
|
2019-05-09 18:54:39 +12:00
|
|
|
{
|
2020-06-20 23:05:43 +12:00
|
|
|
$ch = \curl_init($url);
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2020-06-20 23:05:43 +12:00
|
|
|
\curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
|
|
|
|
\curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
|
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
2020-06-28 10:30:03 +12:00
|
|
|
\curl_setopt($ch, CURLOPT_USERAGENT, 'Appwrite OAuth2');
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-07 05:04:26 +12:00
|
|
|
if (!empty($payload)) {
|
2020-06-20 23:05:43 +12:00
|
|
|
\curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
|
|
|
|
2020-06-20 23:05:43 +12:00
|
|
|
$headers[] = 'Content-length: '.\strlen($payload);
|
|
|
|
\curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
2019-09-30 11:03:22 +13:00
|
|
|
|
|
|
|
// Send the request & save response to $response
|
2020-06-20 23:05:43 +12:00
|
|
|
$response = \curl_exec($ch);
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2020-06-20 23:05:43 +12:00
|
|
|
\curl_close($ch);
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2020-10-27 13:08:29 +13:00
|
|
|
return (string)$response;
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
2019-09-07 05:04:26 +12:00
|
|
|
}
|