Search in sources :

Example 11 with ProceedContext

use of com.okta.idx.sdk.api.client.ProceedContext in project okta-idx-java by okta.

the class LoginController method registerPhone.

/**
 * Handle phone authenticator enrollment functionality.
 *
 * @param phone the phone number
 * @param mode the delivery mode - sms or voice
 * @param session the session
 * @return the view associated with authentication response.
 */
@PostMapping(value = "/register-phone")
public ModelAndView registerPhone(@RequestParam("phone") final String phone, @RequestParam(value = "mode", required = false) final String mode, final HttpSession session) {
    logger.info(":: Enroll Phone Authenticator ::");
    if (!Strings.hasText(phone)) {
        ModelAndView mav = new ModelAndView("register-phone");
        mav.addObject("errors", "Phone is required");
        return mav;
    }
    if (!Strings.hasText(mode)) {
        ModelAndView modelAndView = new ModelAndView("select-phone-factor");
        modelAndView.addObject("phone", phone);
        return modelAndView;
    }
    ProceedContext proceedContext = Util.getProceedContextFromSession(session);
    AuthenticationResponse authenticationResponse = idxAuthenticationWrapper.submitPhoneAuthenticator(proceedContext, phone, getPhoneFactorFromMethod(session, mode));
    if (responseHandler.needsToShowErrors(authenticationResponse)) {
        ModelAndView modelAndView = new ModelAndView("register-phone");
        modelAndView.addObject("mode", mode);
        modelAndView.addObject("errors", authenticationResponse.getErrors());
        return modelAndView;
    }
    ModelAndView terminalTransition = responseHandler.handleTerminalTransitions(authenticationResponse, session);
    if (terminalTransition != null) {
        return terminalTransition;
    }
    return responseHandler.verifyForm();
}
Also used : 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 12 with ProceedContext

use of com.okta.idx.sdk.api.client.ProceedContext in project okta-idx-java by okta.

the class LoginController method enrollWebauthn.

/**
 * Handle webauthn authenticator enrollment functionality.
 *
 * @param webauthnRequest body
 * @param session         the session
 * @return the view associated with authentication response.
 */
@PostMapping(value = "/enroll-webauthn")
public ModelAndView enrollWebauthn(@RequestBody final WebAuthnRequest webauthnRequest, final HttpSession session) {
    logger.info(":: Enroll Webauthn Authenticator ::");
    ProceedContext proceedContext = Util.getProceedContextFromSession(session);
    AuthenticationResponse authenticationResponse = idxAuthenticationWrapper.verifyWebAuthn(proceedContext, webauthnRequest);
    return responseHandler.handleKnownTransitions(authenticationResponse, session);
}
Also used : AuthenticationResponse(com.okta.idx.sdk.api.response.AuthenticationResponse) ProceedContext(com.okta.idx.sdk.api.client.ProceedContext) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 13 with ProceedContext

use of com.okta.idx.sdk.api.client.ProceedContext in project okta-idx-java by okta.

the class LoginController method selectFactor.

/**
 * Handle factor selection during authentication.
 *
 * @param authenticatorId the authenticator ID of selected authenticator
 * @param mode the sms or voice factor mode
 * @param session the session
 * @return the view associated with authentication response.
 */
@PostMapping("/select-factor")
public ModelAndView selectFactor(@RequestParam("authenticatorId") final String authenticatorId, @RequestParam("mode") final String mode, final HttpSession session) {
    ProceedContext proceedContext = Util.getProceedContextFromSession(session);
    List<Authenticator> authenticators = (List<Authenticator>) session.getAttribute("authenticators");
    Authenticator foundAuthenticator = null;
    for (Authenticator auth : authenticators) {
        if (auth.getId().equals(authenticatorId)) {
            foundAuthenticator = auth;
        }
    }
    Assert.notNull(foundAuthenticator, "Authenticator not found");
    AuthenticationResponse authenticationResponse = null;
    Authenticator.Factor foundFactor = null;
    for (Authenticator.Factor factor : foundAuthenticator.getFactors()) {
        if (factor.getMethod().equals(mode)) {
            foundFactor = factor;
            authenticationResponse = idxAuthenticationWrapper.selectFactor(proceedContext, foundFactor);
            Optional.ofNullable(authenticationResponse.getContextualData()).map(ContextualData::getQrcode).map(Qrcode::getHref).ifPresent(qrCode -> {
                session.setAttribute("qrCode", qrCode);
                session.setAttribute("channelName", "qrcode");
            });
            if ("totp".equals(foundFactor.getMethod())) {
                session.setAttribute("totp", "totp");
            }
            break;
        }
    }
    Assert.notNull(foundFactor, "Factor not found");
    ModelAndView terminalTransition = responseHandler.handleTerminalTransitions(authenticationResponse, session);
    if (terminalTransition != null) {
        return terminalTransition;
    }
    switch(authenticationResponse.getAuthenticationStatus()) {
        case AWAITING_AUTHENTICATOR_VERIFICATION_DATA:
            return responseHandler.verifyForm();
        case AWAITING_AUTHENTICATOR_ENROLLMENT:
        case AWAITING_AUTHENTICATOR_ENROLLMENT_DATA:
            return responseHandler.registerVerifyForm(foundFactor);
        case AWAITING_CHANNEL_DATA_ENROLLMENT:
            return responseHandler.oktaVerifyViaChannelDataForm(foundFactor, session);
        case AWAITING_POLL_ENROLLMENT:
            return responseHandler.setupOktaVerifyForm(session);
        case AWAITING_CHALLENGE_POLL:
            return responseHandler.oktaVerifyChallenge(authenticationResponse);
        default:
            return responseHandler.handleKnownTransitions(authenticationResponse, session);
    }
}
Also used : ContextualData(com.okta.idx.sdk.api.model.ContextualData) ModelAndView(org.springframework.web.servlet.ModelAndView) List(java.util.List) AuthenticationResponse(com.okta.idx.sdk.api.response.AuthenticationResponse) Authenticator(com.okta.idx.sdk.api.client.Authenticator) ProceedContext(com.okta.idx.sdk.api.client.ProceedContext) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 14 with ProceedContext

use of com.okta.idx.sdk.api.client.ProceedContext 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 15 with ProceedContext

use of com.okta.idx.sdk.api.client.ProceedContext in project okta-idx-java by okta.

the class LoginController method verifyWebAuthn.

/**
 * Handle webauthn authenticator verification functionality.
 *
 * @param webauthnRequest
 * @param session the session
 * @return the view associated with authentication response.
 */
@PostMapping("/verify-webauthn")
public ModelAndView verifyWebAuthn(@RequestBody final WebAuthnRequest webauthnRequest, final HttpSession session) {
    logger.info(":: Verify Webauthn ::");
    ProceedContext proceedContext = Util.getProceedContextFromSession(session);
    AuthenticationResponse authenticationResponse = idxAuthenticationWrapper.verifyWebAuthn(proceedContext, webauthnRequest);
    if (responseHandler.needsToShowErrors(authenticationResponse)) {
        ModelAndView modelAndView = new ModelAndView("verify-webauthn");
        modelAndView.addObject("errors", authenticationResponse.getErrors());
        return modelAndView;
    }
    return responseHandler.handleKnownTransitions(authenticationResponse, session);
}
Also used : 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)

Aggregations

ProceedContext (com.okta.idx.sdk.api.client.ProceedContext)16 AuthenticationResponse (com.okta.idx.sdk.api.response.AuthenticationResponse)15 ModelAndView (org.springframework.web.servlet.ModelAndView)13 PostMapping (org.springframework.web.bind.annotation.PostMapping)12 VerifyAuthenticatorOptions (com.okta.idx.sdk.api.model.VerifyAuthenticatorOptions)6 GetMapping (org.springframework.web.bind.annotation.GetMapping)6 Authenticator (com.okta.idx.sdk.api.client.Authenticator)5 VerifyChannelDataOptions (com.okta.idx.sdk.api.model.VerifyChannelDataOptions)5 List (java.util.List)5 ContextualData (com.okta.idx.sdk.api.model.ContextualData)4 VerifyAuthenticatorAnswer (com.okta.idx.sdk.api.model.VerifyAuthenticatorAnswer)4 PollResults (com.okta.spring.example.helpers.PollResults)4 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)4 Assert (com.okta.commons.lang.Assert)3 Strings (com.okta.commons.lang.Strings)3 IDXAuthenticationWrapper (com.okta.idx.sdk.api.client.IDXAuthenticationWrapper)3 AuthenticationOptions (com.okta.idx.sdk.api.model.AuthenticationOptions)3 AuthenticationStatus (com.okta.idx.sdk.api.model.AuthenticationStatus)3 FormValue (com.okta.idx.sdk.api.model.FormValue)3 Qrcode (com.okta.idx.sdk.api.model.Qrcode)3