appwrite/src/Auth/OAuth.php

155 lines
3.2 KiB
PHP
Raw Normal View History

2019-05-09 06:54:39 +00:00
<?php
namespace Auth;
abstract class OAuth
{
/**
* @var string
*/
protected $appID;
/**
* @var string
*/
protected $appSecret;
/**
* @var string
*/
protected $callback;
/**
* @var string
*/
protected $state;
/**
* @var array
*/
protected $scopes;
2019-05-09 06:54:39 +00:00
/**
* OAuth constructor.
*
* @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, $state = [], $scopes = [])
2019-05-09 06:54:39 +00:00
{
$this->appID = $appId;
$this->appSecret = $appSecret;
$this->callback = $callback;
$this->state = $state;
foreach($scopes as $scope) {
$this->addScope($scope);
}
2019-05-09 06:54:39 +00:00
}
/**
* @return string
*/
abstract public function getName():string;
/**
* @return string
*/
abstract public function getLoginURL():string;
/**
* @param string $code
*
2019-05-09 06:54:39 +00:00
* @return string
*/
abstract public function getAccessToken(string $code):string;
/**
* @param $accessToken
*
2019-05-09 06:54:39 +00:00
* @return string
*/
abstract public function getUserID(string $accessToken):string;
/**
* @param $accessToken
*
2019-05-09 06:54:39 +00:00
* @return string
*/
abstract public function getUserEmail(string $accessToken):string;
/**
* @param $accessToken
*
2019-05-09 06:54:39 +00:00
* @return string
*/
abstract public function getUserName(string $accessToken):string;
/**
* @param $scope
*
* @return array
*/
protected function addScope(string $scope){
// Add a scope to the scopes array if it isn't already present
if (!in_array($scope, $this->scopes)){
$this->scopes[] = $scope;
}
}
/**
* @return array
*/
protected function getScopes(){
return $this->scopes;
}
2019-10-07 19:39:01 +00:00
2020-01-12 11:51:30 +00:00
// The parseState function was designed specifically for Amazon OAuth 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
*
2019-10-07 19:39:01 +00:00
* @return string
*/
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
{
$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, 'Console_OAuth_Agent');
if (!empty($payload)) {
2019-05-09 06:54:39 +00:00
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
}
2019-10-01 04:34:01 +00:00
$headers[] = 'Content-length: '.strlen($payload);
2019-09-29 22:03:22 +00:00
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Send the request & save response to $response
2019-05-09 06:54:39 +00:00
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
}