Search in sources :

Example 1 with AuthenticationException

use of org.ovirt.engine.core.sso.utils.AuthenticationException in project ovirt-engine by oVirt.

the class InteractiveAuthServlet method service.

@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    log.debug("Entered InteractiveAuthServlet");
    try {
        String redirectUrl;
        SsoSession ssoSession = SsoUtils.getSsoSession(request);
        // clean up the sso session id token
        ssoContext.removeSsoSessionById(ssoSession);
        if (StringUtils.isEmpty(ssoSession.getClientId())) {
            redirectUrl = ssoContext.getEngineUrl();
        } else {
            Credentials userCredentials = getUserCredentials(request);
            try {
                if (SsoUtils.isUserAuthenticated(request)) {
                    log.debug("User is authenticated redirecting to {}", SsoConstants.INTERACTIVE_REDIRECT_TO_MODULE_URI);
                    redirectUrl = request.getContextPath() + SsoConstants.INTERACTIVE_REDIRECT_TO_MODULE_URI;
                } else {
                    redirectUrl = authenticateUser(request, response, userCredentials);
                }
            } catch (AuthenticationException ex) {
                if (userCredentials != null) {
                    String profile = userCredentials.getProfile() == null ? "N/A" : userCredentials.getProfile();
                    log.error("Cannot authenticate user '{}@{}' connecting from '{}': {}", userCredentials.getUsername(), profile, ssoSession.getSourceAddr(), ex.getMessage());
                    log.debug("Exception", ex);
                    SsoUtils.getSsoSession(request).setLoginMessage(ex.getMessage());
                }
                log.debug("Redirecting to LoginPage");
                ssoSession.setReauthenticate(false);
                ssoContext.registerSsoSessionById(SsoUtils.generateIdToken(), ssoSession);
                if (StringUtils.isNotEmpty(ssoContext.getSsoDefaultProfile()) && Arrays.stream(request.getCookies()).noneMatch(c -> c.getName().equals("profile"))) {
                    Cookie cookie = new Cookie("profile", ssoContext.getSsoDefaultProfile());
                    cookie.setSecure("https".equalsIgnoreCase(request.getScheme()));
                    response.addCookie(cookie);
                }
                redirectUrl = request.getContextPath() + SsoConstants.INTERACTIVE_LOGIN_FORM_URI;
            }
        }
        if (redirectUrl != null) {
            response.sendRedirect(redirectUrl);
        }
    } catch (Exception ex) {
        SsoUtils.redirectToErrorPage(request, response, ex);
    }
}
Also used : Cookie(javax.servlet.http.Cookie) AuthenticationException(org.ovirt.engine.core.sso.utils.AuthenticationException) SsoSession(org.ovirt.engine.core.sso.utils.SsoSession) Credentials(org.ovirt.engine.core.sso.utils.Credentials) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) AuthenticationException(org.ovirt.engine.core.sso.utils.AuthenticationException)

Example 2 with AuthenticationException

use of org.ovirt.engine.core.sso.utils.AuthenticationException in project ovirt-engine by oVirt.

the class InteractiveAuthServlet method authenticateUser.

private String authenticateUser(HttpServletRequest request, HttpServletResponse response, Credentials userCredentials) throws ServletException, IOException, AuthenticationException {
    if (userCredentials == null || !SsoUtils.areCredentialsValid(request, userCredentials, true)) {
        throw new AuthenticationException(ssoContext.getLocalizationUtils().localize(SsoConstants.APP_ERROR_INVALID_CREDENTIALS, (Locale) request.getAttribute(SsoConstants.LOCALE)));
    }
    try {
        log.debug("Authenticating user using credentials");
        Cookie cookie = new Cookie("profile", userCredentials.getProfile());
        cookie.setSecure("https".equalsIgnoreCase(request.getScheme()));
        response.addCookie(cookie);
        AuthenticationUtils.handleCredentials(ssoContext, request, userCredentials);
        return request.getContextPath() + SsoConstants.INTERACTIVE_REDIRECT_TO_MODULE_URI;
    } catch (AuthenticationException ex) {
        throw ex;
    } catch (Exception ex) {
        log.error("Internal Server Error: {}", ex.getMessage());
        log.debug("Exception", ex);
        throw new RuntimeException(ex.getMessage(), ex);
    }
}
Also used : Locale(java.util.Locale) Cookie(javax.servlet.http.Cookie) AuthenticationException(org.ovirt.engine.core.sso.utils.AuthenticationException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) AuthenticationException(org.ovirt.engine.core.sso.utils.AuthenticationException)

Example 3 with AuthenticationException

use of org.ovirt.engine.core.sso.utils.AuthenticationException in project ovirt-engine by oVirt.

the class InteractiveBasicAuthServlet method service.

@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    log.debug("Extracting basic auth credentials from header");
    Credentials credentials = SsoUtils.getUserCredentialsFromHeader(request);
    boolean credentialsValid = false;
    try {
        credentialsValid = credentials != null && SsoUtils.areCredentialsValid(request, credentials);
    } catch (AuthenticationException ex) {
        log.error("Error validating credentials: {}", ex.getMessage());
        log.debug("Exception", ex);
    }
    if (credentialsValid) {
        log.debug("Credentials Valid redirecting to url: {}", SsoConstants.INTERACTIVE_LOGIN_URI);
        SsoUtils.getSsoSession(request).setTempCredentials(credentials);
        response.sendRedirect(request.getContextPath() + SsoConstants.INTERACTIVE_LOGIN_URI);
    } else {
        log.debug("Redirecting to url: {}", SsoConstants.INTERACTIVE_LOGIN_NEXT_AUTH_URI);
        response.sendRedirect(request.getContextPath() + SsoConstants.INTERACTIVE_LOGIN_NEXT_AUTH_URI);
    }
}
Also used : AuthenticationException(org.ovirt.engine.core.sso.utils.AuthenticationException) Credentials(org.ovirt.engine.core.sso.utils.Credentials)

Example 4 with AuthenticationException

use of org.ovirt.engine.core.sso.utils.AuthenticationException in project ovirt-engine by oVirt.

the class InteractiveChangePasswdServlet method service.

@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    log.debug("Entered InteractiveChangePasswdServlet");
    Credentials userCredentials = null;
    String redirectUrl;
    try {
        log.debug("User is not authenticated extracting credentials from request.");
        userCredentials = getUserCredentials(request);
        if (userCredentials == null) {
            throw new AuthenticationException(ssoContext.getLocalizationUtils().localize(SsoConstants.APP_ERROR_UNABLE_TO_EXTRACT_CREDENTIALS, (Locale) request.getAttribute(SsoConstants.LOCALE)));
        }
        if (!userCredentials.getNewCredentials().equals(userCredentials.getConfirmedNewCredentials())) {
            throw new AuthenticationException(ssoContext.getLocalizationUtils().localize(SsoConstants.APP_ERROR_PASSWORDS_DONT_MATCH, (Locale) request.getAttribute(SsoConstants.LOCALE)));
        }
        redirectUrl = changeUserPasswd(request, userCredentials);
    } catch (Exception ex) {
        String msg = String.format(ssoContext.getLocalizationUtils().localize(SsoConstants.APP_ERROR_CHANGE_PASSWORD_FAILED, (Locale) request.getAttribute(SsoConstants.LOCALE)), userCredentials == null ? "" : userCredentials.getUsername() + "@" + userCredentials.getProfile(), ex.getMessage());
        log.error(msg);
        log.debug("Exception", ex);
        SsoUtils.getSsoSession(request).setChangePasswdMessage(msg);
        redirectUrl = request.getContextPath() + SsoConstants.INTERACTIVE_CHANGE_PASSWD_FORM_URI;
    }
    log.debug("Redirecting to url: {}", redirectUrl);
    response.sendRedirect(redirectUrl);
}
Also used : Locale(java.util.Locale) AuthenticationException(org.ovirt.engine.core.sso.utils.AuthenticationException) Credentials(org.ovirt.engine.core.sso.utils.Credentials) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) AuthenticationException(org.ovirt.engine.core.sso.utils.AuthenticationException)

Example 5 with AuthenticationException

use of org.ovirt.engine.core.sso.utils.AuthenticationException in project ovirt-engine by oVirt.

the class InteractiveChangePasswdServlet method getUserCredentials.

private Credentials getUserCredentials(HttpServletRequest request) throws AuthenticationException {
    try {
        String username = SsoUtils.getFormParameter(request, USERNAME);
        String credentials = SsoUtils.getFormParameter(request, CREDENTIALS);
        String credentialsNew1 = SsoUtils.getFormParameter(request, CREDENTIALS_NEW1);
        String credentialsNew2 = SsoUtils.getFormParameter(request, CREDENTIALS_NEW2);
        String profile = SsoUtils.getFormParameter(request, PROFILE);
        return StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(credentials) && StringUtils.isNotEmpty(credentialsNew1) && StringUtils.isNotEmpty(credentialsNew2) && StringUtils.isNotEmpty(profile) ? new Credentials(username, credentials, credentialsNew1, credentialsNew2, profile) : null;
    } catch (Exception ex) {
        throw new AuthenticationException(ssoContext.getLocalizationUtils().localize(SsoConstants.APP_ERROR_UNABLE_TO_EXTRACT_CREDENTIALS, (Locale) request.getAttribute(SsoConstants.LOCALE)), ex);
    }
}
Also used : AuthenticationException(org.ovirt.engine.core.sso.utils.AuthenticationException) Credentials(org.ovirt.engine.core.sso.utils.Credentials) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) AuthenticationException(org.ovirt.engine.core.sso.utils.AuthenticationException)

Aggregations

AuthenticationException (org.ovirt.engine.core.sso.utils.AuthenticationException)9 IOException (java.io.IOException)6 ServletException (javax.servlet.ServletException)6 Credentials (org.ovirt.engine.core.sso.utils.Credentials)6 Locale (java.util.Locale)5 SsoSession (org.ovirt.engine.core.sso.utils.SsoSession)5 Cookie (javax.servlet.http.Cookie)2 OAuthException (org.ovirt.engine.core.sso.utils.OAuthException)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 AuthResult (org.ovirt.engine.core.sso.utils.AuthResult)1 NonInteractiveAuth (org.ovirt.engine.core.sso.utils.NonInteractiveAuth)1