org.cloudfoundry.identity.uaa.account.EmailAccountCreationService Maven / Gradle / Ivy
The newest version!
package org.cloudfoundry.identity.uaa.account;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.cloudfoundry.identity.uaa.codestore.ExpiringCode;
import org.cloudfoundry.identity.uaa.codestore.ExpiringCodeStore;
import org.cloudfoundry.identity.uaa.constants.OriginKeys;
import org.cloudfoundry.identity.uaa.error.UaaException;
import org.cloudfoundry.identity.uaa.message.MessageService;
import org.cloudfoundry.identity.uaa.message.MessageType;
import org.cloudfoundry.identity.uaa.scim.ScimUser;
import org.cloudfoundry.identity.uaa.scim.ScimUserProvisioning;
import org.cloudfoundry.identity.uaa.scim.exception.ScimResourceAlreadyExistsException;
import org.cloudfoundry.identity.uaa.scim.util.ScimUtils;
import org.cloudfoundry.identity.uaa.scim.validate.PasswordValidator;
import org.cloudfoundry.identity.uaa.util.JsonUtils;
import org.cloudfoundry.identity.uaa.zone.ClientServicesExtension;
import org.cloudfoundry.identity.uaa.zone.IdentityZone;
import org.cloudfoundry.identity.uaa.zone.MergedZoneBrandingInformation;
import org.cloudfoundry.identity.uaa.zone.beans.IdentityZoneManager;
import org.springframework.http.HttpStatus;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.NoSuchClientException;
import org.springframework.util.StringUtils;
import org.springframework.web.client.HttpClientErrorException;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring4.SpringTemplateEngine;
import java.util.*;
import static org.cloudfoundry.identity.uaa.codestore.ExpiringCodeType.REGISTRATION;
import static org.cloudfoundry.identity.uaa.util.UaaUrlUtils.findMatchingRedirectUri;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
public class EmailAccountCreationService implements AccountCreationService {
private final Log logger = LogFactory.getLog(getClass());
public static final String SIGNUP_REDIRECT_URL = "signup_redirect_url";
private final SpringTemplateEngine templateEngine;
private final MessageService messageService;
private final ExpiringCodeStore codeStore;
private final ScimUserProvisioning scimUserProvisioning;
private final ClientServicesExtension clientDetailsService;
private final PasswordValidator passwordValidator;
private final IdentityZoneManager identityZoneManager;
public EmailAccountCreationService(
SpringTemplateEngine templateEngine,
MessageService messageService,
ExpiringCodeStore codeStore,
ScimUserProvisioning scimUserProvisioning,
ClientServicesExtension clientDetailsService,
PasswordValidator passwordValidator,
IdentityZoneManager identityZoneManager) {
this.templateEngine = templateEngine;
this.messageService = messageService;
this.codeStore = codeStore;
this.scimUserProvisioning = scimUserProvisioning;
this.clientDetailsService = clientDetailsService;
this.passwordValidator = passwordValidator;
this.identityZoneManager = identityZoneManager;
}
@Override
public void beginActivation(String email, String password, String clientId, String redirectUri) {
passwordValidator.validate(password);
String subject = buildSubjectText();
try {
ScimUser scimUser = createUser(email, password, OriginKeys.UAA);
generateAndSendCode(email, clientId, subject, scimUser.getId(), redirectUri, identityZoneManager.getCurrentIdentityZone());
} catch (ScimResourceAlreadyExistsException e) {
List users = scimUserProvisioning.query("userName eq \"" + email + "\" and origin eq \"" + OriginKeys.UAA + "\"", identityZoneManager.getCurrentIdentityZoneId());
if (users.size() > 0) {
if (users.get(0).isVerified()) {
throw new UaaException("User already active.", HttpStatus.CONFLICT.value());
} else {
generateAndSendCode(email, clientId, subject, users.get(0).getId(), redirectUri, identityZoneManager.getCurrentIdentityZone());
}
}
}
}
private void generateAndSendCode(
String email,
String clientId,
String subject,
String userId,
String redirectUri,
IdentityZone currentIdentityZone) {
ExpiringCode expiringCode = ScimUtils.getExpiringCode(
codeStore,
userId,
email,
clientId,
redirectUri,
REGISTRATION,
identityZoneManager.getCurrentIdentityZoneId());
String htmlContent = getEmailHtml(expiringCode.getCode(), email, currentIdentityZone);
messageService.sendMessage(email, MessageType.CREATE_ACCOUNT_CONFIRMATION, subject, htmlContent);
}
@Override
public AccountCreationResponse completeActivation(String code) {
ExpiringCode expiringCode = codeStore.retrieveCode(code, identityZoneManager.getCurrentIdentityZoneId());
if ((null == expiringCode) || ((null != expiringCode.getIntent()) && !REGISTRATION.name().equals(expiringCode.getIntent()))) {
throw new HttpClientErrorException(BAD_REQUEST);
}
Map data = JsonUtils.readValue(expiringCode.getData(), new TypeReference