1
0
Fork 0
mirror of synced 2024-09-29 08:51:28 +13:00
appwrite/src/Auth/OAuth.php

153 lines
3.2 KiB
PHP
Raw Normal View History

2019-05-09 18:54:39 +12: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
*/
2020-01-13 00:51:30 +13:00
protected $userScopes;
2019-05-09 18:54:39 +12:00
/**
* OAuth constructor.
*
* @param string $appId
* @param string $appSecret
* @param string $callback
* @param array $state
2020-01-13 00:51:30 +13:00
* @param array $userScopes
2019-05-09 18:54:39 +12:00
*/
2020-01-13 00:51:30 +13:00
public function __construct(string $appId, string $appSecret, string $callback, $state = [], $userScopes = [])
2019-05-09 18:54:39 +12:00
{
$this->appID = $appId;
$this->appSecret = $appSecret;
$this->callback = $callback;
$this->state = $state;
2020-01-13 00:51:30 +13:00
$this->userScopes = $userScopes;
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
*
* @return array
*/
protected function addScope(string $scope){
// Add a scope to the scopes array if it isn't already present
2020-01-13 00:51:30 +13:00
if (!in_array($scope, $this->userScopes)){
$this->userScopes[] = $scope;
}
}
/**
* @return array
*/
protected function getScopes(){
2020-01-13 00:51:30 +13:00
return $this->userScopes;
}
2019-10-08 08:39:01 +13:00
2020-01-13 00:51:30 +13:00
// The parseState function was designed specifically for Amazon OAuth 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
*
2019-10-08 08:39:01 +13:00
* @return string
*/
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, 'Console_OAuth_Agent');
if (!empty($payload)) {
2019-05-09 18:54:39 +12:00
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
}
2019-10-01 17:34:01 +13:00
$headers[] = 'Content-length: '.strlen($payload);
2019-09-30 11:03:22 +13:00
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Send the request & save response to $response
2019-05-09 18:54:39 +12:00
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
}