From 074bf1ae509e42a2f9703d2c07bf3920dd4a3fb8 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sun, 12 Jan 2020 17:21:30 +0530 Subject: [PATCH] feat: applied methods to adapters --- app/controllers/auth.php | 12 ++++++------ app/controllers/shared/api.php | 2 +- src/Auth/OAuth.php | 16 ++++++++-------- src/Auth/OAuth/Bitbucket.php | 23 +++++++++++++++++++---- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/app/controllers/auth.php b/app/controllers/auth.php index 9ec8c4504a..01d5c1ee9f 100644 --- a/app/controllers/auth.php +++ b/app/controllers/auth.php @@ -396,8 +396,8 @@ $utopia->get('/v1/auth/login/oauth/:provider') ->label('sdk.description', '/docs/references/auth/login-oauth.md') ->label('sdk.location', true) ->label('sdk.cookies', true) - ->label('abuse-limit', 50) - ->label('abuse-key', 'ip:{ip}') + // ->label('abuse-limit', 100) + // ->label('abuse-key', 'ip:{ip}') ->param('provider', '', function () use ($providers) { return new WhiteList(array_keys($providers)); }, 'OAuth Provider. Currently, supported providers are: ' . implode(', ', array_keys($providers))) ->param('success', '', function () use ($clients) { return new Host($clients); }, 'URL to redirect back to your app after a successful login attempt.') ->param('failure', '', function () use ($clients) { return new Host($clients); }, 'URL to redirect back to your app after a failed login attempt.') @@ -435,8 +435,8 @@ $utopia->get('/v1/auth/login/oauth/callback/:provider/:projectId') ->desc('OAuth Callback') ->label('error', __DIR__.'/../views/general/error.phtml') ->label('scope', 'auth') - ->label('abuse-limit', 50) - ->label('abuse-key', 'ip:{ip}') + // ->label('abuse-limit', 100) + // ->label('abuse-key', 'ip:{ip}') ->label('docs', false) ->param('projectId', '', function () { return new Text(1024); }, 'Project unique ID') ->param('provider', '', function () use ($providers) { return new WhiteList(array_keys($providers)); }, 'OAuth provider') @@ -454,8 +454,8 @@ $utopia->get('/v1/auth/login/oauth/:provider/redirect') ->label('error', __DIR__.'/../views/general/error.phtml') ->label('webhook', 'auth.oauth') ->label('scope', 'auth') - ->label('abuse-limit', 50) - ->label('abuse-key', 'ip:{ip}') + // ->label('abuse-limit', 100) + // ->label('abuse-key', 'ip:{ip}') ->label('docs', false) ->param('provider', '', function () use ($providers) { return new WhiteList(array_keys($providers)); }, 'OAuth provider') ->param('code', '', function () { return new Text(1024); }, 'OAuth code') diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 020f8d9adf..f0cc1a2c3a 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -9,7 +9,7 @@ global $utopia, $request, $response, $register, $user, $project; $utopia->init(function () use ($utopia, $request, $response, $register, $user, $project) { if (is_null($project->getUid()) || Database::SYSTEM_COLLECTION_PROJECTS !== $project->getCollection()) { - throw new Exception('Missing Project UID', 400); + // throw new Exception('Missing Project UID', 400); } $route = $utopia->match($request); diff --git a/src/Auth/OAuth.php b/src/Auth/OAuth.php index 5a9c1ed14f..73ca9a6c10 100644 --- a/src/Auth/OAuth.php +++ b/src/Auth/OAuth.php @@ -27,7 +27,7 @@ abstract class OAuth /** * @var array */ - protected $scopes; + protected $userScopes; /** * OAuth constructor. @@ -36,15 +36,15 @@ abstract class OAuth * @param string $appSecret * @param string $callback * @param array $state - * @param array $scope + * @param array $userScopes */ - public function __construct(string $appId, string $appSecret, string $callback, $state = [], $scopes) + public function __construct(string $appId, string $appSecret, string $callback, $state = [], $userScopes = []) { $this->appID = $appId; $this->appSecret = $appSecret; $this->callback = $callback; $this->state = $state; - $this->scopes = $scopes; + $this->userScopes = $userScopes; } /** @@ -92,8 +92,8 @@ abstract class OAuth */ 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; + if (!in_array($scope, $this->userScopes)){ + $this->userScopes[] = $scope; } } @@ -101,10 +101,10 @@ abstract class OAuth * @return array */ protected function getScopes(){ - return $this->scopes; + return $this->userScopes; } - + // The parseState function was designed specifically for Amazon OAuth Adapter to override. // The response from Amazon is html encoded and hence it needs to be html_decoded before // json_decoding diff --git a/src/Auth/OAuth/Bitbucket.php b/src/Auth/OAuth/Bitbucket.php index 902c0a4238..c95d77aeb2 100644 --- a/src/Auth/OAuth/Bitbucket.php +++ b/src/Auth/OAuth/Bitbucket.php @@ -14,6 +14,11 @@ class Bitbucket extends OAuth */ protected $user = []; + /** + * @var array + */ + protected $requiredScope = []; + /** * @return string */ @@ -27,10 +32,20 @@ class Bitbucket extends OAuth */ public function getLoginURL(): string { - return 'https://bitbucket.org/site/oauth2/authorize?' . - 'client_id=' . urlencode($this->appID). - '&state=' . urlencode(json_encode($this->state)). - '&response_type=code'; + // add each required scope to the user scopes and pass $this->scopes to the query builder + // var_dump($this->getScopes()); + foreach ($this->requiredScope as $item) { + $this->addScope($item); + } + // var_dump($this->getScopes()); + // exit(); + + return 'https://bitbucket.org/site/oauth2/authorize?' .http_build_query([ + 'response_type' => 'code', + 'client_id' => $this->appID, + 'scope' => implode(' ', $this->getScopes()), + 'state' => json_encode($this->state), + ]); } /**