mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
feat(client): support custom authSource for SCRAM authentication
SCRAM authentication was hardcoded to run against the admin database, which fails when the MongoDB user was created in the application database itself (db.createUser() run inside the target database). Add an authSource option to Auth and a matching constructor parameter to Client so callers can authenticate against the database that actually holds the user. Defaults to admin, preserving existing behaviour. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0b0aac1cd2
commit
1d45f5f8cc
3 files changed
+67
-4
No files matched your search
+4
-2
@@ -9,6 +9,7 @@ class Auth
|
||||
private string $authcid;
|
||||
private string $secret;
|
||||
private string $authzid;
|
||||
private string $authSource;
|
||||
private string $gs2Header;
|
||||
private ?string $cnonce = null;
|
||||
private string $firstMessageBare;
|
||||
@@ -23,6 +24,7 @@ class Auth
|
||||
$this->authcid = $options['authcid'] ?? '';
|
||||
$this->secret = $options['secret'] ?? '';
|
||||
$this->authzid = $options['authzid'] ?? '';
|
||||
$this->authSource = $options['authSource'] ?? 'admin';
|
||||
$this->cnonce = base64_encode(random_bytes(32));
|
||||
}
|
||||
|
||||
@@ -39,7 +41,7 @@ class Auth
|
||||
"autoAuthorize" => 1,
|
||||
"options" => ["skipEmptyExchange" => true],
|
||||
],
|
||||
'admin'
|
||||
$this->authSource
|
||||
];
|
||||
}
|
||||
|
||||
@@ -61,7 +63,7 @@ class Auth
|
||||
"conversationId" => $cid,
|
||||
"payload" => $payload,
|
||||
],
|
||||
'admin'
|
||||
$this->authSource
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
+7
-2
@@ -134,6 +134,9 @@ class Client
|
||||
* @param string $user
|
||||
* @param string $password
|
||||
* @param Boolean $useCoroutine
|
||||
* @param string|null $authSource Database to authenticate against; defaults to 'admin'.
|
||||
* Set this when the user was created in a database other than admin (e.g. the
|
||||
* application database itself).
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function __construct(
|
||||
@@ -144,7 +147,8 @@ class Client
|
||||
string $password,
|
||||
bool $useCoroutine = false,
|
||||
bool $tls = false,
|
||||
array $tlsOptions = []
|
||||
array $tlsOptions = [],
|
||||
?string $authSource = null
|
||||
) {
|
||||
if (empty($database)) {
|
||||
throw new \InvalidArgumentException('Database name cannot be empty');
|
||||
@@ -210,7 +214,8 @@ class Client
|
||||
|
||||
$this->auth = new Auth([
|
||||
'authcid' => $user,
|
||||
'secret' => Auth::encodeCredentials($user, $password)
|
||||
'secret' => Auth::encodeCredentials($user, $password),
|
||||
'authSource' => $authSource ?? 'admin',
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace Utopia\Tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Utopia\Mongo\Auth;
|
||||
|
||||
class AuthTest extends TestCase
|
||||
{
|
||||
public function testStartDefaultsToAdminAuthSource(): void
|
||||
{
|
||||
$auth = new Auth([
|
||||
'authcid' => 'root',
|
||||
'secret' => Auth::encodeCredentials('root', 'example'),
|
||||
]);
|
||||
|
||||
[$payload, $database] = $auth->start();
|
||||
|
||||
$this->assertSame(1, $payload['saslStart']);
|
||||
$this->assertSame('admin', $database);
|
||||
}
|
||||
|
||||
public function testStartUsesProvidedAuthSource(): void
|
||||
{
|
||||
$auth = new Auth([
|
||||
'authcid' => 'appuser',
|
||||
'secret' => Auth::encodeCredentials('appuser', 'example'),
|
||||
'authSource' => 'appdb',
|
||||
]);
|
||||
|
||||
[$payload, $database] = $auth->start();
|
||||
|
||||
$this->assertSame(1, $payload['saslStart']);
|
||||
$this->assertSame('appdb', $database);
|
||||
}
|
||||
|
||||
public function testContinueUsesProvidedAuthSource(): void
|
||||
{
|
||||
$auth = new Auth([
|
||||
'authcid' => 'appuser',
|
||||
'secret' => Auth::encodeCredentials('appuser', 'example'),
|
||||
'authSource' => 'appdb',
|
||||
]);
|
||||
|
||||
$auth->start();
|
||||
|
||||
$data = new \stdClass();
|
||||
$data->conversationId = 1;
|
||||
$data->payload = new \MongoDB\BSON\Binary('r=' . $auth->getCnonce() . 'server,s=' . base64_encode('salt') . ',i=4096', 0);
|
||||
|
||||
[$payload, $database] = $auth->continue($data);
|
||||
|
||||
$this->assertSame(1, $payload['saslContinue']);
|
||||
$this->assertSame('appdb', $database);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user