@@ -931,179 +931,6 @@ _cluster_run_hello (mongoc_cluster_t *cluster,
931931}
932932
933933
934- /*
935- *--------------------------------------------------------------------------
936- *
937- * _mongoc_cluster_build_basic_auth_digest --
938- *
939- * Computes the Basic Authentication digest using the credentials
940- * configured for @cluster and the @nonce provided.
941- *
942- * The result should be freed by the caller using bson_free() when
943- * they are finished with it.
944- *
945- * Returns:
946- * A newly allocated string containing the digest.
947- *
948- * Side effects:
949- * None.
950- *
951- *--------------------------------------------------------------------------
952- */
953-
954- static char *
955- _mongoc_cluster_build_basic_auth_digest (mongoc_cluster_t * cluster , const char * nonce )
956- {
957- const char * username ;
958- const char * password ;
959- char * password_digest ;
960- char * password_md5 ;
961- char * digest_in ;
962- char * ret ;
963-
964- ENTRY ;
965-
966- /*
967- * The following generates the digest to be used for basic authentication
968- * with a MongoDB server. More information on the format can be found
969- * at the following location:
970- *
971- * https://www.mongodb.com/docs/meta-driver/latest/legacy/
972- * implement-authentication-in-driver/
973- */
974-
975- BSON_ASSERT (cluster );
976- BSON_ASSERT (cluster -> uri );
977-
978- username = mongoc_uri_get_username (cluster -> uri );
979- password = mongoc_uri_get_password (cluster -> uri );
980- password_digest = bson_strdup_printf ("%s:mongo:%s" , username , password );
981- password_md5 = _mongoc_hex_md5 (password_digest );
982- digest_in = bson_strdup_printf ("%s%s%s" , nonce , username , password_md5 );
983- ret = _mongoc_hex_md5 (digest_in );
984- bson_free (digest_in );
985- bson_free (password_md5 );
986- bson_free (password_digest );
987-
988- RETURN (ret );
989- }
990-
991-
992- /*
993- *--------------------------------------------------------------------------
994- *
995- * _mongoc_cluster_auth_node_cr --
996- *
997- * Performs authentication of @node using the credentials provided
998- * when configuring the @cluster instance.
999- *
1000- * This is the Challenge-Response mode of authentication.
1001- *
1002- * Returns:
1003- * true if authentication was successful; otherwise false and
1004- * @error is set.
1005- *
1006- * Side effects:
1007- * None.
1008- *
1009- *--------------------------------------------------------------------------
1010- */
1011-
1012- static bool
1013- _mongoc_cluster_auth_node_cr (mongoc_cluster_t * cluster ,
1014- mongoc_stream_t * stream ,
1015- mongoc_server_description_t * sd ,
1016- bson_error_t * error )
1017- {
1018- mongoc_cmd_parts_t parts ;
1019- bson_iter_t iter ;
1020- const char * auth_source ;
1021- bson_t command ;
1022- bson_t reply ;
1023- char * digest ;
1024- char * nonce ;
1025- bool ret ;
1026- mongoc_server_stream_t * server_stream ;
1027- mc_shared_tpld td ;
1028-
1029- ENTRY ;
1030-
1031- BSON_ASSERT (cluster );
1032- BSON_ASSERT (stream );
1033-
1034- if (!(auth_source = mongoc_uri_get_auth_source (cluster -> uri )) || (* auth_source == '\0' )) {
1035- auth_source = "admin" ;
1036- }
1037-
1038- /*
1039- * To authenticate a node using basic authentication, we need to first
1040- * get the nonce from the server. We use that to hash our password which
1041- * is sent as a reply to the server. If everything went good we get a
1042- * success notification back from the server.
1043- */
1044-
1045- /*
1046- * Execute the getnonce command to fetch the nonce used for generating
1047- * md5 digest of our password information.
1048- */
1049- bson_init (& command );
1050- bson_append_int32 (& command , "getnonce" , 8 , 1 );
1051- mongoc_cmd_parts_init (& parts , cluster -> client , auth_source , MONGOC_QUERY_SECONDARY_OK , & command );
1052- parts .prohibit_lsid = true;
1053-
1054- td = mc_tpld_take_ref (cluster -> client -> topology );
1055- server_stream = _mongoc_cluster_create_server_stream (td .ptr , sd , stream );
1056- mc_tpld_drop_ref (& td );
1057-
1058- if (!mongoc_cluster_run_command_parts (cluster , server_stream , & parts , & reply , error )) {
1059- mongoc_server_stream_cleanup (server_stream );
1060- bson_destroy (& command );
1061- bson_destroy (& reply );
1062- RETURN (false);
1063- }
1064- bson_destroy (& command );
1065- if (!bson_iter_init_find_case (& iter , & reply , "nonce" )) {
1066- bson_set_error (error , MONGOC_ERROR_CLIENT , MONGOC_ERROR_CLIENT_GETNONCE , "Invalid reply from getnonce" );
1067- bson_destroy (& reply );
1068- RETURN (false);
1069- }
1070-
1071- /*
1072- * Build our command to perform the authentication.
1073- */
1074- nonce = bson_iter_dup_utf8 (& iter , NULL );
1075- digest = _mongoc_cluster_build_basic_auth_digest (cluster , nonce );
1076- bson_init (& command );
1077- bson_append_int32 (& command , "authenticate" , 12 , 1 );
1078- bson_append_utf8 (& command , "user" , 4 , mongoc_uri_get_username (cluster -> uri ), -1 );
1079- bson_append_utf8 (& command , "nonce" , 5 , nonce , -1 );
1080- bson_append_utf8 (& command , "key" , 3 , digest , -1 );
1081- bson_destroy (& reply );
1082- bson_free (nonce );
1083- bson_free (digest );
1084-
1085- /*
1086- * Execute the authenticate command. mongoc_cluster_run_command_private
1087- * checks for {ok: 1} in the response.
1088- */
1089- mongoc_cmd_parts_init (& parts , cluster -> client , auth_source , MONGOC_QUERY_SECONDARY_OK , & command );
1090- parts .prohibit_lsid = true;
1091- ret = mongoc_cluster_run_command_parts (cluster , server_stream , & parts , & reply , error );
1092-
1093- if (!ret ) {
1094- /* error->message is already set */
1095- error -> domain = MONGOC_ERROR_CLIENT ;
1096- error -> code = MONGOC_ERROR_CLIENT_AUTHENTICATE ;
1097- }
1098-
1099- mongoc_server_stream_cleanup (server_stream );
1100- bson_destroy (& command );
1101- bson_destroy (& reply );
1102-
1103- RETURN (ret );
1104- }
1105-
1106-
1107934/*
1108935 *--------------------------------------------------------------------------
1109936 *
@@ -1779,9 +1606,7 @@ _mongoc_cluster_auth_node (mongoc_cluster_t *cluster,
17791606 }
17801607 }
17811608
1782- if (0 == strcasecmp (mechanism , "MONGODB-CR" )) {
1783- ret = _mongoc_cluster_auth_node_cr (cluster , stream , sd , error );
1784- } else if (0 == strcasecmp (mechanism , "MONGODB-X509" )) {
1609+ if (0 == strcasecmp (mechanism , "MONGODB-X509" )) {
17851610 ret = _mongoc_cluster_auth_node_x509 (cluster , stream , sd , error );
17861611 } else if (0 == strcasecmp (mechanism , "SCRAM-SHA-1" )) {
17871612 ret = _mongoc_cluster_auth_node_scram_sha_1 (cluster , stream , sd , error );
0 commit comments