Skip to content

Commit

Permalink
Fixed bug where creating an account without a client_id wouldn't
Browse files Browse the repository at this point in the history
redirect correctly afterwards.

Clients with empty ID strings are handled correctly when generating
activation emails.

Creating accouts in subdomains with and without clients now works.

Changed activation email to show identity zone name (if there is one).

[Finishes #82406674, Fixes #83898390]

Conflicts:
	login/src/main/java/org/cloudfoundry/identity/uaa/login/EmailAccountCreationService.java
	uaa/src/test/java/org/cloudfoundry/identity/uaa/login/AccountsControllerIntegrationTest.java
  • Loading branch information
Rob Szumlakowski authored and Chris Dutra committed Feb 5, 2015
1 parent 406965b commit fcd17fb
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 10 deletions.
Expand Up @@ -48,14 +48,14 @@ public AccountsController(AccountCreationService accountCreationService) {

@RequestMapping(value = "/create_account", method = GET)
public String activationEmail(Model model,
@RequestParam(value = "client_id", defaultValue = "login") String clientId) {
@RequestParam(value = "client_id", required = false) String clientId) {
model.addAttribute("client_id", clientId);
return "accounts/new_activation_email";
}

@RequestMapping(value = "/create_account.do", method = POST)
public String sendActivationEmail(Model model, HttpServletResponse response,
@RequestParam("client_id") String clientId,
@RequestParam(value = "client_id", required = false) String clientId,
@Valid @ModelAttribute("email") ValidEmail email, BindingResult result,
@RequestParam("password") String password,
@RequestParam("password_confirmation") String passwordConfirmation) {
Expand Down
Expand Up @@ -14,16 +14,13 @@
import org.springframework.http.HttpStatus;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.security.oauth2.provider.NoSuchClientException;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring4.SpringTemplateEngine;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -122,12 +119,28 @@ public AccountCreationResponse completeActivation(String code) throws IOExceptio
Map<String, String> data = objectMapper.readValue(expiringCode.getData(), new TypeReference<Map<String, String>>() {});
ScimUser user = scimUserProvisioning.retrieve(data.get("user_id"));
user = scimUserProvisioning.verifyUser(user.getId(), user.getVersion());
ClientDetails clientDetails = clientDetailsService.loadClientByClientId(data.get("client_id"));
String redirectLocation = (String) clientDetails.getAdditionalInformation().get(SIGNUP_REDIRECT_URL);

String clientId = data.get("client_id");
String redirectLocation;
if (clientId != null) {
try {
ClientDetails clientDetails = clientDetailsService.loadClientByClientId(clientId);
redirectLocation = (String) clientDetails.getAdditionalInformation().get(SIGNUP_REDIRECT_URL);
}
catch (NoSuchClientException e) {
redirectLocation = getDefaultRedirect();
}
} else {
redirectLocation = getDefaultRedirect();
}

return new AccountCreationResponse(user.getId(), user.getUserName(), user.getUserName(), redirectLocation);
}

private String getDefaultRedirect() throws IOException {
return "home";
}

@Override
public void resendVerificationCode(String email, String clientId) {
List<ScimUser> resources = scimUserProvisioning.query("userName eq \"" + email + "\" and origin eq \"" + Origin.UAA + "\"");
Expand All @@ -138,8 +151,7 @@ public void resendVerificationCode(String email, String clientId) {
logger.error("Exception raised while resending activation email for " + email, e);
}
}



@Override
public ScimUser createUser(String username, String password) {
ScimUser scimUser = new ScimUser();
Expand Down
Expand Up @@ -124,6 +124,63 @@ public void testCreatingAnAccount() throws Exception {
JdbcExpiringCodeStore store = webApplicationContext.getBean(JdbcExpiringCodeStore.class);
store.setGenerator(generator);

mockMvc.perform(post("/create_account.do")
.param("email", userEmail)
.param("password", "secret")
.param("password_confirmation", "secret"))
.andExpect(status().isFound())
.andExpect(redirectedUrl("accounts/email_sent"));

MvcResult mvcResult = mockMvc.perform(get("/verify_user")
.param("code", "test"+generator.counter.get()))
.andDo(print())
.andExpect(status().isFound())
.andExpect(redirectedUrl("home"))
.andReturn();

SecurityContext securityContext = (SecurityContext) mvcResult.getRequest().getSession().getAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
Authentication authentication = securityContext.getAuthentication();
Assert.assertThat(authentication.getPrincipal(), instanceOf(UaaPrincipal.class));
UaaPrincipal principal = (UaaPrincipal) authentication.getPrincipal();
Assert.assertThat(principal.getEmail(), equalTo(userEmail));
Assert.assertThat(principal.getOrigin(), equalTo(Origin.UAA));
}

@Test
public void testCreatingAnAccountWithAnEmptyClientId() throws Exception {
PredictableGenerator generator = new PredictableGenerator();
JdbcExpiringCodeStore store = webApplicationContext.getBean(JdbcExpiringCodeStore.class);
store.setGenerator(generator);

mockMvc.perform(post("/create_account.do")
.param("email", userEmail)
.param("password", "secret")
.param("password_confirmation", "secret")
.param("client_id", ""))
.andExpect(status().isFound())
.andExpect(redirectedUrl("accounts/email_sent"));

MvcResult mvcResult = mockMvc.perform(get("/verify_user")
.param("code", "test"+generator.counter.get()))
.andDo(print())
.andExpect(status().isFound())
.andExpect(redirectedUrl("home"))
.andReturn();

SecurityContext securityContext = (SecurityContext) mvcResult.getRequest().getSession().getAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
Authentication authentication = securityContext.getAuthentication();
Assert.assertThat(authentication.getPrincipal(), instanceOf(UaaPrincipal.class));
UaaPrincipal principal = (UaaPrincipal) authentication.getPrincipal();
Assert.assertThat(principal.getEmail(), equalTo(userEmail));
Assert.assertThat(principal.getOrigin(), equalTo(Origin.UAA));
}

@Test
public void testCreatingAnAccountWithClientRedirect() throws Exception {
PredictableGenerator generator = new PredictableGenerator();
JdbcExpiringCodeStore store = webApplicationContext.getBean(JdbcExpiringCodeStore.class);
store.setGenerator(generator);

mockMvc.perform(post("/create_account.do")
.param("email", userEmail)
.param("password", "secret")
Expand Down

0 comments on commit fcd17fb

Please sign in to comment.