Allow empty usernames for BasicAuth

The RFCs around basic authentication don't explicitly disallow empty
usernames. On the other hand usernames containing colons are, as colons
are used to separate the username from the password.
This commit is contained in:
Norman Soetbeer
2017-10-25 21:28:07 +02:00
committed by Rob Winch
parent abe4420006
commit 4dee333a75
2 changed files with 10 additions and 10 deletions

View File

@@ -46,9 +46,9 @@ public class BasicAuthorizationInterceptor implements ClientHttpRequestIntercept
* @param username the username to use * @param username the username to use
* @param password the password to use * @param password the password to use
*/ */
public BasicAuthorizationInterceptor(String username, @Nullable String password) { public BasicAuthorizationInterceptor(@Nullable String username, @Nullable String password) {
Assert.hasLength(username, "Username must not be empty"); Assert.doesNotContain(username, ":", "Username must not contain a colon");
this.username = username; this.username = (username != null ? username : "");
this.password = (password != null ? password : ""); this.password = (password != null ? password : "");
} }

View File

@@ -43,17 +43,17 @@ public class BasicAuthorizationInterceptorTests {
public ExpectedException thrown = ExpectedException.none(); public ExpectedException thrown = ExpectedException.none();
@Test @Test
public void createWhenUsernameIsNullShouldThrowException() { public void createWhenUsernameContainsColonShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class); this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Username must not be empty"); this.thrown.expectMessage("Username must not contain a colon");
new BasicAuthorizationInterceptor(null, "password"); new BasicAuthorizationInterceptor("username:", "password");
} }
@Test @Test
public void createWhenUsernameIsEmptyShouldThrowException() throws Exception { public void createWhenUsernameIsNullShouldUseEmptyUsername() throws Exception {
this.thrown.expect(IllegalArgumentException.class); BasicAuthorizationInterceptor interceptor = new BasicAuthorizationInterceptor(
this.thrown.expectMessage("Username must not be empty"); null, "password");
new BasicAuthorizationInterceptor("", "password"); assertEquals("", new DirectFieldAccessor(interceptor).getPropertyValue("username"));
} }
@Test @Test