Search in sources :

Example 1 with RequestContext

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;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpPost(org.apache.http.client.methods.HttpPost) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) HttpGet(org.apache.http.client.methods.HttpGet) RestCallFailedException(org.openkilda.exception.RestCallFailedException) HttpResponse(org.apache.http.HttpResponse) HttpPut(org.apache.http.client.methods.HttpPut) HttpPatch(org.apache.http.client.methods.HttpPatch) ExternalSystemException(org.openkilda.exception.ExternalSystemException) RestCallFailedException(org.openkilda.exception.RestCallFailedException) InvalidResponseException(org.openkilda.integration.exception.InvalidResponseException) UnauthorizedException(org.openkilda.exception.UnauthorizedException) IOException(java.io.IOException) StringEntity(org.apache.http.entity.StringEntity) HttpClient(org.apache.http.client.HttpClient) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestContext(org.openkilda.auth.model.RequestContext)

Example 2 with RequestContext

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());
}
Also used : RequestContext(org.openkilda.auth.model.RequestContext)

Example 3 with RequestContext

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;
}
Also used : LogInfo(org.openkilda.log.model.LogInfo) RequestContext(org.openkilda.auth.model.RequestContext)

Example 4 with RequestContext

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;
}
Also used : HttpSession(javax.servlet.http.HttpSession) Permissions(org.openkilda.auth.model.Permissions) UserInfo(org.usermanagement.model.UserInfo) RequestContext(org.openkilda.auth.model.RequestContext) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 5 with RequestContext

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;
}
Also used : AccessDeniedException(java.nio.file.AccessDeniedException) UserInfo(org.usermanagement.model.UserInfo) RequestContext(org.openkilda.auth.model.RequestContext)

Aggregations

RequestContext (org.openkilda.auth.model.RequestContext)7 UserInfo (org.usermanagement.model.UserInfo)2 IOException (java.io.IOException)1 AccessDeniedException (java.nio.file.AccessDeniedException)1 HttpSession (javax.servlet.http.HttpSession)1 HttpResponse (org.apache.http.HttpResponse)1 HttpClient (org.apache.http.client.HttpClient)1 HttpEntityEnclosingRequestBase (org.apache.http.client.methods.HttpEntityEnclosingRequestBase)1 HttpGet (org.apache.http.client.methods.HttpGet)1 HttpPatch (org.apache.http.client.methods.HttpPatch)1 HttpPost (org.apache.http.client.methods.HttpPost)1 HttpPut (org.apache.http.client.methods.HttpPut)1 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)1 StringEntity (org.apache.http.entity.StringEntity)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1 Before (org.junit.Before)1 Permissions (org.openkilda.auth.model.Permissions)1 BaseEntity (org.openkilda.entity.BaseEntity)1 ExternalSystemException (org.openkilda.exception.ExternalSystemException)1 RestCallFailedException (org.openkilda.exception.RestCallFailedException)1