Search in sources :

Example 1 with FormValue

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;
}
Also used : HttpSession(javax.servlet.http.HttpSession) Strings(com.okta.commons.lang.Strings) RequestParam(org.springframework.web.bind.annotation.RequestParam) Arrays(java.util.Arrays) Util(com.okta.spring.example.helpers.Util) AuthenticationResponse(com.okta.idx.sdk.api.response.AuthenticationResponse) FormValue(com.okta.idx.sdk.api.model.FormValue) HomeHelper(com.okta.spring.example.helpers.HomeHelper) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Controller(org.springframework.stereotype.Controller) Authenticator(com.okta.idx.sdk.api.client.Authenticator) ResponseHandler(com.okta.spring.example.helpers.ResponseHandler) ProceedContext(com.okta.idx.sdk.api.client.ProceedContext) ModelAndView(org.springframework.web.servlet.ModelAndView) IDXAuthenticationWrapper(com.okta.idx.sdk.api.client.IDXAuthenticationWrapper) List(java.util.List) CollectionUtils(org.springframework.util.CollectionUtils) TokenResponse(com.okta.idx.sdk.api.response.TokenResponse) GetMapping(org.springframework.web.bind.annotation.GetMapping) Optional(java.util.Optional) VerifyAuthenticatorOptions(com.okta.idx.sdk.api.model.VerifyAuthenticatorOptions) LinkedList(java.util.LinkedList) FormValue(com.okta.idx.sdk.api.model.FormValue) ModelAndView(org.springframework.web.servlet.ModelAndView) AuthenticationResponse(com.okta.idx.sdk.api.response.AuthenticationResponse) LinkedList(java.util.LinkedList) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 2 with FormValue

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;
}
Also used : Options(com.okta.idx.sdk.api.model.Options) FormValue(com.okta.idx.sdk.api.model.FormValue) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with FormValue

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;
}
Also used : Options(com.okta.idx.sdk.api.model.Options) FormValue(com.okta.idx.sdk.api.model.FormValue) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 4 with FormValue

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);
}
Also used : RequestParam(org.springframework.web.bind.annotation.RequestParam) Arrays(java.util.Arrays) Qrcode(com.okta.idx.sdk.api.model.Qrcode) ContextualData(com.okta.idx.sdk.api.model.ContextualData) Util(com.okta.spring.example.helpers.Util) AuthenticationResponse(com.okta.idx.sdk.api.response.AuthenticationResponse) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Controller(org.springframework.stereotype.Controller) Authenticator(com.okta.idx.sdk.api.client.Authenticator) UserProfile(com.okta.idx.sdk.api.model.UserProfile) ResponseHandler(com.okta.spring.example.helpers.ResponseHandler) RequestBody(org.springframework.web.bind.annotation.RequestBody) ProceedContext(com.okta.idx.sdk.api.client.ProceedContext) IDXAuthenticationWrapper(com.okta.idx.sdk.api.client.IDXAuthenticationWrapper) VerifyChannelDataOptions(com.okta.idx.sdk.api.model.VerifyChannelDataOptions) PollResults(com.okta.spring.example.helpers.PollResults) GetMapping(org.springframework.web.bind.annotation.GetMapping) VerifyAuthenticatorOptions(com.okta.idx.sdk.api.model.VerifyAuthenticatorOptions) HttpSession(javax.servlet.http.HttpSession) PostMapping(org.springframework.web.bind.annotation.PostMapping) Strings(com.okta.commons.lang.Strings) VerifyAuthenticatorAnswer(com.okta.idx.sdk.api.model.VerifyAuthenticatorAnswer) Assert(com.okta.commons.lang.Assert) Logger(org.slf4j.Logger) FormValue(com.okta.idx.sdk.api.model.FormValue) AuthenticationOptions(com.okta.idx.sdk.api.model.AuthenticationOptions) AuthenticationStatus(com.okta.idx.sdk.api.model.AuthenticationStatus) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) Collectors(java.util.stream.Collectors) WebAuthnRequest(com.okta.idx.sdk.api.request.WebAuthnRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) List(java.util.List) Optional(java.util.Optional) UserProfile(com.okta.idx.sdk.api.model.UserProfile) FormValue(com.okta.idx.sdk.api.model.FormValue) ModelAndView(org.springframework.web.servlet.ModelAndView) AuthenticationResponse(com.okta.idx.sdk.api.response.AuthenticationResponse) ProceedContext(com.okta.idx.sdk.api.client.ProceedContext) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 5 with FormValue

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);
    }
}
Also used : DeviceContext(com.okta.idx.sdk.api.model.DeviceContext) Arrays(java.util.Arrays) ChallengeRequestBuilder(com.okta.idx.sdk.api.request.ChallengeRequestBuilder) SkipAuthenticatorEnrollmentRequest(com.okta.idx.sdk.api.request.SkipAuthenticatorEnrollmentRequest) AuthenticationResponse(com.okta.idx.sdk.api.response.AuthenticationResponse) LoggerFactory(org.slf4j.LoggerFactory) IdentifyRequest(com.okta.idx.sdk.api.request.IdentifyRequest) ProcessingException(com.okta.idx.sdk.api.exception.ProcessingException) SkipAuthenticatorEnrollmentRequestBuilder(com.okta.idx.sdk.api.request.SkipAuthenticatorEnrollmentRequestBuilder) ErrorResponse(com.okta.idx.sdk.api.response.ErrorResponse) Recover(com.okta.idx.sdk.api.model.Recover) IDXResponse(com.okta.idx.sdk.api.response.IDXResponse) Map(java.util.Map) ClientUtil(com.okta.idx.sdk.api.util.ClientUtil) EnrollUserProfileUpdateRequestBuilder(com.okta.idx.sdk.api.request.EnrollUserProfileUpdateRequestBuilder) Assert(com.okta.commons.lang.Assert) RemediationOption(com.okta.idx.sdk.api.model.RemediationOption) Set(java.util.Set) AuthenticationStatus(com.okta.idx.sdk.api.model.AuthenticationStatus) Response(com.okta.commons.http.Response) WrapperUtil.handleProcessingException(com.okta.idx.sdk.api.client.WrapperUtil.handleProcessingException) EnrollRequestBuilder(com.okta.idx.sdk.api.request.EnrollRequestBuilder) Collectors(java.util.stream.Collectors) WebAuthnRequest(com.okta.idx.sdk.api.request.WebAuthnRequest) List(java.util.List) Optional(java.util.Optional) AuthenticatorEnrollment(com.okta.idx.sdk.api.model.AuthenticatorEnrollment) AnswerChallengeRequest(com.okta.idx.sdk.api.request.AnswerChallengeRequest) EnrollUserProfileUpdateRequest(com.okta.idx.sdk.api.request.EnrollUserProfileUpdateRequest) UserProfile(com.okta.idx.sdk.api.model.UserProfile) EnrollRequest(com.okta.idx.sdk.api.request.EnrollRequest) TokenType(com.okta.idx.sdk.api.model.TokenType) VerifyChannelDataOptions(com.okta.idx.sdk.api.model.VerifyChannelDataOptions) ChallengeRequest(com.okta.idx.sdk.api.request.ChallengeRequest) Authenticator(com.okta.idx.sdk.api.model.Authenticator) IdentifyRequestBuilder(com.okta.idx.sdk.api.request.IdentifyRequestBuilder) VerifyAuthenticatorOptions(com.okta.idx.sdk.api.model.VerifyAuthenticatorOptions) AnswerChallengeRequestBuilder(com.okta.idx.sdk.api.request.AnswerChallengeRequestBuilder) RecoverRequest(com.okta.idx.sdk.api.request.RecoverRequest) VerifyAuthenticatorAnswer(com.okta.idx.sdk.api.model.VerifyAuthenticatorAnswer) Logger(org.slf4j.Logger) PollRequestBuilder(com.okta.idx.sdk.api.request.PollRequestBuilder) Credentials(com.okta.idx.sdk.api.model.Credentials) FormValue(com.okta.idx.sdk.api.model.FormValue) RemediationType(com.okta.idx.sdk.api.model.RemediationType) WrapperUtil.handleIllegalArgumentException(com.okta.idx.sdk.api.client.WrapperUtil.handleIllegalArgumentException) AuthenticationOptions(com.okta.idx.sdk.api.model.AuthenticationOptions) EmailTokenType(com.okta.idx.sdk.api.model.EmailTokenType) RecoverRequestBuilder(com.okta.idx.sdk.api.request.RecoverRequestBuilder) AuthenticatorEnrollments(com.okta.idx.sdk.api.model.AuthenticatorEnrollments) IDXClientContext(com.okta.idx.sdk.api.model.IDXClientContext) TokenResponse(com.okta.idx.sdk.api.response.TokenResponse) PollRequest(com.okta.idx.sdk.api.request.PollRequest) PollInfo(com.okta.idx.sdk.api.model.PollInfo) EnrollRequest(com.okta.idx.sdk.api.request.EnrollRequest) FormValue(com.okta.idx.sdk.api.model.FormValue) RemediationOption(com.okta.idx.sdk.api.model.RemediationOption) AuthenticationResponse(com.okta.idx.sdk.api.response.AuthenticationResponse) WrapperUtil.handleIllegalArgumentException(com.okta.idx.sdk.api.client.WrapperUtil.handleIllegalArgumentException) ProcessingException(com.okta.idx.sdk.api.exception.ProcessingException) WrapperUtil.handleProcessingException(com.okta.idx.sdk.api.client.WrapperUtil.handleProcessingException)

Aggregations

FormValue (com.okta.idx.sdk.api.model.FormValue)5 VerifyAuthenticatorOptions (com.okta.idx.sdk.api.model.VerifyAuthenticatorOptions)3 AuthenticationResponse (com.okta.idx.sdk.api.response.AuthenticationResponse)3 Arrays (java.util.Arrays)3 List (java.util.List)3 Optional (java.util.Optional)3 Assert (com.okta.commons.lang.Assert)2 Strings (com.okta.commons.lang.Strings)2 Authenticator (com.okta.idx.sdk.api.client.Authenticator)2 IDXAuthenticationWrapper (com.okta.idx.sdk.api.client.IDXAuthenticationWrapper)2 ProceedContext (com.okta.idx.sdk.api.client.ProceedContext)2 AuthenticationOptions (com.okta.idx.sdk.api.model.AuthenticationOptions)2 AuthenticationStatus (com.okta.idx.sdk.api.model.AuthenticationStatus)2 Options (com.okta.idx.sdk.api.model.Options)2 UserProfile (com.okta.idx.sdk.api.model.UserProfile)2 VerifyAuthenticatorAnswer (com.okta.idx.sdk.api.model.VerifyAuthenticatorAnswer)2 VerifyChannelDataOptions (com.okta.idx.sdk.api.model.VerifyChannelDataOptions)2 WebAuthnRequest (com.okta.idx.sdk.api.request.WebAuthnRequest)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2