1
0
Fork 0
mirror of synced 2024-06-03 03:14:50 +12:00
appwrite/src/Appwrite/Auth/OAuth2.php

158 lines
3.3 KiB
PHP
Raw Normal View History

2019-05-09 18:54:39 +12:00
<?php
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
*/
protected $appID;
/**
* @var string
*/
protected $appSecret;
/**
* @var string
*/
protected $callback;
/**
* @var array
2019-05-09 18:54:39 +12:00
*/
protected $state;
/**
* @var array
*/
protected $scopes;
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
* @param array $state
* @param array $scopes
2019-05-09 18:54:39 +12:00
*/
public function __construct(string $appId, string $appSecret, string $callback, array $state = [], array $scopes = [])
2019-05-09 18:54:39 +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) {
$this->addScope($scope);
}
2019-05-09 18:54:39 +12:00
}
/**
* @return string
*/
abstract public function getName():string;
/**
* @return string
*/
abstract public function getLoginURL():string;
/**
* @param string $code
*
2019-05-09 18:54:39 +12:00
* @return string
*/
abstract public function getAccessToken(string $code):string;
/**
* @param $accessToken
*
2019-05-09 18:54:39 +12:00
* @return string
*/
abstract public function getUserID(string $accessToken):string;
/**
* @param $accessToken
*
2019-05-09 18:54:39 +12:00
* @return string
*/
abstract public function getUserEmail(string $accessToken):string;
/**
* @param $accessToken
*
2019-05-09 18:54:39 +12:00
* @return string
*/
abstract public function getUserName(string $accessToken):string;
/**
* @param $scope
2020-06-25 09:05:16 +12:00
*
2020-01-14 03:13:08 +13:00
* @return $this
*/
2020-02-17 00:41:03 +13: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;
}
return $this;
}
2020-06-25 09:02:27 +12:00
/**
* @return array
*/
2020-01-19 10:08:28 +13:00
protected function getScopes():array
{
return $this->scopes;
}
2019-10-08 08:39:01 +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
// json_decoding
/**
* @param $state
*
* @return array
*/
2019-10-14 07:25:39 +13:00
public function parseState(string $state)
2019-10-07 01:58:01 +13:00
{
return \json_decode($state, true);
}
2019-05-09 18:54:39 +12:00
/**
* @param string $method
* @param string $url
* @param array $headers
2019-05-09 18:54:39 +12:00
* @param string $payload
*
2019-05-09 18:54:39 +12:00
* @return string
*/
protected function request(string $method, string $url = '', array $headers = [], string $payload = ''):string
{
$ch = \curl_init($url);
2019-05-09 18:54:39 +12: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 18:54:39 +12:00
if (!empty($payload)) {
\curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
2019-05-09 18:54:39 +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
$response = \curl_exec($ch);
2019-05-09 18:54:39 +12:00
\curl_close($ch);
2019-05-09 18:54:39 +12:00
return (string)$response;
2019-05-09 18:54:39 +12:00
}
}