@@ -242,38 +242,47 @@ internal class ConnectionString {
242242
243243 /// Sets and validates authentication-related on the underlying `mongoc_uri_t`.
244244 private func applyAndValidateAuthOptions( _ options: MongoClientOptions ? ) throws {
245- guard let credential = options? . credential else {
246- return
247- }
245+ if let credential = options? . credential {
246+ if let username = credential. username {
247+ guard mongoc_uri_set_username ( self . _uri, username) else {
248+ throw self . failedToSet ( " username " , to: username)
249+ }
250+ }
248251
249- if let username = credential. username {
250- guard mongoc_uri_set_username ( self . _uri, username) else {
251- throw self . failedToSet ( " username " , to: username)
252+ if let password = credential. password {
253+ guard mongoc_uri_set_password ( self . _uri, password) else {
254+ throw MongoError . InvalidArgumentError ( message: " Failed to set password " )
255+ }
252256 }
253- }
254257
255- if let password = credential. password {
256- guard mongoc_uri_set_password ( self . _uri, password) else {
257- throw MongoError . InvalidArgumentError ( message: " Failed to set password " )
258+ if let authSource = credential. source {
259+ guard mongoc_uri_set_auth_source ( self . _uri, authSource) else {
260+ throw self . failedToSet ( MONGOC_URI_AUTHSOURCE, to: authSource)
261+ }
258262 }
259- }
260263
261- if let authSource = credential. source {
262- guard mongoc_uri_set_auth_source ( self . _uri, authSource) else {
263- throw self . failedToSet ( MONGOC_URI_AUTHSOURCE, to: authSource)
264+ if let mechanism = credential. mechanism {
265+ guard mongoc_uri_set_auth_mechanism ( self . _uri, mechanism. name) else {
266+ throw self . failedToSet ( MONGOC_URI_AUTHMECHANISM, to: mechanism)
267+ }
264268 }
265- }
266269
267- if let mechanism = credential. mechanism {
268- guard mongoc_uri_set_auth_mechanism ( self . _uri, mechanism. name) else {
269- throw self . failedToSet ( MONGOC_URI_AUTHMECHANISM, to: mechanism)
270+ try credential. mechanismProperties? . withBSONPointer { mechanismPropertiesPtr in
271+ guard mongoc_uri_set_mechanism_properties ( self . _uri, mechanismPropertiesPtr) else {
272+ let desc = String ( describing: credential. mechanismProperties)
273+ throw self . failedToSet ( MONGOC_URI_AUTHMECHANISMPROPERTIES, to: desc)
274+ }
270275 }
271276 }
272277
273- try credential. mechanismProperties? . withBSONPointer { mechanismPropertiesPtr in
274- guard mongoc_uri_set_mechanism_properties ( self . _uri, mechanismPropertiesPtr) else {
275- let desc = String ( describing: credential. mechanismProperties)
276- throw self . failedToSet ( MONGOC_URI_AUTHMECHANISMPROPERTIES, to: desc)
278+ // libmongoc incorrectly permits an empty string as auth source. this logic could be removed once CDRIVER-3517
279+ // is complete (Swift work tracked by SWIFT-1298), however we are likely to be using the pure Swift URI parser
280+ // by the time that happens anyway.
281+ if let authSource = self . authSource {
282+ guard !authSource. isEmpty else {
283+ throw MongoError . InvalidArgumentError (
284+ message: " \( MONGOC_URI_AUTHSOURCE) cannot be set to an empty string "
285+ )
277286 }
278287 }
279288 }
0 commit comments