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();
}
}
}
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);
}
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());
}
}
Aggregations