Skip to content

Commit

Permalink
Merge pull request #71 from toddkazakov/uri-encode
Browse files Browse the repository at this point in the history
URI encode username path segment
  • Loading branch information
daniel-frak committed Dec 6, 2022
2 parents 27d2529 + 6328b81 commit 64e2fb3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.danielfrak.code.keycloak.providers.rest.rest.http.HttpClient;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpStatus;
import org.keycloak.common.util.Encode;
import org.keycloak.component.ComponentModel;

import java.io.IOException;
Expand Down Expand Up @@ -68,6 +69,9 @@ public Optional<LegacyUser> findByUsername(String username) {
}

private Optional<LegacyUser> findLegacyUser(String usernameOrEmail) {
if (usernameOrEmail != null) {
usernameOrEmail = Encode.urlEncode(usernameOrEmail);
}
var getUsernameUri = String.format("%s/%s", this.uri, usernameOrEmail);
try {
var response = this.httpClient.get(getUsernameUri);
Expand All @@ -83,6 +87,9 @@ private Optional<LegacyUser> findLegacyUser(String usernameOrEmail) {

@Override
public boolean isPasswordValid(String username, String password) {
if (username != null) {
username = Encode.urlEncode(username);
}
var passwordValidationUri = String.format("%s/%s", this.uri, username);
var dto = new UserPasswordDto(password);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.keycloak.common.util.Encode;
import org.keycloak.common.util.MultivaluedHashMap;
import org.keycloak.component.ComponentModel;
import org.mockito.Mock;
Expand Down Expand Up @@ -118,7 +119,7 @@ void findByEmailShouldThrowWhenIOExceptionOccurs() {
void findByEmailShouldReturnAUserWhenUserIsFoundAndEmailMatches() throws IOException {
var expectedUser = createALegacyUser("someUsername", "email@example.com");
var response = new HttpResponse(HttpStatus.SC_OK, objectMapper.writeValueAsString(expectedUser));
var path = String.format(URI_PATH_FORMAT, URI, expectedUser.getEmail());
var path = String.format(URI_PATH_FORMAT, URI, Encode.urlEncode(expectedUser.getEmail()));
var restUserService = new RestUserService(model, httpClient, new ObjectMapper());

when(httpClient.get(path)).thenReturn(response);
Expand All @@ -133,7 +134,7 @@ void findByEmailShouldReturnAUserWhenUserIsFoundAndEmailMatches() throws IOExcep
void findByEmailShouldReturnAUserWhenUserIsFoundAndEmailMatchesCaseInsensitive() throws IOException {
var expectedUser = createALegacyUser("someUsername", "email@example.com");
var response = new HttpResponse(HttpStatus.SC_OK, objectMapper.writeValueAsString(expectedUser));
var path = String.format(URI_PATH_FORMAT, URI, "EMAIL@EXAMPLE.COM");
var path = String.format(URI_PATH_FORMAT, URI, Encode.urlEncode("EMAIL@EXAMPLE.COM"));
var restUserService = new RestUserService(model, httpClient, new ObjectMapper());

when(httpClient.get(path)).thenReturn(response);
Expand Down Expand Up @@ -163,7 +164,7 @@ private LegacyUser createALegacyUser(String username, String email) {
@Test
void findByEmailShouldReturnAnEmptyOptionalWhenUserIsNotFound() {
var expectedUser = createALegacyUser("someUsername", "email@example.com");
var path = String.format(URI_PATH_FORMAT, URI, expectedUser.getEmail());
var path = String.format(URI_PATH_FORMAT, URI, Encode.urlEncode(expectedUser.getEmail()));
var response = new HttpResponse(HttpStatus.SC_NOT_FOUND);
var restUserService = new RestUserService(model, httpClient, new ObjectMapper());
when(httpClient.get(path)).thenReturn(response);
Expand Down Expand Up @@ -248,6 +249,20 @@ void findByUsernameShouldReturnAUserWhenUserIsFoundAndUsernameMatchesCaseInsensi
assertEquals(result.get(), expectedUser);
}

@Test
void findByUsernameShouldReturnAUserWhenUserIsFoundAndUsernameContainsSpace() throws IOException {
var expectedUser = createALegacyUser("some Username", "email@example.com");
var path = String.format(URI_PATH_FORMAT, URI, "SOME+USERNAME");
var response = new HttpResponse(HttpStatus.SC_OK, objectMapper.writeValueAsString(expectedUser));
var restUserService = new RestUserService(model, httpClient, new ObjectMapper());
when(httpClient.get(path)).thenReturn(response);

var result = restUserService.findByUsername("SOME USERNAME");

assertTrue(result.isPresent());
assertEquals(result.get(), expectedUser);
}

@Test
void findByUsernameShouldReturnAnEmptyOptionalWhenUserIsNotFound() {
var expectedUser = createALegacyUser("someUsername", "email@example.com");
Expand Down Expand Up @@ -310,6 +325,36 @@ void isPasswordValidShouldReturnTrueWhenPasswordsMatches() throws IOException {
assertTrue(isPasswordValid);
}

@Test
void isPasswordValidShouldNotThrowNullPointerExceptionWhenPasswordIsNull() throws IOException {
String username = null;
var password = "anyPassword";
var path = String.format(URI_PATH_FORMAT, URI, username);
var restUserService = new RestUserService(model, httpClient, new ObjectMapper());
var response = new HttpResponse(HttpStatus.SC_OK);
var expectedBody = objectMapper.writeValueAsString(new UserPasswordDto(password));
when(httpClient.post(path, expectedBody)).thenReturn(response);

var isPasswordValid = restUserService.isPasswordValid(username, password);

assertTrue(isPasswordValid);
}

@Test
void isPasswordValidShouldReturnTrueWhenPasswordsMatchesAndUsernameContainsSpace() throws IOException {
var username = "some Username";
var password = "anyPassword";
var path = String.format(URI_PATH_FORMAT, URI, "some+Username");
var restUserService = new RestUserService(model, httpClient, new ObjectMapper());
var response = new HttpResponse(HttpStatus.SC_OK);
var expectedBody = objectMapper.writeValueAsString(new UserPasswordDto(password));
when(httpClient.post(path, expectedBody)).thenReturn(response);

var isPasswordValid = restUserService.isPasswordValid(username, password);

assertTrue(isPasswordValid);
}

@Test
void isPasswordValidShouldReturnFalseWhenPasswordsDoNotMatch() {
var username = "someUsername";
Expand Down

0 comments on commit 64e2fb3

Please sign in to comment.