Merge pull request #276 from appwrite/OAuth-mock-server-and-adapter
OAuth mock server and adapter
This commit is contained in:
commit
1f2a9f7087
2 changed files with 191 additions and 0 deletions
|
@ -6,6 +6,7 @@ use Utopia\Validator\Numeric;
|
||||||
use Utopia\Validator\Text;
|
use Utopia\Validator\Text;
|
||||||
use Utopia\Validator\ArrayList;
|
use Utopia\Validator\ArrayList;
|
||||||
use Storage\Validators\File;
|
use Storage\Validators\File;
|
||||||
|
use Utopia\Validator\Host;
|
||||||
|
|
||||||
$result = [];
|
$result = [];
|
||||||
|
|
||||||
|
@ -218,6 +219,73 @@ $utopia->get('/v1/mock/tests/general/empty')
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$utopia->get('/v1/mock/tests/general/oauth/login')
|
||||||
|
->desc('Mock an OAuth login route')
|
||||||
|
->label('scope', 'public')
|
||||||
|
->label('sdk.namespace', 'general')
|
||||||
|
->label('sdk.method', 'empty')
|
||||||
|
->label('sdk.description', 'Mock an OAuth login route')
|
||||||
|
->param('client_id', '', function () { return new Text(100); }, 'OAuth Client ID.')
|
||||||
|
->param('redirect_uri', '', function () { return new Host(['http://localhost']); }, 'OAuth Redirect URI.')
|
||||||
|
->param('scope', '', function () { return new Text(100); }, 'OAuth scope list.')
|
||||||
|
->param('state', '', function () { return new Text(100); }, 'OAuth state.')
|
||||||
|
->action(
|
||||||
|
function ($clientId, $redirectURI, $scope, $state) use ($response) {
|
||||||
|
var_dump($clientId, $redirectURI, $scope, $state);
|
||||||
|
exit();
|
||||||
|
$response->redirect('');
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
$utopia->get('/v1/mock/tests/general/oauth/token')
|
||||||
|
->desc('Mock an OAuth login route')
|
||||||
|
->label('scope', 'public')
|
||||||
|
->label('sdk.namespace', 'general')
|
||||||
|
->label('sdk.method', 'empty')
|
||||||
|
->label('sdk.description', 'Mock an OAuth login route')
|
||||||
|
->param('client_id', '', function () { return new Text(100); }, 'OAuth Client ID.')
|
||||||
|
->param('redirect_uri', '', function () { return new Host(['http://localhost']); }, 'OAuth Redirect URI.')
|
||||||
|
->param('client_secret', '', function () { return new Text(100); }, 'OAuth scope list.')
|
||||||
|
->param('code', '', function () { return new Text(100); }, 'OAuth state.')
|
||||||
|
->action(
|
||||||
|
function ($clientId, $redirectURI, $clientSecret, $code) use ($response) {
|
||||||
|
if($clientId != '1') {
|
||||||
|
throw new Exception('Invalid client ID');
|
||||||
|
}
|
||||||
|
|
||||||
|
if($clientSecret != 'secret') {
|
||||||
|
throw new Exception('Invalid client secret');
|
||||||
|
}
|
||||||
|
|
||||||
|
if($code != 'abcdef') {
|
||||||
|
throw new Exception('Invalid token');
|
||||||
|
}
|
||||||
|
|
||||||
|
$response->json(['access_token' => '123456']);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
$utopia->get('/v1/mock/tests/general/oauth/user')
|
||||||
|
->desc('Mock an OAuth user route')
|
||||||
|
->label('scope', 'public')
|
||||||
|
->label('sdk.namespace', 'general')
|
||||||
|
->label('sdk.method', 'empty')
|
||||||
|
->label('sdk.description', 'Mock an OAuth user route')
|
||||||
|
->param('token', '', function () { return new Text(100); }, 'OAuth Access Token.')
|
||||||
|
->action(
|
||||||
|
function ($token) use ($response) {
|
||||||
|
if($token != '123456') {
|
||||||
|
throw new Exception('Invalid token');
|
||||||
|
}
|
||||||
|
|
||||||
|
$response->json([
|
||||||
|
'id' => 1,
|
||||||
|
'name' => 'User Name',
|
||||||
|
'email' => 'user@localhost',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
$utopia->shutdown(function() use ($response, $request, &$result, $utopia) {
|
$utopia->shutdown(function() use ($response, $request, &$result, $utopia) {
|
||||||
|
|
||||||
$route = $utopia->match($request);
|
$route = $utopia->match($request);
|
||||||
|
|
123
src/Auth/OAuth/Mock.php
Normal file
123
src/Auth/OAuth/Mock.php
Normal file
|
@ -0,0 +1,123 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Auth\OAuth;
|
||||||
|
|
||||||
|
use Auth\OAuth;
|
||||||
|
|
||||||
|
class Mock extends OAuth
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $version = 'v1';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $user = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getName():string
|
||||||
|
{
|
||||||
|
return 'mock';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getLoginURL():string
|
||||||
|
{
|
||||||
|
return 'http://localhost/'.$this->version.'/oauth?client_id='.urlencode($this->appID).'&redirect_uri='.urlencode($this->callback).'&scope=email&state='.urlencode(json_encode($this->state));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $code
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getAccessToken(string $code):string
|
||||||
|
{
|
||||||
|
$accessToken = $this->request(
|
||||||
|
'GET',
|
||||||
|
'http://localhost/'.$this->version.'/oauth/token?'.
|
||||||
|
'client_id='.urlencode($this->appID).
|
||||||
|
'&redirect_uri='.urlencode($this->callback).
|
||||||
|
'&client_secret='.urlencode($this->appSecret).
|
||||||
|
'&code='.urlencode($code)
|
||||||
|
);
|
||||||
|
|
||||||
|
$accessToken = json_decode($accessToken, true); //
|
||||||
|
|
||||||
|
if (isset($accessToken['access_token'])) {
|
||||||
|
return $accessToken['access_token'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $accessToken
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getUserID(string $accessToken):string
|
||||||
|
{
|
||||||
|
$user = $this->getUser($accessToken);
|
||||||
|
|
||||||
|
if (isset($user['id'])) {
|
||||||
|
return $user['id'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $accessToken
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getUserEmail(string $accessToken):string
|
||||||
|
{
|
||||||
|
$user = $this->getUser($accessToken);
|
||||||
|
|
||||||
|
if (isset($user['email'])) {
|
||||||
|
return $user['email'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $accessToken
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getUserName(string $accessToken):string
|
||||||
|
{
|
||||||
|
$user = $this->getUser($accessToken);
|
||||||
|
|
||||||
|
if (isset($user['name'])) {
|
||||||
|
return $user['name'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $accessToken
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getUser(string $accessToken):array
|
||||||
|
{
|
||||||
|
if (empty($this->user)) {
|
||||||
|
$user = $this->request('GET', 'http://localhost/'.$this->version.'/oauth/user?token='.urlencode($accessToken));
|
||||||
|
|
||||||
|
$this->user = json_decode($user, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->user;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue