use of com.okta.idx.sdk.api.model.FormValue in project okta-idx-java by okta.
the class HomeController method displayRegisterPage.
/**
* Display the registration page.
*
* @param session the http session
* @return the register view
*/
@GetMapping("/register")
public ModelAndView displayRegisterPage(final HttpSession session) {
AuthenticationResponse authenticationResponse = begin(session);
authenticationResponse = authenticationWrapper.fetchSignUpFormValues(authenticationResponse.getProceedContext());
ModelAndView modelAndView = new ModelAndView("register");
Optional<FormValue> userProfileFormValue = authenticationResponse.getFormValues().stream().filter(x -> x.getName().equals("userProfile")).findFirst();
if (!userProfileFormValue.isPresent()) {
return displayErrorPage();
}
List<FormValue> userProfileAttributes = new LinkedList<>(Arrays.asList(userProfileFormValue.get().form().getValue()));
if (!CollectionUtils.isEmpty(userProfileAttributes)) {
modelAndView.addObject("userProfileAttributes", userProfileAttributes);
}
return modelAndView;
}
use of com.okta.idx.sdk.api.model.FormValue in project okta-idx-java by okta.
the class AuthenticationTransaction method getAuthenticators.
private List<Authenticator> getAuthenticators(Options[] options) {
if (options == null || options.length == 0) {
return null;
}
List<Authenticator> authenticators = new ArrayList<>();
for (Options option : options) {
String id = null;
String label = option.getLabel();
String enrollmentId = null;
String authenticatorType = null;
boolean hasNestedFactors = false;
boolean isChannelFactor = false;
Map<String, String> nestedMethods = new LinkedHashMap<>();
FormValue[] optionFormValues = ((OptionsForm) option.getValue()).getForm().getValue();
for (FormValue formValue : optionFormValues) {
if (formValue.getName().equals("methodType")) {
authenticatorType = String.valueOf(formValue.getValue());
// parse value from children
Options[] nestedOptions = formValue.options();
if (nestedOptions.length > 0) {
for (Options children : nestedOptions) {
nestedMethods.put(String.valueOf(children.getValue()), String.valueOf(children.getLabel()));
}
hasNestedFactors = true;
} else {
nestedMethods.put(String.valueOf(formValue.getValue()), label);
}
} else if ("channel".equals(formValue.getName())) {
authenticatorType = String.valueOf(option.getLabel()).toLowerCase(Locale.ROOT).replaceAll(" ", "_");
isChannelFactor = true;
Options[] nestedOptions = formValue.options();
if (nestedOptions.length > 0) {
for (Options children : nestedOptions) {
nestedMethods.put(String.valueOf(children.getValue()), String.valueOf(children.getLabel()));
}
hasNestedFactors = true;
} else {
nestedMethods.put(authenticatorType, label);
}
}
if (formValue.getName().equals("id")) {
id = String.valueOf(formValue.getValue());
}
if (formValue.getName().equals("enrollmentId")) {
enrollmentId = String.valueOf(formValue.getValue());
}
}
List<Authenticator.Factor> factors = new ArrayList<>();
for (Map.Entry<String, String> entry : nestedMethods.entrySet()) {
factors.add(new Authenticator.Factor(id, entry.getKey(), enrollmentId, entry.getValue(), isChannelFactor ? entry.getKey() : null));
}
authenticators.add(new Authenticator(id, authenticatorType, label, factors, hasNestedFactors));
}
return authenticators;
}
use of com.okta.idx.sdk.api.model.FormValue in project okta-idx-java by okta.
the class AuthenticationTransaction method getAuthenticators.
private List<Authenticator> getAuthenticators(FormValue parent) {
if (parent == null) {
return null;
}
List<Authenticator> authenticators = new ArrayList<>();
String id = null;
String label = parent.getLabel();
String enrollmentId = null;
String authenticatorType = null;
Map<String, String> nestedMethods = new LinkedHashMap<>();
boolean hasNestedFactors = false;
for (FormValue formValue : parent.form().getValue()) {
if (formValue.getName().equals("methodType")) {
authenticatorType = String.valueOf(formValue.getValue());
// parse value from children
Options[] nestedOptions = formValue.options();
if (nestedOptions.length > 0) {
for (Options children : nestedOptions) {
nestedMethods.put(String.valueOf(children.getValue()), String.valueOf(children.getLabel()));
}
hasNestedFactors = true;
} else {
nestedMethods.put(String.valueOf(formValue.getValue()), label);
}
}
if (formValue.getName().equals("id")) {
id = String.valueOf(formValue.getValue());
}
if (formValue.getName().equals("enrollmentId")) {
enrollmentId = String.valueOf(formValue.getValue());
}
}
List<Authenticator.Factor> factors = new ArrayList<>();
for (Map.Entry<String, String> entry : nestedMethods.entrySet()) {
factors.add(new Authenticator.Factor(id, entry.getKey(), enrollmentId, entry.getValue(), null));
}
authenticators.add(new Authenticator(id, authenticatorType, label, factors, hasNestedFactors));
return authenticators;
}
use of com.okta.idx.sdk.api.model.FormValue 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.FormValue 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