use of org.ovirt.engine.core.common.action.CreateUserSessionParameters 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());
}
}
use of org.ovirt.engine.core.common.action.CreateUserSessionParameters in project ovirt-engine by oVirt.
the class SsoUtils method createUserSession.
public static String createUserSession(HttpServletRequest req, Map<String, Object> jsonResponse, boolean loginAsAdmin) {
String engineSessionId = null;
if (!FiltersHelper.isStatusOk(jsonResponse)) {
throw new RuntimeException((String) jsonResponse.get("MESSAGE"));
}
InitialContext ctx = null;
Map<String, Object> payload = (Map<String, Object>) jsonResponse.get("ovirt");
String username = (String) jsonResponse.get("user_id");
String profile = null;
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), (String) jsonResponse.get(SessionConstants.SSO_SCOPE_KEY), 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"), req == null ? "" : req.getRemoteAddr(), (Collection<ExtMap>) payload.get("group_ids"), loginAsAdmin));
if (!queryRetVal.getSucceeded()) {
throw new RuntimeException(String.format("The user %s is not authorized to perform login", username));
}
engineSessionId = queryRetVal.getActionReturnValue();
if (req != null) {
req.setAttribute(SessionConstants.HTTP_SESSION_ENGINE_SESSION_ID_KEY, engineSessionId);
req.setAttribute(FiltersHelper.Constants.REQUEST_LOGIN_FILTER_AUTHENTICATION_DONE, true);
}
} catch (Exception ex) {
log.error("User '{}@{}' login failed: {}", username, profile, ex.getMessage());
log.debug("User '{}@{}' login failed", username, profile, ex);
} finally {
try {
if (ctx != null) {
ctx.close();
}
} catch (NamingException ex) {
log.error("Unable to close context", ex);
}
}
return engineSessionId;
}
Aggregations