Skip to content

Commit

Permalink
Retrieve username from URI when RedisURI is built from URL #1242
Browse files Browse the repository at this point in the history
  • Loading branch information
gkorland authored and mp911de committed Mar 21, 2020
1 parent dedb57e commit 71632e3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/main/java/io/lettuce/core/RedisURI.java
Original file line number Diff line number Diff line change
Expand Up @@ -567,17 +567,23 @@ private static RedisURI buildRedisUriFromUri(URI uri) {

if (isNotEmpty(userInfo)) {
String password = userInfo;
String username = null;
if (password.startsWith(":")) {
password = password.substring(1);
} else {

int index = password.indexOf(':');
if (index > 0) {
username = password.substring(0, index);
password = password.substring(index + 1);
}
}
if (LettuceStrings.isNotEmpty(password)) {
builder.withPassword(password);
if(username == null) {
builder.withPassword(password);
} else {
builder.withAuthentication(username, password);
}
}
}

Expand Down Expand Up @@ -654,6 +660,9 @@ private String getAuthority(String scheme) {
if (password != null && password.length != 0) {
authority = urlEncode(new String(password)) + "@" + authority;
}
if (username != null) {
authority = urlEncode(username) + ":" + authority;
}
return authority;
}

Expand Down
4 changes: 4 additions & 0 deletions src/test/java/io/lettuce/core/RedisURIBuilderUnitTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,18 @@ void redisFromUrl() {
void redisFromUrlNoPassword() {
RedisURI redisURI = RedisURI.create("redis://localhost:1234/5");
assertThat(redisURI.getPassword()).isNull();
assertThat(redisURI.getUsername()).isNull();

redisURI = RedisURI.create("redis://h:@localhost.com:14589");
assertThat(redisURI.getPassword()).isNull();
assertThat(redisURI.getUsername()).isNull();
}

@Test
void redisFromUrlPassword() {
RedisURI redisURI = RedisURI.create("redis://h:password@localhost.com:14589");
assertThat(redisURI.getPassword()).isEqualTo("password".toCharArray());
assertThat(redisURI.getUsername()).isEqualTo("h");
}

@Test
Expand All @@ -141,6 +144,7 @@ void redisSslFromUrl() {
assertThat(result.getHost()).isEqualTo("localhost");
assertThat(result.getPort()).isEqualTo(RedisURI.DEFAULT_REDIS_PORT);
assertThat(result.getPassword()).isEqualTo("password".toCharArray());
assertThat(result.getUsername()).isNull();
assertThat(result.isSsl()).isTrue();
}

Expand Down

0 comments on commit 71632e3

Please sign in to comment.