Search in sources :

Example 6 with URLBuilder

use of org.ovirt.engine.core.uutils.net.URLBuilder in project ovirt-engine by oVirt.

the class SsoUtils method redirectToErrorPageImpl.

private static void redirectToErrorPageImpl(HttpServletRequest request, HttpServletResponse response, OAuthException ex) {
    log.debug("Entered redirectToErrorPage");
    SsoSession ssoSession = null;
    try {
        ssoSession = getSsoSession(request, true);
        if (ssoSession.getStatus() != SsoSession.Status.authenticated) {
            ssoSession.setStatus(SsoSession.Status.unauthenticated);
        }
        URLBuilder redirectUrlBuilder = new URLBuilder(getRedirectUrl(request));
        redirectUrlBuilder.addParameter(SsoConstants.ERROR, ex.getCode()).addParameter(SsoConstants.ERROR_DESCRIPTION, ex.getMessage());
        String state = SsoUtils.getRequestParameter(request, SsoConstants.HTTP_PARAM_STATE, "");
        if (StringUtils.isNotEmpty(state)) {
            redirectUrlBuilder.addParameter("state", state);
        }
        response.setStatus(HttpStatus.SC_BAD_REQUEST);
        String redirectUrl = redirectUrlBuilder.build();
        response.sendRedirect(redirectUrl);
        log.debug("Redirecting back to module: {}", redirectUrl);
    } catch (Exception e) {
        log.error("Error redirecting to error page: {}", e.getMessage());
        log.debug("Exception", e);
        throw new RuntimeException(ex);
    } finally {
        if (ssoSession != null) {
            ssoSession.cleanup();
        }
    }
}
Also used : IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) URLBuilder(org.ovirt.engine.core.uutils.net.URLBuilder)

Example 7 with URLBuilder

use of org.ovirt.engine.core.uutils.net.URLBuilder in project ovirt-engine by oVirt.

the class OAuthAuthorizeServlet method login.

protected void login(HttpServletRequest request, HttpServletResponse response, SsoSession ssoSession) throws Exception {
    log.debug("Entered login queryString: {}", request.getQueryString());
    String redirectUrl;
    if (SsoUtils.isUserAuthenticated(request)) {
        log.debug("User is authenticated redirecting to interactive-redirect-to-module");
        redirectUrl = request.getContextPath() + SsoConstants.INTERACTIVE_REDIRECT_TO_MODULE_URI;
    } else if (SsoUtils.scopeAsList(SsoUtils.getScopeRequestParameter(request, "")).contains("ovirt-ext=auth:identity")) {
        redirectUrl = new URLBuilder(SsoUtils.getRedirectUrl(request)).addParameter(SsoConstants.ERROR, SsoConstants.ERR_OVIRT_CODE_NOT_AUTHENTICATED).addParameter(SsoConstants.ERROR_DESCRIPTION, SsoConstants.ERR_CODE_NOT_AUTHENTICATED_MSG).build();
    } else {
        ssoSession.setAuthStack(getAuthSeq(ssoSession));
        if (ssoSession.getAuthStack().isEmpty()) {
            throw new OAuthException(SsoConstants.ERR_CODE_ACCESS_DENIED, ssoContext.getLocalizationUtils().localize(SsoConstants.APP_ERROR_NO_VALID_AUTHENTICATION_MECHANISM_FOUND, (Locale) request.getAttribute(SsoConstants.LOCALE)));
        }
        redirectUrl = request.getContextPath() + SsoConstants.INTERACTIVE_LOGIN_NEXT_AUTH_URI;
    }
    log.debug("Redirecting to url: {}", redirectUrl);
    response.sendRedirect(redirectUrl);
}
Also used : OAuthException(org.ovirt.engine.core.sso.utils.OAuthException) URLBuilder(org.ovirt.engine.core.uutils.net.URLBuilder)

Example 8 with URLBuilder

use of org.ovirt.engine.core.uutils.net.URLBuilder in project ovirt-engine by oVirt.

the class SsoPostLoginServlet method service.

@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    log.debug("Entered SsoPostLoginServlet");
    String username = null;
    String profile = null;
    InitialContext ctx = null;
    try {
        String error_description = request.getParameter("error_description");
        String error = request.getParameter("error");
        if (StringUtils.isNotEmpty(error_description) && StringUtils.isNotEmpty(error)) {
            throw new RuntimeException(String.format("%s: %s", error, error_description));
        }
        String code = request.getParameter("code");
        if (StringUtils.isEmpty(code)) {
            throw new RuntimeException("No authorization code found in request");
        }
        String appUrl = request.getParameter("app_url");
        log.debug("Received app_url '{}'", appUrl);
        Map<String, Object> jsonResponse = FiltersHelper.getPayloadForAuthCode(code, "ovirt-app-admin ovirt-app-portal ovirt-app-api", URLEncoder.encode(postActionUrl, "UTF-8"));
        Map<String, Object> payload = (Map<String, Object>) jsonResponse.get("ovirt");
        username = (String) jsonResponse.get("user_id");
        profile = "";
        int index = username.lastIndexOf("@");
        if (index != -1) {
            profile = username.substring(index + 1);
            username = username.substring(0, index);
        }
        try {
            ctx = new InitialContext();
            ActionReturnValue queryRetVal = FiltersHelper.getBackend(ctx).runAction(ActionType.CreateUserSession, new CreateUserSessionParameters((String) jsonResponse.get(SessionConstants.SSO_TOKEN_KEY), (String) jsonResponse.get(SessionConstants.SSO_SCOPE_KEY), appScope, profile, username, (String) payload.get("principal_id"), (String) payload.get("email"), (String) payload.get("first_name"), (String) payload.get("last_name"), (String) payload.get("namespace"), request.getRemoteAddr(), (Collection<ExtMap>) payload.get("group_ids"), loginAsAdmin));
            if (!queryRetVal.getSucceeded()) {
                throw new RuntimeException(String.format("The user %s@%s is not authorized to perform login", username, profile));
            } else {
                HttpSession httpSession = request.getSession(true);
                httpSession.setAttribute(SessionConstants.HTTP_SESSION_ENGINE_SESSION_ID_KEY, queryRetVal.getActionReturnValue());
                httpSession.setAttribute(FiltersHelper.Constants.REQUEST_LOGIN_FILTER_AUTHENTICATION_DONE, true);
                log.debug("Redirecting to '{}'", appUrl);
                response.sendRedirect(appUrl);
            }
        } catch (RuntimeException ex) {
            throw ex;
        } catch (Exception ex) {
            throw new RuntimeException(String.format("User login failure: %s", username), ex);
        } finally {
            try {
                if (ctx != null) {
                    ctx.close();
                }
            } catch (NamingException ex) {
                log.error("Unable to close context", ex);
            }
        }
    } catch (Exception ex) {
        log.error(ex.getMessage());
        log.debug("User login failure", ex);
        String url = String.format("%s://%s:%s%s/", request.getScheme(), FiltersHelper.getRedirectUriServerName(request.getServerName()), request.getServerPort(), EngineLocalConfig.getInstance().getProperty("ENGINE_URI"));
        response.sendRedirect(new URLBuilder(url).addParameter("error_description", StringUtils.defaultIfEmpty(ex.getMessage(), "Internal Server error")).addParameter("error", "server_error").build());
    }
}
Also used : HttpSession(javax.servlet.http.HttpSession) CreateUserSessionParameters(org.ovirt.engine.core.common.action.CreateUserSessionParameters) InitialContext(javax.naming.InitialContext) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) NamingException(javax.naming.NamingException) URLBuilder(org.ovirt.engine.core.uutils.net.URLBuilder) ActionReturnValue(org.ovirt.engine.core.common.action.ActionReturnValue) Collection(java.util.Collection) NamingException(javax.naming.NamingException) ExtMap(org.ovirt.engine.api.extensions.ExtMap) Map(java.util.Map)

Aggregations

URLBuilder (org.ovirt.engine.core.uutils.net.URLBuilder)8 IOException (java.io.IOException)5 ServletException (javax.servlet.ServletException)3 HttpSession (javax.servlet.http.HttpSession)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 InitialContext (javax.naming.InitialContext)2 Collection (java.util.Collection)1 Map (java.util.Map)1 NamingException (javax.naming.NamingException)1 ExtMap (org.ovirt.engine.api.extensions.ExtMap)1 ActionParametersBase (org.ovirt.engine.core.common.action.ActionParametersBase)1 ActionReturnValue (org.ovirt.engine.core.common.action.ActionReturnValue)1 CreateUserSessionParameters (org.ovirt.engine.core.common.action.CreateUserSessionParameters)1 QueryParametersBase (org.ovirt.engine.core.common.queries.QueryParametersBase)1 QueryReturnValue (org.ovirt.engine.core.common.queries.QueryReturnValue)1 OAuthException (org.ovirt.engine.core.sso.utils.OAuthException)1