use of com.okta.idx.sdk.api.model.UserProfile in project okta-idx-java by okta.
the class LoginController method register.
/**
* Handle new user registration functionality.
*
* @param userProfileAttributes string array for user profile attributes from register form
* @param session the session
* @return the enroll authenticators view.
*/
@PostMapping("/register")
public ModelAndView register(@RequestParam(value = "userProfileAttribute[]") final String[] userProfileAttributes, final HttpSession session) {
logger.info(":: Register ::");
AuthenticationResponse beginResponse = idxAuthenticationWrapper.begin();
if (responseHandler.needsToShowErrors(beginResponse)) {
ModelAndView modelAndView = new ModelAndView("register");
modelAndView.addObject("errors", beginResponse.getErrors());
return modelAndView;
}
ProceedContext beginProceedContext = beginResponse.getProceedContext();
AuthenticationResponse newUserRegistrationResponse = idxAuthenticationWrapper.fetchSignUpFormValues(beginProceedContext);
if (responseHandler.needsToShowErrors(newUserRegistrationResponse)) {
ModelAndView modelAndView = new ModelAndView("register");
modelAndView.addObject("errors", newUserRegistrationResponse.getErrors());
return modelAndView;
}
if (responseHandler.needsToShowErrors(newUserRegistrationResponse)) {
ModelAndView mav = new ModelAndView("register");
mav.addObject("errors", newUserRegistrationResponse.getErrors());
return mav;
}
UserProfile userProfile = new UserProfile();
// FormValue userProfileFormValue = null;
// for (FormValue formValue: newUserRegistrationResponse.getFormValues()) {
// if (formValue.getName().contentEquals("userProfile")) {
// userProfileFormValue = formValue;
// }
// }
Optional<FormValue> userProfileFormValue = newUserRegistrationResponse.getFormValues().stream().filter(x -> x.getName().equals("userProfile")).findFirst();
if (!userProfileFormValue.isPresent()) {
ModelAndView modelAndView = new ModelAndView("register");
modelAndView.addObject("errors", "Unknown error occurred!");
return modelAndView;
}
int i = 0;
for (FormValue value : userProfileFormValue.get().form().getValue()) {
// Build the user profile
userProfile.addAttribute(value.getName(), userProfileAttributes[i]);
i++;
}
ProceedContext proceedContext = newUserRegistrationResponse.getProceedContext();
AuthenticationResponse authenticationResponse = idxAuthenticationWrapper.register(proceedContext, userProfile);
if (responseHandler.needsToShowErrors(authenticationResponse)) {
ModelAndView modelAndView = new ModelAndView("register");
modelAndView.addObject("errors", authenticationResponse.getErrors());
return modelAndView;
}
return responseHandler.handleKnownTransitions(authenticationResponse, session);
}
use of com.okta.idx.sdk.api.model.UserProfile in project okta-idx-java by okta.
the class IDXAuthenticationWrapper method fetchSignUpFormValues.
/**
* Populate UI form values for signing up a new user.
*
* @param proceedContext the proceedContext
* @return the authentication response
*/
public AuthenticationResponse fetchSignUpFormValues(ProceedContext proceedContext) {
AuthenticationResponse newUserRegistrationResponse = new AuthenticationResponse();
try {
Assert.notNull(proceedContext.getSelectProfileEnrollHref(), "Org policy is not configured to register new users.");
// enroll new user
AuthenticationTransaction enrollTransaction = AuthenticationTransaction.proceed(client, proceedContext, () -> {
EnrollRequest enrollRequest = EnrollRequestBuilder.builder().withStateHandle(proceedContext.getStateHandle()).build();
return client.enroll(enrollRequest, proceedContext.getSelectProfileEnrollHref());
});
RemediationOption enrollProfileRemediationOption = enrollTransaction.getRemediationOption(RemediationType.ENROLL_PROFILE);
List<FormValue> enrollProfileFormValues = Arrays.stream(enrollProfileRemediationOption.form()).filter(x -> "userProfile".equals(x.getName())).collect(Collectors.toList());
newUserRegistrationResponse.setFormValues(enrollProfileFormValues);
newUserRegistrationResponse.setProceedContext(enrollTransaction.createProceedContext());
return newUserRegistrationResponse;
} catch (ProcessingException e) {
return handleProcessingException(e);
} catch (IllegalArgumentException e) {
return handleIllegalArgumentException(e);
}
}
Aggregations