Skip to content
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

fix: generate email if it is the empty string on external login #2868

Merged
merged 1 commit into from
May 29, 2024
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 @@ -245,8 +245,8 @@ protected UaaUser getUser(Authentication request, ExternalAuthenticationDetails
}
}

if (email == null) {
email = generateEmailIfNull(name);
if (StringUtils.isEmpty(email)) {
email = generateEmailIfNullOrEmpty(name);
}

String givenName = null;
Expand Down Expand Up @@ -278,7 +278,7 @@ protected UaaUser getUser(Authentication request, ExternalAuthenticationDetails
return new UaaUser(userPrototype);
}

protected String generateEmailIfNull(String name) {
protected String generateEmailIfNullOrEmpty(String name) {
String email;
if (name != null) {
if (name.contains("@")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,8 @@ protected UaaUser getUser(Authentication request, AuthenticationData authenticat
Object verifiedObj = claims.get(emailVerifiedClaim == null ? "email_verified" : emailVerifiedClaim);
boolean verified = verifiedObj instanceof Boolean ? (Boolean)verifiedObj: false;

if (email == null) {
email = generateEmailIfNull(username);
if (!StringUtils.hasText(email)) {
Copy link
Member

@peterhaochen47 peterhaochen47 May 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use !StringUtils.hasText(email) here but StringUtils.isEmpty(email) in ExternalLoginAuthenticationManager.java? Why the different logics?

It looks like !StringUtils.hasText is a narrower/stricter validation? It also evaluates to true if the string contains only whitespace(s).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only because this one uses the Spring StringUtils and isEmpty is deprecated. ExternalLoginAuthenticationManager uses the Apache StringUtils. I didn't want to affect other lines of code by changing one or the other, and I didn't want to use a deprecated method.

email = generateEmailIfNullOrEmpty(username);
}

logger.debug(String.format("Returning user data for username:%s, email:%s", username, email));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,38 @@ public void testAmpersandInEndOfName() {

}

@Test
public void testEmptyEmail() {
String name = "filip";
String actual = name + "@user.from."+origin+".cf";
String email = "";
userDetails = mock(UserDetails.class, withSettings().extraInterfaces(Mailable.class));
when(((Mailable)userDetails).getEmailAddress()).thenReturn(email);
mockUserDetails(userDetails);
mockUaaWithUser();

when(userDetails.getUsername()).thenReturn(name);
when(user.getUsername()).thenReturn(name);
when(uaaUserDatabase.retrieveUserByName(eq(name),eq(origin)))
.thenReturn(null)
.thenReturn(user);

Authentication result = manager.authenticate(inputAuth);
assertNotNull(result);
assertEquals(UaaAuthentication.class, result.getClass());
UaaAuthentication uaaAuthentication = (UaaAuthentication)result;
assertEquals(name,uaaAuthentication.getPrincipal().getName());
assertEquals(origin, uaaAuthentication.getPrincipal().getOrigin());
assertEquals(userId, uaaAuthentication.getPrincipal().getId());

userArgumentCaptor = ArgumentCaptor.forClass(ApplicationEvent.class);
verify(applicationEventPublisher,times(2)).publishEvent(userArgumentCaptor.capture());
assertEquals(2,userArgumentCaptor.getAllValues().size());
NewUserAuthenticatedEvent event = (NewUserAuthenticatedEvent)userArgumentCaptor.getAllValues().get(0);
assertEquals(origin, event.getUser().getOrigin());
assertEquals(actual, event.getUser().getEmail());
}

@Test(expected = BadCredentialsException.class)
public void testAuthenticateUserInsertFails() {
when(uaaUserDatabase.retrieveUserByName(anyString(),anyString())).thenThrow(new UsernameNotFoundException(""));
Expand Down
Loading