appwrite/src/Appwrite/Auth/OAuth2.php

218 lines
4.6 KiB
PHP
Raw Normal View History

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