use of oap.http.Context in project oap by oaplatform.
the class SecurityInterceptor2Test method testShouldVerifyAndSetUserInSessionIfAuthorizationHeaderIsPresent.
@Test
public void testShouldVerifyAndSetUserInSessionIfAuthorizationHeaderIsPresent() throws UnknownHostException {
val methodWithAnnotation = REFLECTION.method(method -> method.name().equals("methodWithAnnotation")).get();
val context = new Context("/", InetAddress.getLocalHost(), Protocol.HTTP.name());
val tokenId = UUID.randomUUID().toString();
val httpRequest = new HttpGet();
httpRequest.setHeader("Authorization", tokenId);
httpRequest.setHeader("Host", "localhost");
val request = new Request(httpRequest, context);
val userId = "testUser";
val token = new Token2(tokenId, userId, DateTimeUtils.currentTimeMillis());
when(mockTokenService.getToken(tokenId)).thenReturn(Optional.of(token));
val session = new Session();
when(mockAclService.checkOne("obj", userId, "parent.read")).thenReturn(true);
val httpResponse = securityInterceptor.intercept(request, session, methodWithAnnotation, p -> "obj");
assertThat(httpResponse).isEmpty();
assertThat(session.get(USER_ID)).contains(userId);
}
use of oap.http.Context in project oap by oaplatform.
the class NioHandlerAdapter method handle.
@Override
public void handle(final HttpRequest httpRequest, final HttpAsyncExchange httpAsyncExchange, final HttpContext httpContext) throws HttpException, IOException {
LOGGER.trace("handling [{}]", httpRequest);
final HttpInetConnection connection = (HttpInetConnection) httpContext.getAttribute(HttpCoreContext.HTTP_CONNECTION);
final InetAddress remoteAddress = connection.getRemoteAddress();
final HttpResponse response = httpAsyncExchange.getResponse();
final String httpContextProtocol = String.valueOf(httpContext.getAttribute("protocol"));
if (Protocol.LOCAL.equals(this.protocol) && !Inet.isLocalAddress(remoteAddress)) {
response.setStatusCode(HTTP_FORBIDDEN);
} else {
Request request = new Request(httpRequest, new Context(location, remoteAddress, httpContextProtocol));
handler.handle(request, new Response(response, corsPolicy.getCors(request)));
}
httpAsyncExchange.submitResponse();
}
use of oap.http.Context in project oap by oaplatform.
the class SecurityInterceptorTest method testShouldVerifyAndSetUserInSessionIfAuthorizationHeaderIsPresent.
@Test
public void testShouldVerifyAndSetUserInSessionIfAuthorizationHeaderIsPresent() throws UnknownHostException {
final Reflection.Method methodWithAnnotation = REFLECTION.method(method -> method.name().equals("methodWithAnnotation")).get();
final Context context = new Context("/", InetAddress.getLocalHost(), Protocol.HTTP.name());
final String tokenId = UUID.randomUUID().toString();
final HttpRequest httpRequest = new HttpGet();
httpRequest.setHeader("Authorization", tokenId);
httpRequest.setHeader("Host", "localhost");
final Request request = new Request(httpRequest, context);
final User user = new DefaultUser(Role.ADMIN, "testOrg", "test@example.com");
final Token token = new Token();
token.user = new DefaultUser(user);
token.id = tokenId;
token.created = DateTime.now();
when(mockTokenService.getToken(tokenId)).thenReturn(Optional.of(token));
final Session session = new Session();
final Optional<HttpResponse> httpResponse = securityInterceptor.intercept(request, session, methodWithAnnotation, p -> null);
assertFalse(httpResponse.isPresent());
assertNotNull(session.get("user"));
}
use of oap.http.Context in project oap by oaplatform.
the class GenericCorsPolicyTest method testShouldVerifyDefaultAllowMethods.
@Test
public void testShouldVerifyDefaultAllowMethods() throws UnknownHostException {
final BasicHttpRequest basicHttpRequest = new BasicHttpRequest("GET", "http://test.com");
basicHttpRequest.addHeader("Origin", "*");
basicHttpRequest.addHeader("Host", "some-host");
final Request request = new Request(basicHttpRequest, new Context("not important", InetAddress.getLocalHost(), "not important"));
final RequestCors requestCors = GenericCorsPolicy.DEFAULT.getCors(request);
assertThat(requestCors.allowMethods).isEqualTo("HEAD, POST, GET, PUT, DELETE, OPTIONS");
}
use of oap.http.Context in project oap by oaplatform.
the class PatternCorsPolicyTest method getRequest.
private static Request getRequest(final String origin, final String url) throws UnknownHostException {
final BasicHttpRequest basicHttpRequest = new BasicHttpRequest("GET", url);
basicHttpRequest.addHeader("Origin", origin);
basicHttpRequest.addHeader("Host", "some-host");
final Context context = new Context("not important", InetAddress.getLocalHost(), "not important");
return new Request(basicHttpRequest, context);
}
Aggregations