use of io.gravitee.rest.api.service.common.JWTHelper.ACTION in project gravitee-management-rest-api by gravitee-io.
the class UserServiceImpl method finalizeRegistration.
/**
* Allows to complete the creation of a user which is pre-created.
* @param registerUserEntity a valid token and a password
* @return the user
*/
@Override
public UserEntity finalizeRegistration(final RegisterUserEntity registerUserEntity) {
try {
DecodedJWT jwt = getDecodedJWT(registerUserEntity.getToken());
final String action = jwt.getClaim(Claims.ACTION).asString();
if (RESET_PASSWORD.name().equals(action)) {
throw new UserStateConflictException("Reset password forbidden on this resource");
}
if (USER_REGISTRATION.name().equals(action)) {
checkUserRegistrationEnabled(GraviteeContext.getCurrentContext());
} else if (GROUP_INVITATION.name().equals(action)) {
// check invitations
final String email = jwt.getClaim(Claims.EMAIL).asString();
final List<InvitationEntity> invitations = invitationService.findAll();
final List<InvitationEntity> userInvitations = invitations.stream().filter(invitation -> invitation.getEmail().equals(email)).collect(toList());
if (userInvitations.isEmpty()) {
throw new IllegalStateException("Invitation has been canceled");
}
}
// check password here to avoid user creation if password is invalid
if (registerUserEntity.getPassword() != null) {
if (!passwordValidator.validate(registerUserEntity.getPassword())) {
throw new PasswordFormatInvalidException();
}
}
final Object subject = jwt.getSubject();
User user;
if (subject == null) {
final NewExternalUserEntity externalUser = new NewExternalUserEntity();
final String email = jwt.getClaim(Claims.EMAIL).asString();
externalUser.setSource(IDP_SOURCE_GRAVITEE);
externalUser.setSourceId(email);
externalUser.setFirstname(registerUserEntity.getFirstname());
externalUser.setLastname(registerUserEntity.getLastname());
externalUser.setEmail(email);
user = convert(create(externalUser, true));
user.setOrganizationId(GraviteeContext.getCurrentOrganization());
} else {
final String username = subject.toString();
LOGGER.debug("Create an internal user {}", username);
Optional<User> checkUser = userRepository.findById(username);
user = checkUser.orElseThrow(() -> new UserNotFoundException(username));
if (StringUtils.isNotBlank(user.getPassword())) {
throw new UserAlreadyFinalizedException(GraviteeContext.getCurrentOrganization());
}
}
if (GROUP_INVITATION.name().equals(action)) {
// check invitations
final String email = user.getEmail();
final String userId = user.getId();
final List<InvitationEntity> invitations = invitationService.findAll();
invitations.stream().filter(invitation -> invitation.getEmail().equals(email)).forEach(invitation -> {
invitationService.addMember(invitation.getReferenceType().name(), invitation.getReferenceId(), userId, invitation.getApiRole(), invitation.getApplicationRole());
invitationService.delete(invitation.getId(), invitation.getReferenceId());
});
}
// Set date fields
user.setUpdatedAt(new Date());
// Encrypt password if internal user
encryptPassword(user, registerUserEntity.getPassword());
user = userRepository.update(user);
auditService.createOrganizationAuditLog(Collections.singletonMap(USER, user.getId()), User.AuditEvent.USER_CREATED, user.getUpdatedAt(), null, user);
// Do not send back the password
user.setPassword(null);
final UserEntity userEntity = convert(user, true);
searchEngineService.index(userEntity, false);
return userEntity;
} catch (AbstractManagementException ex) {
throw ex;
} catch (Exception ex) {
LOGGER.error("An error occurs while trying to create an internal user with the token {}", registerUserEntity.getToken(), ex);
throw new TechnicalManagementException(ex.getMessage(), ex);
}
}
Aggregations