diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 0e24dcd431..1a5d6f7b00 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'] ?? '{}'; @@ -394,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']); diff --git a/tests/e2e/Services/Account/AccountCustomClientTest.php b/tests/e2e/Services/Account/AccountCustomClientTest.php index 47ed22be70..82002eb485 100644 --- a/tests/e2e/Services/Account/AccountCustomClientTest.php +++ b/tests/e2e/Services/Account/AccountCustomClientTest.php @@ -41,6 +41,7 @@ class AccountCustomClientTest extends Scope 'provider' => $provider, 'appId' => $appId, 'secret' => $secret, + 'enabled' => true, ]); $this->assertEquals($response['headers']['status-code'], 200); @@ -57,6 +58,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(412, $response['headers']['status-code']); + return []; } @@ -498,6 +527,7 @@ class AccountCustomClientTest extends Scope 'provider' => $provider, 'appId' => $appId, 'secret' => $secret, + 'enabled' => true, ]); $this->assertEquals($response['headers']['status-code'], 200);