File tree Expand file tree Collapse file tree 2 files changed +28
-1
lines changed
Expand file tree Collapse file tree 2 files changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -1382,11 +1382,21 @@ static const char* php_phongo_bson_type_to_string(bson_type_t type) /* {{{ */
13821382
13831383static 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 (!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;
Original file line number Diff line number Diff 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
5159Failed to parse URI options: 'SCRAM-SHA-1' authentication mechanism requires username.
5260OK: Got MongoDB\Driver\Exception\InvalidArgumentException
5361Failed 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===
You can’t perform that action at this time.
0 commit comments