Search in sources :

Example 1 with URLBuilder

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

the class SsoLoginServlet method service.

@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    log.debug("Entered SsoLoginServlet");
    String scope = String.format("ovirt-app-admin ovirt-app-portal ovirt-ext=auth:sequence-priority=%s", EngineLocalConfig.getInstance().getProperty(authSequencePriorityPropertyName));
    String redirectUri = String.format("%s://%s:%s%s", request.getScheme(), FiltersHelper.getRedirectUriServerName(request.getServerName()), request.getServerPort(), postActionUrl);
    URLBuilder urlBuilder = new URLBuilder(FiltersHelper.getEngineSsoUrl(request), "/oauth/authorize").addParameter("client_id", EngineLocalConfig.getInstance().getProperty("ENGINE_SSO_CLIENT_ID")).addParameter("response_type", "code").addParameter("app_url", request.getParameter("app_url")).addParameter("engine_url", FiltersHelper.getEngineUrl(request)).addParameter("redirect_uri", redirectUri).addParameter("scope", scope).addParameter("source_addr", request.getRemoteAddr());
    if (StringUtils.isNotEmpty(request.getParameter("sso_token"))) {
        urlBuilder.addParameter("sso_token", request.getParameter("sso_token"));
    }
    String url = urlBuilder.build();
    log.debug("Redirecting to '{}'", url);
    response.sendRedirect(url);
}
Also used : URLBuilder(org.ovirt.engine.core.uutils.net.URLBuilder)

Example 2 with URLBuilder

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

the class SsoLogoutServlet method service.

@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    log.debug("Entered SsoLogoutServlet");
    String token = null;
    try {
        String engineSessionId = (String) request.getAttribute(SessionConstants.HTTP_SESSION_ENGINE_SESSION_ID_KEY);
        if (StringUtils.isEmpty(engineSessionId) && request.getSession(false) != null) {
            engineSessionId = (String) request.getSession(false).getAttribute(SessionConstants.HTTP_SESSION_ENGINE_SESSION_ID_KEY);
        }
        if (StringUtils.isNotEmpty(engineSessionId)) {
            InitialContext ctx = new InitialContext();
            try {
                QueryParametersBase params = new QueryParametersBase(engineSessionId);
                params.setFiltered(true);
                QueryReturnValue retValue = FiltersHelper.getBackend(ctx).runQuery(QueryType.GetEngineSessionIdToken, params);
                token = retValue.getReturnValue();
                FiltersHelper.getBackend(ctx).runAction(ActionType.LogoutSession, new ActionParametersBase(engineSessionId));
            } finally {
                ctx.close();
            }
        }
    } catch (Exception ex) {
        log.error("Unable to clear user session {}", ex.getMessage());
    }
    HttpSession session = request.getSession(false);
    if (session != null) {
        log.debug("Setting session attribute {}", FiltersHelper.Constants.LOGOUT_INPROGRESS);
        session.setAttribute(FiltersHelper.Constants.LOGOUT_INPROGRESS, true);
    }
    Map<String, Object> revokeResponse = SsoOAuthServiceUtils.revoke(token);
    String error_description = (String) revokeResponse.get("error_description");
    String error = (String) revokeResponse.get("error");
    if (StringUtils.isNotEmpty(error_description)) {
        log.error("Unable to logout user: {}", error_description);
    }
    String url = String.format("%s://%s:%s%s/oauth2-callback", request.getScheme(), FiltersHelper.getRedirectUriServerName(request.getServerName()), request.getServerPort(), EngineLocalConfig.getInstance().getProperty("ENGINE_URI"));
    String redirectUri = new URLBuilder(url).addParameter("error_description", StringUtils.defaultIfEmpty(error_description, "")).addParameter("error", StringUtils.defaultIfEmpty(error, "")).build();
    if (session != null) {
        log.debug("Invalidating existing session");
        session.invalidate();
    }
    log.debug("Redirecting to {}", redirectUri);
    response.sendRedirect(redirectUri);
    log.debug("Exiting SsoLogoutServlet");
}
Also used : QueryReturnValue(org.ovirt.engine.core.common.queries.QueryReturnValue) HttpSession(javax.servlet.http.HttpSession) QueryParametersBase(org.ovirt.engine.core.common.queries.QueryParametersBase) InitialContext(javax.naming.InitialContext) ActionParametersBase(org.ovirt.engine.core.common.action.ActionParametersBase) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) URLBuilder(org.ovirt.engine.core.uutils.net.URLBuilder)

Example 3 with URLBuilder

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

the class LoginServlet method service.

@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Map<String, Object> deployedResponse = SsoOAuthServiceUtils.isSsoDeployed();
    if (deployedResponse.containsKey(WelcomeUtils.ERROR_DESCRIPTION)) {
        request.getSession(true).setAttribute(WelcomeUtils.ERROR_DESCRIPTION, deployedResponse.get(WelcomeUtils.ERROR_DESCRIPTION));
        request.getSession(true).setAttribute(WelcomeUtils.ERROR, deployedResponse.get(WelcomeUtils.ERROR));
        response.sendRedirect(EngineLocalConfig.getInstance().getProperty(WelcomeUtils.ENGINE_URI));
    } else {
        response.sendRedirect(new URLBuilder(FiltersHelper.getEngineSsoUrl(request), WelcomeUtils.OAUTH_AUTHORIZE_URI).addParameter(WelcomeUtils.HTTP_PARAM_CLIENT_ID, EngineLocalConfig.getInstance().getProperty(WelcomeUtils.ENGINE_SSO_CLIENT_ID)).addParameter(WelcomeUtils.HTTP_PARAM_RESPONSE_TYPE, WelcomeUtils.CODE).addParameter(WelcomeUtils.HTTP_PARAM_ENGINE_URL, FiltersHelper.getEngineUrl(request)).addParameter(WelcomeUtils.HTTP_PARAM_REDIRECT_URI, WelcomeUtils.getOauth2CallbackUrl(request)).addParameter(WelcomeUtils.HTTP_PARAM_SCOPE, request.getParameter(WelcomeUtils.SCOPE)).addParameter(WelcomeUtils.HTTP_PARAM_LOCALE, request.getAttribute(WelcomeUtils.LOCALE).toString()).addParameter(WelcomeUtils.HTTP_PARAM_SOURCE_ADDR, request.getRemoteAddr()).build());
    }
}
Also used : URLBuilder(org.ovirt.engine.core.uutils.net.URLBuilder)

Example 4 with URLBuilder

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

the class LogoutServlet method service.

@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String errorDescription = null;
    String error = null;
    try {
        Map<String, Object> revokeResponse = SsoOAuthServiceUtils.revoke((String) request.getSession(true).getAttribute(WelcomeUtils.TOKEN));
        errorDescription = (String) revokeResponse.get(WelcomeUtils.ERROR_DESCRIPTION);
        error = (String) revokeResponse.get(WelcomeUtils.ERROR);
        HttpSession session = request.getSession();
        if (session != null) {
            session.invalidate();
        }
    } catch (Exception ex) {
        log.error("Unable to logout user: {}", ex.getMessage());
    }
    response.sendRedirect(new URLBuilder(WelcomeUtils.getOauth2CallbackUrl(request)).addParameter(WelcomeUtils.ERROR_DESCRIPTION, StringUtils.defaultIfEmpty(errorDescription, "")).addParameter(WelcomeUtils.ERROR, StringUtils.defaultIfEmpty(error, "")).build());
}
Also used : HttpSession(javax.servlet.http.HttpSession) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) URLBuilder(org.ovirt.engine.core.uutils.net.URLBuilder)

Example 5 with URLBuilder

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

the class SsoUtils method redirectToModule.

public static void redirectToModule(HttpServletRequest request, HttpServletResponse response) throws IOException {
    log.debug("Entered redirectToModule");
    try {
        SsoSession ssoSession = getSsoSession(request);
        URLBuilder redirectUrl = new URLBuilder(getRedirectUrl(request)).addParameter("code", ssoSession.getAuthorizationCode());
        String appUrl = ssoSession.getAppUrl();
        if (StringUtils.isNotEmpty(appUrl)) {
            redirectUrl.addParameter("app_url", appUrl);
        }
        String state = ssoSession.getState();
        if (StringUtils.isNotEmpty(state)) {
            redirectUrl.addParameter("state", state);
        }
        String url = redirectUrl.build();
        response.sendRedirect(url);
        log.debug("Redirecting back to module: {}", url);
    } catch (Exception ex) {
        log.error("Error redirecting back to module: {}", ex.getMessage());
        log.debug("Exception", ex);
        throw new RuntimeException(ex);
    } finally {
        getSsoSession(request).cleanup();
    }
}
Also used : IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) URLBuilder(org.ovirt.engine.core.uutils.net.URLBuilder)

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