Search in sources :

Example 1 with AuthResult

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

the class OAuthTokenServlet method issueTokenUsingHttpHeaders.

private void issueTokenUsingHttpHeaders(HttpServletRequest request, HttpServletResponse response) throws Exception {
    log.debug("Entered issueTokenUsingHttpHeaders");
    try {
        AuthResult authResult = null;
        for (NonInteractiveAuth auth : getAuthSeq()) {
            authResult = auth.doAuth(request, response);
            if (authResult.getStatus() == Authn.AuthResult.SUCCESS || authResult.getStatus() == Authn.AuthResult.NEGOTIATION_INCOMPLETE) {
                break;
            }
        }
        if (authResult != null && authResult.getStatus() != Authn.AuthResult.SUCCESS) {
            log.debug("Authentication failed using http headers");
            List<String> schemes = (List<String>) request.getAttribute(NegotiateAuthUtils.REQUEST_SCHEMES_KEY);
            for (String scheme : new HashSet<>(schemes == null ? Collections.emptyList() : schemes)) {
                response.setHeader("WWW-Authenticate", scheme);
            }
            response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
        } else if (authResult != null && StringUtils.isNotEmpty(authResult.getToken())) {
            SsoSession ssoSession = SsoUtils.getSsoSessionFromRequest(request, authResult.getToken());
            if (ssoSession == null) {
                throw new OAuthException(SsoConstants.ERR_CODE_INVALID_GRANT, ssoContext.getLocalizationUtils().localize(SsoConstants.APP_ERROR_AUTHORIZATION_GRANT_EXPIRED, (Locale) request.getAttribute(SsoConstants.LOCALE)));
            }
            validateClientAcceptHeader(ssoSession, request);
            log.debug("Sending json response");
            SsoUtils.sendJsonData(response, buildResponse(ssoSession));
        } else {
            throw new AuthenticationException(ssoContext.getLocalizationUtils().localize(SsoConstants.APP_ERROR_AUTHENTICATION_FAILED, (Locale) request.getAttribute(SsoConstants.LOCALE)));
        }
    } catch (Exception ex) {
        throw new AuthenticationException(String.format(ssoContext.getLocalizationUtils().localize(SsoConstants.APP_ERROR_CANNOT_AUTHENTICATE_USER, (Locale) request.getAttribute(SsoConstants.LOCALE)), ex.getMessage()));
    }
}
Also used : Locale(java.util.Locale) NonInteractiveAuth(org.ovirt.engine.core.sso.utils.NonInteractiveAuth) AuthenticationException(org.ovirt.engine.core.sso.utils.AuthenticationException) OAuthException(org.ovirt.engine.core.sso.utils.OAuthException) AuthResult(org.ovirt.engine.core.sso.utils.AuthResult) ArrayList(java.util.ArrayList) List(java.util.List) SsoSession(org.ovirt.engine.core.sso.utils.SsoSession) ServletException(javax.servlet.ServletException) OAuthException(org.ovirt.engine.core.sso.utils.OAuthException) AuthenticationException(org.ovirt.engine.core.sso.utils.AuthenticationException) IOException(java.io.IOException) HashSet(java.util.HashSet)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Locale (java.util.Locale)1 ServletException (javax.servlet.ServletException)1 AuthResult (org.ovirt.engine.core.sso.utils.AuthResult)1 AuthenticationException (org.ovirt.engine.core.sso.utils.AuthenticationException)1 NonInteractiveAuth (org.ovirt.engine.core.sso.utils.NonInteractiveAuth)1 OAuthException (org.ovirt.engine.core.sso.utils.OAuthException)1 SsoSession (org.ovirt.engine.core.sso.utils.SsoSession)1