Skip to content

Deserialize details field in UsernamePasswordAuthenticationToken #7660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ public UsernamePasswordAuthenticationToken deserialize(JsonParser jp, Deserializ
if (detailsNode.isNull() || detailsNode.isMissingNode()) {
token.setDetails(null);
} else {
token.setDetails(detailsNode);
Object details = mapper.readValue(detailsNode.toString(), new TypeReference<Object>() {});
token.setDetails(details);
}
return token;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public class UsernamePasswordAuthenticationTokenMixinTests extends AbstractMixin
+ "}";
// @formatter:on

// @formatter:off
private static final String AUTHENTICATED_STRINGDETAILS_JSON = AUTHENTICATED_JSON.replace("\"details\": null, ", "\"details\": \"details\", ");
// @formatter:on

// @formatter:off
private static final String AUTHENTICATED_NON_USER_PRINCIPAL_JSON = AUTHENTICATED_JSON
.replace(UserDeserializerTests.USER_JSON, NON_USER_PRINCIPAL_JSON)
Expand Down Expand Up @@ -155,6 +159,18 @@ public void deserializeAuthenticatedUsernamePasswordAuthenticationTokenWithNonUs
assertThat(token.getPrincipal()).isNotNull().isInstanceOf(NonUserPrincipal.class);
}

@Test
public void deserializeAuthenticatedUsernamePasswordAuthenticationTokenWithDetailsTest() throws IOException {
UsernamePasswordAuthenticationToken token = mapper
.readValue(AUTHENTICATED_STRINGDETAILS_JSON, UsernamePasswordAuthenticationToken.class);
assertThat(token).isNotNull();
assertThat(token.getPrincipal()).isNotNull().isInstanceOf(User.class);
assertThat(((User) token.getPrincipal()).getAuthorities()).isNotNull().hasSize(1).contains(new SimpleGrantedAuthority("ROLE_USER"));
assertThat(token.isAuthenticated()).isEqualTo(true);
assertThat(token.getAuthorities()).hasSize(1).contains(new SimpleGrantedAuthority("ROLE_USER"));
assertThat(token.getDetails()).isExactlyInstanceOf(String.class).isEqualTo("details");
}

@Test
public void serializingThenDeserializingWithNoCredentialsOrDetailsShouldWork() throws IOException {
// given
Expand Down