Skip to content

Commit 9706415

Browse files
committed
Merge pull request #1125
2 parents caeb5f4 + 0fa7df9 commit 9706415

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

php_phongo.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1382,11 +1382,21 @@ static const char* php_phongo_bson_type_to_string(bson_type_t type) /* {{{ */
13821382

13831383
static bool php_phongo_uri_finalize_auth(mongoc_uri_t* uri) /* {{{ */
13841384
{
1385+
const bson_t* credentials = mongoc_uri_get_credentials(uri);
1386+
bson_iter_t iter;
1387+
const char* source = NULL;
1388+
const char* username = mongoc_uri_get_username(uri);
1389+
bool require_auth = username != NULL;
1390+
1391+
if (bson_iter_init_find_case(&iter, credentials, MONGOC_URI_AUTHSOURCE)) {
1392+
source = bson_iter_utf8(&iter, NULL);
1393+
require_auth = true;
1394+
}
1395+
13851396
/* authSource with GSSAPI or X509 should always be external */
13861397
if (mongoc_uri_get_auth_mechanism(uri)) {
13871398
if (!strcasecmp(mongoc_uri_get_auth_mechanism(uri), "GSSAPI") ||
13881399
!strcasecmp(mongoc_uri_get_auth_mechanism(uri), "MONGODB-X509")) {
1389-
const char* source = mongoc_uri_get_auth_source(uri);
13901400

13911401
if (source) {
13921402
if (strcasecmp(source, "$external")) {
@@ -1415,6 +1425,11 @@ static bool php_phongo_uri_finalize_auth(mongoc_uri_t* uri) /* {{{ */
14151425
return false;
14161426
}
14171427
}
1428+
} else if (require_auth) {
1429+
if (source && strcmp(source, "$external") != 0 && (!username || strcmp(username, "") == 0)) {
1430+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Failed to parse URI options: Default authentication mechanism requires username.");
1431+
return false;
1432+
}
14181433
}
14191434

14201435
return true;

tests/manager/manager-ctor-auth_mechanism-error-001.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ echo throws(function() {
3333
new MongoDB\Driver\Manager('mongodb://localhost:27017/', ['password' => 'password', 'authMechanism' => 'MONGODB-X509']);
3434
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
3535

36+
echo throws(function() {
37+
new MongoDB\Driver\Manager('mongodb://localhost:27017/?authSource=foo');
38+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
39+
40+
echo throws(function() {
41+
new MongoDB\Driver\Manager('mongodb://localhost:27017/', ['authSource' => 'foo']);
42+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
43+
3644
?>
3745
===DONE===
3846
<?php exit(0); ?>
@@ -51,4 +59,8 @@ OK: Got MongoDB\Driver\Exception\InvalidArgumentException
5159
Failed to parse URI options: 'SCRAM-SHA-1' authentication mechanism requires username.
5260
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
5361
Failed to parse URI options: X509 authentication mechanism does not accept a password.
62+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
63+
Failed to parse MongoDB URI: 'mongodb://localhost:27017/?authSource=foo'. Default authentication mechanism requires username.
64+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
65+
Failed to parse URI options: Default authentication mechanism requires username.
5466
===DONE===
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::__construct(): authSource option
3+
--FILE--
4+
<?php
5+
6+
$tests = [
7+
['mongodb://[email protected]/?authSource=$external', []],
8+
[null, ['authSource' => '$external']],
9+
];
10+
11+
foreach ($tests as $test) {
12+
list($uri, $options) = $test;
13+
14+
/* Note: the Manager's debug information does not include the auth mechanism
15+
* so we are merely testing that no exception is thrown. */
16+
$manager = new MongoDB\Driver\Manager($uri, $options);
17+
}
18+
19+
?>
20+
===DONE===
21+
<?php exit(0); ?>
22+
--EXPECT--
23+
===DONE===

0 commit comments

Comments
 (0)