Skip to content

Commit 1aa2ee5

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-1713 - Allow using URL encoded username/password for <mongo-client credentials=… />.
We now URL decode username & password before creating MongoCredentials. This allows usage of reserved characters in the credentials string. Original pull request: spring-projects#477.
1 parent b9282c8 commit 1aa2ee5

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditor.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616
package org.springframework.data.mongodb.config;
1717

1818
import java.beans.PropertyEditorSupport;
19+
import java.io.UnsupportedEncodingException;
20+
import java.net.URLDecoder;
1921
import java.util.ArrayList;
2022
import java.util.Arrays;
2123
import java.util.List;
@@ -136,7 +138,12 @@ private static String[] extractUserNameAndPassword(String text) {
136138

137139
index = index != -1 ? index : text.lastIndexOf(OPTIONS_DELIMINATOR);
138140

139-
return index == -1 ? new String[] {} : text.substring(0, index).split(USERNAME_PASSWORD_DELIMINATOR);
141+
if (index == -1) {
142+
return new String[] {};
143+
}
144+
145+
return Arrays.asList(text.substring(0, index).split(USERNAME_PASSWORD_DELIMINATOR)).stream()
146+
.map(MongoCredentialPropertyEditor::decodeParameter).toArray(String[]::new);
140147
}
141148

142149
private static String extractDB(String text) {
@@ -195,4 +202,12 @@ private static void verifyUserNamePresent(String[] source) {
195202
throw new IllegalArgumentException("Credentials need to specify username!");
196203
}
197204
}
205+
206+
private static String decodeParameter(String it) {
207+
try {
208+
return URLDecoder.decode(it, "UTF-8");
209+
} catch (UnsupportedEncodingException e) {
210+
throw new IllegalArgumentException("o_O UTF-8 not supported!", e);
211+
}
212+
}
198213
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditorUnitTests.java

+39
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import static org.hamcrest.core.IsNull.*;
2020
import static org.junit.Assert.*;
2121

22+
import java.io.UnsupportedEncodingException;
23+
import java.net.URLEncoder;
2224
import java.util.Arrays;
2325
import java.util.List;
2426

@@ -46,6 +48,12 @@ public class MongoCredentialPropertyEditorUnitTests {
4648
static final String USER_3_NAME = "CN=myName,OU=myOrgUnit,O=myOrg,L=myLocality,ST=myState,C=myCountry";
4749
static final String USER_3_DB = "stark";
4850

51+
static final String USER_4_PLAIN_NAME = "m0ng0@dmin";
52+
static final String USER_4_ENCODED_NAME;
53+
static final String USER_4_PLAIN_PWD = "mo_res:bw6},Qsdxx@admin";
54+
static final String USER_4_ENCODED_PWD;
55+
static final String USER_4_DB = "targaryen";
56+
4957
static final String USER_1_AUTH_STRING = USER_1_NAME + ":" + USER_1_PWD + "@" + USER_1_DB;
5058
static final String USER_1_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM = USER_1_AUTH_STRING + "?uri.authMechanism=PLAIN";
5159

@@ -56,6 +64,8 @@ public class MongoCredentialPropertyEditorUnitTests {
5664
static final String USER_3_AUTH_STRING_WITH_X509_AUTH_MECHANISM = "'" + USER_3_NAME + "@" + USER_3_DB
5765
+ "?uri.authMechanism=MONGODB-X509'";
5866

67+
static final String USER_4_AUTH_STRING;
68+
5969
static final MongoCredential USER_1_CREDENTIALS = MongoCredential.createCredential(USER_1_NAME, USER_1_DB,
6070
USER_1_PWD.toCharArray());
6171
static final MongoCredential USER_1_CREDENTIALS_PLAIN_AUTH = MongoCredential.createPlainCredential(USER_1_NAME,
@@ -68,8 +78,26 @@ public class MongoCredentialPropertyEditorUnitTests {
6878

6979
static final MongoCredential USER_3_CREDENTIALS_X509_AUTH = MongoCredential.createMongoX509Credential(USER_3_NAME);
7080

81+
static final MongoCredential USER_4_CREDENTIALS = MongoCredential.createCredential(USER_4_PLAIN_NAME, USER_4_DB,
82+
USER_4_PLAIN_PWD.toCharArray());
83+
7184
MongoCredentialPropertyEditor editor;
7285

86+
static {
87+
88+
String encodedUserName = null;
89+
String encodedUserPassword = null;
90+
try {
91+
encodedUserName = URLEncoder.encode(USER_4_PLAIN_NAME, "UTF-8");
92+
encodedUserPassword = URLEncoder.encode(USER_4_PLAIN_PWD, "UTF-8");
93+
} catch (UnsupportedEncodingException e) {}
94+
95+
USER_4_ENCODED_NAME = encodedUserName;
96+
USER_4_ENCODED_PWD = encodedUserPassword;
97+
USER_4_AUTH_STRING = USER_4_ENCODED_NAME + ":" + USER_4_ENCODED_PWD + "@" + USER_4_DB;
98+
99+
}
100+
73101
@Before
74102
public void setUp() {
75103
this.editor = new MongoCredentialPropertyEditor();
@@ -202,4 +230,15 @@ public void shouldThrowExceptionWhenDbIsEmptyForMongodbCR() {
202230

203231
editor.getValue();
204232
}
233+
234+
@Test // DATAMONGO-1317
235+
@SuppressWarnings("unchecked")
236+
public void encodedUserNameAndPasswrodShouldBeDecoded() throws UnsupportedEncodingException {
237+
238+
editor.setAsText(USER_4_AUTH_STRING);
239+
240+
System.out.println("USER_4_AUTH_STRING: " + USER_4_AUTH_STRING);
241+
242+
assertThat((List<MongoCredential>) editor.getValue(), contains(USER_4_CREDENTIALS));
243+
}
205244
}

src/main/asciidoc/reference/mongodb.adoc

+3
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,9 @@ public class ApplicationContextEventTestsAppConfig extends AbstractMongoConfigur
362362

363363
In order to use authentication with XML configuration use the `credentials` attribue on `<mongo-client>`.
364364

365+
NOTE: When using XML configuration along with username/password containing reserved characters `:`, `@`, `,` need to be URL encoded.
366+
Example: `m0ng0@dmin:mo_res:bw6},Qsdxx@admin@database` -> `m0ng0%40dmin:mo_res%3Abw6%7D%2CQsdxx%40admin@database`
367+
365368

366369
[[mongo.mongo-db-factory-xml]]
367370
=== Registering a MongoDbFactory instance using XML based metadata

0 commit comments

Comments
 (0)