use of org.openkilda.auth.model.RequestContext in project open-kilda by telstra.
the class RestClientManager method invoke.
/**
* Invoke.
*
* @param apiUrl the api url
* @param httpMethod the http method
* @param payload the payload
* @param contentType the content type
* @param basicAuth the basic auth
* @return the http response
*/
public HttpResponse invoke(final String apiUrl, final HttpMethod httpMethod, final String payload, final String contentType, final String basicAuth) {
HttpResponse httpResponse = null;
try {
RequestContext requestContext = serverContext.getRequestContext();
HttpClient client = HttpClients.createDefault();
HttpUriRequest httpUriRequest = null;
HttpEntityEnclosingRequestBase httpEntityEnclosingRequest = null;
// Initializing Request
if (HttpMethod.POST.equals(httpMethod)) {
httpEntityEnclosingRequest = new HttpPost(apiUrl);
} else if (HttpMethod.PUT.equals(httpMethod)) {
httpEntityEnclosingRequest = new HttpPut(apiUrl);
} else if (HttpMethod.DELETE.equals(httpMethod)) {
httpEntityEnclosingRequest = new HttpEntityEnclosingRequestBase() {
@Override
public String getMethod() {
return "DELETE";
}
};
} else if (HttpMethod.PATCH.equals(httpMethod)) {
httpEntityEnclosingRequest = new HttpPatch(apiUrl);
} else {
httpUriRequest = new HttpGet(apiUrl);
}
if (!HttpMethod.POST.equals(httpMethod) && !HttpMethod.PUT.equals(httpMethod) && !HttpMethod.PATCH.equals(httpMethod) && !HttpMethod.DELETE.equals(httpMethod)) {
// Setting Required Headers
if (!StringUtil.isNullOrEmpty(basicAuth)) {
LOGGER.debug("[invoke] Setting authorization in header as " + IAuthConstants.Header.AUTHORIZATION);
httpUriRequest.setHeader(IAuthConstants.Header.AUTHORIZATION, basicAuth);
httpUriRequest.setHeader(IAuthConstants.Header.CORRELATION_ID, requestContext.getCorrelationId());
}
}
if (HttpMethod.POST.equals(httpMethod) || HttpMethod.PUT.equals(httpMethod) || HttpMethod.PATCH.equals(httpMethod)) {
LOGGER.info("[invoke] Executing POST/ PUT request : httpEntityEnclosingRequest : " + httpEntityEnclosingRequest + " : payload : " + payload);
// Setting POST/PUT related headers
httpEntityEnclosingRequest.setHeader(HttpHeaders.CONTENT_TYPE, contentType);
httpEntityEnclosingRequest.setHeader(IAuthConstants.Header.AUTHORIZATION, basicAuth);
httpEntityEnclosingRequest.setHeader(IAuthConstants.Header.CORRELATION_ID, requestContext.getCorrelationId());
// Setting request payload
httpEntityEnclosingRequest.setEntity(new StringEntity(payload));
httpResponse = client.execute(httpEntityEnclosingRequest);
LOGGER.debug("[invoke] Call executed successfully");
} else if (HttpMethod.DELETE.equals(httpMethod)) {
httpEntityEnclosingRequest.setURI(URI.create(apiUrl));
LOGGER.info("[invoke] Executing DELETE request : httpDeleteRequest : " + httpEntityEnclosingRequest + " : payload : " + payload);
// Setting DELETE related headers
httpEntityEnclosingRequest.setHeader(HttpHeaders.CONTENT_TYPE, contentType);
httpEntityEnclosingRequest.setHeader(IAuthConstants.Header.EXTRA_AUTH, String.valueOf(System.currentTimeMillis()));
httpEntityEnclosingRequest.setHeader(IAuthConstants.Header.AUTHORIZATION, basicAuth);
httpEntityEnclosingRequest.setHeader(IAuthConstants.Header.CORRELATION_ID, requestContext.getCorrelationId());
// Setting request payload
httpEntityEnclosingRequest.setEntity(new StringEntity(payload));
httpResponse = client.execute(httpEntityEnclosingRequest);
LOGGER.debug("[invoke] Call executed successfully");
} else {
LOGGER.info("[invoke] Executing : httpUriRequest : " + httpUriRequest);
httpResponse = client.execute(httpUriRequest);
LOGGER.info("[invoke] Call executed successfully");
}
} catch (Exception e) {
LOGGER.error("Error occurred while trying to communicate third party service provider", e);
throw new RestCallFailedException(e);
}
return httpResponse;
}
use of org.openkilda.auth.model.RequestContext in project open-kilda by telstra.
the class RequestInterceptor method updateRequestContext.
private void updateRequestContext(final String correlationId, final HttpServletRequest request, final UserInfo userInfo) {
RequestContext requestContext = serverContext.getRequestContext();
requestContext.setCorrelationId(userInfo.getUsername() + "_" + correlationId);
requestContext.setUserId(userInfo.getUserId());
requestContext.setUserName(userInfo.getUsername());
requestContext.setFullName(userInfo.getName());
requestContext.setPermissions(userInfo.getPermissions());
requestContext.setIs2FaEnabled(userInfo.getIs2FaEnabled());
requestContext.setStatus(userInfo.getStatus());
requestContext.setClientIpAddress(getClientIp(request));
MDC.put(CORRELATION_ID, requestContext.getCorrelationId());
}
use of org.openkilda.auth.model.RequestContext in project open-kilda by telstra.
the class ActivityLogger method getLogInfo.
private LogInfo getLogInfo(final ActivityType activityType, final String objectId) {
LogInfo logInfo = new LogInfo();
RequestContext requestContext = serverContext.getRequestContext();
logInfo.setUserId(requestContext.getUserId());
logInfo.setActivityType(activityType);
logInfo.setObjectId(objectId);
logInfo.setActivityTime(Calendar.getInstance().getTime());
logInfo.setClientIpAddress(requestContext.getClientIpAddress());
return logInfo;
}
use of org.openkilda.auth.model.RequestContext in project open-kilda by telstra.
the class RequestInterceptor method preHandle.
@Override
public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) throws AccessDeniedException {
String correlationId = request.getParameter(CORRELATION_ID);
correlationId = correlationId == null ? UUID.randomUUID().toString() : correlationId;
HttpSession session = request.getSession();
UserInfo userInfo = null;
if (IConstants.SessionTimeout.TIME_IN_MINUTE == null) {
IConstants.SessionTimeout.TIME_IN_MINUTE = Integer.valueOf(applicationSettingService.getApplicationSettings().get(ApplicationSetting.SESSION_TIMEOUT.name()));
}
session.setMaxInactiveInterval(IConstants.SessionTimeout.TIME_IN_MINUTE * 60);
userInfo = (UserInfo) session.getAttribute(IConstants.SESSION_OBJECT);
if (userInfo != null) {
validateUser(userInfo);
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Permissions permissions = handlerMethod.getMethod().getAnnotation(Permissions.class);
if (permissions != null) {
validateAndPopulatePermisssion(userInfo, permissions);
}
}
updateRequestContext(correlationId, request, userInfo);
} else {
RequestContext requestContext = serverContext.getRequestContext();
requestContext.setCorrelationId(correlationId);
}
return true;
}
use of org.openkilda.auth.model.RequestContext in project open-kilda by telstra.
the class UserService method getLoggedInUserInfo.
/**
* Gets the logged in user info.
*
* @return the logged in user info
* @throws AccessDeniedException the access denied exception
*/
public UserInfo getLoggedInUserInfo() throws AccessDeniedException {
RequestContext requestContext = serverContext.getRequestContext();
if (requestContext.getUserId() == null) {
throw new AccessDeniedException(messageUtils.getUnauthorizedMessage());
}
UserInfo userInfo = new UserInfo();
userInfo.setUserId(requestContext.getUserId());
userInfo.setUsername(requestContext.getUserName());
userInfo.setIs2FaEnabled(requestContext.getIs2FaEnabled());
userInfo.setStatus(requestContext.getStatus());
userInfo.setName(requestContext.getFullName());
userInfo.setPermissions(requestContext.getPermissions());
return userInfo;
}
Aggregations