From 87c7e76a1609319798e6b80498a639f5bde26266 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 9 Jan 2023 16:20:03 +0545 Subject: [PATCH 1/6] Oauth2 check if provider is enabled --- app/controllers/api/account.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index af5c8e4f53..0ad8c5b56a 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -280,6 +280,12 @@ App::get('/v1/account/sessions/oauth2/:provider') $protocol = $request->getProtocol(); $callback = $protocol . '://' . $request->getHostname() . '/v1/account/sessions/oauth2/callback/' . $provider . '/' . $project->getId(); + $providerEnabled = $project->getAttribute('authProviders', [])[$provider.'Enabled'] ?? false; + + if (!$providerEnabled) { + throw new Exception(Exception::PROJECT_PROVIDER_DISABLED, 'This provider is disabled. Please enable the provider from your ' . APP_NAME . ' console to continue.'); + } + $appId = $project->getAttribute('authProviders', [])[$provider . 'Appid'] ?? ''; $appSecret = $project->getAttribute('authProviders', [])[$provider . 'Secret'] ?? '{}'; From b238a1ef2d3a85bd9aa59dd2497edb2df4136105 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 9 Jan 2023 10:44:28 +0000 Subject: [PATCH 2/6] fix format --- app/controllers/api/account.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 0ad8c5b56a..40e8c11f6e 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -280,12 +280,12 @@ App::get('/v1/account/sessions/oauth2/:provider') $protocol = $request->getProtocol(); $callback = $protocol . '://' . $request->getHostname() . '/v1/account/sessions/oauth2/callback/' . $provider . '/' . $project->getId(); - $providerEnabled = $project->getAttribute('authProviders', [])[$provider.'Enabled'] ?? false; - + $providerEnabled = $project->getAttribute('authProviders', [])[$provider . 'Enabled'] ?? false; + if (!$providerEnabled) { throw new Exception(Exception::PROJECT_PROVIDER_DISABLED, 'This provider is disabled. Please enable the provider from your ' . APP_NAME . ' console to continue.'); } - + $appId = $project->getAttribute('authProviders', [])[$provider . 'Appid'] ?? ''; $appSecret = $project->getAttribute('authProviders', [])[$provider . 'Secret'] ?? '{}'; From d2ca803753b03ee22e3f641935a1533668d3bfe2 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 10 Jan 2023 04:52:21 +0000 Subject: [PATCH 3/6] add provider enabled check in the redirect --- app/controllers/api/account.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 40e8c11f6e..a80050bbfa 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -400,6 +400,11 @@ App::get('/v1/account/sessions/oauth2/:provider/redirect') $validateURL = new URL(); $appId = $project->getAttribute('authProviders', [])[$provider . 'Appid'] ?? ''; $appSecret = $project->getAttribute('authProviders', [])[$provider . 'Secret'] ?? '{}'; + $providerEnabled = $project->getAttribute('authProviders', [])[$provider . 'Enabled'] ?? false; + + if (!$providerEnabled) { + throw new Exception(Exception::PROJECT_PROVIDER_DISABLED, 'This provider is disabled. Please enable the provider from your ' . APP_NAME . ' console to continue.'); + } if (!empty($appSecret) && isset($appSecret['version'])) { $key = App::getEnv('_APP_OPENSSL_KEY_V' . $appSecret['version']); From d53456c6ed6aca042cb4180846e47295e66dffac Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 17 Jan 2023 01:17:37 +0000 Subject: [PATCH 4/6] fix test --- tests/e2e/Services/Account/AccountCustomClientTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/e2e/Services/Account/AccountCustomClientTest.php b/tests/e2e/Services/Account/AccountCustomClientTest.php index 9971b1bca3..aadef9708f 100644 --- a/tests/e2e/Services/Account/AccountCustomClientTest.php +++ b/tests/e2e/Services/Account/AccountCustomClientTest.php @@ -40,6 +40,7 @@ class AccountCustomClientTest extends Scope 'provider' => $provider, 'appId' => $appId, 'secret' => $secret, + 'enabled' => true, ]); $this->assertEquals($response['headers']['status-code'], 200); @@ -496,6 +497,7 @@ class AccountCustomClientTest extends Scope 'provider' => $provider, 'appId' => $appId, 'secret' => $secret, + 'enabled' => true, ]); $this->assertEquals($response['headers']['status-code'], 200); From 637e4098be1f8f134ebc2b34ede038f2dc8d102b Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 14 Feb 2023 05:56:14 +0000 Subject: [PATCH 5/6] provider disabled test --- .../Account/AccountCustomClientTest.php | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/e2e/Services/Account/AccountCustomClientTest.php b/tests/e2e/Services/Account/AccountCustomClientTest.php index aadef9708f..f66b42092f 100644 --- a/tests/e2e/Services/Account/AccountCustomClientTest.php +++ b/tests/e2e/Services/Account/AccountCustomClientTest.php @@ -57,6 +57,34 @@ class AccountCustomClientTest extends Scope $this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals('success', $response['body']['result']); + /** + * Test for Failure when disabled + */ + $response = $this->client->call(Client::METHOD_PATCH, '/projects/' . $this->getProject()['$id'] . '/oauth2', array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => 'console', + 'cookie' => 'a_session_console=' . $this->getRoot()['session'], + ]), [ + 'provider' => $provider, + 'appId' => $appId, + 'secret' => $secret, + 'enabled' => false, + ]); + + $this->assertEquals($response['headers']['status-code'], 200); + + $response = $this->client->call(Client::METHOD_GET, '/account/sessions/oauth2/' . $provider, array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ]), [ + 'success' => 'http://localhost/v1/mock/tests/general/oauth2/success', + 'failure' => 'http://localhost/v1/mock/tests/general/oauth2/failure', + ]); + + $this->assertEquals(500, $response['headers']['status-code']); + return []; } From 3f171fbdb9253172ca480ae990f674e577585eed Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 14 Feb 2023 06:07:06 +0000 Subject: [PATCH 6/6] fix error code --- tests/e2e/Services/Account/AccountCustomClientTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/Services/Account/AccountCustomClientTest.php b/tests/e2e/Services/Account/AccountCustomClientTest.php index f66b42092f..d623d07661 100644 --- a/tests/e2e/Services/Account/AccountCustomClientTest.php +++ b/tests/e2e/Services/Account/AccountCustomClientTest.php @@ -83,7 +83,7 @@ class AccountCustomClientTest extends Scope 'failure' => 'http://localhost/v1/mock/tests/general/oauth2/failure', ]); - $this->assertEquals(500, $response['headers']['status-code']); + $this->assertEquals(412, $response['headers']['status-code']); return []; }