Search in sources :

Example 1 with Context

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);
}
Also used : lombok.val(lombok.val) DateTimeUtils(org.joda.time.DateTimeUtils) Reflection(oap.reflect.Reflection) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.testng.annotations.Test) Collections.singletonList(java.util.Collections.singletonList) Reflect(oap.reflect.Reflect) InetAddress(java.net.InetAddress) Arrays.asList(java.util.Arrays.asList) USER_ID(oap.ws.Interceptor.USER_ID) Session(oap.http.Session) MockRequest(oap.http.testng.MockRequest) AclService(oap.security.acl.AclService) Id(oap.util.Id) Context(oap.http.Context) WsParam(oap.ws.WsParam) lombok.val(lombok.val) UUID(java.util.UUID) Mockito.when(org.mockito.Mockito.when) UnknownHostException(java.net.UnknownHostException) Request(oap.http.Request) List(java.util.List) HttpGet(org.apache.http.client.methods.HttpGet) Optional(java.util.Optional) Protocol(oap.http.Protocol) Mockito.mock(org.mockito.Mockito.mock) Context(oap.http.Context) HttpGet(org.apache.http.client.methods.HttpGet) MockRequest(oap.http.testng.MockRequest) Request(oap.http.Request) Session(oap.http.Session) Test(org.testng.annotations.Test)

Example 2 with Context

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();
}
Also used : Context(oap.http.Context) HttpCoreContext(org.apache.http.protocol.HttpCoreContext) HttpContext(org.apache.http.protocol.HttpContext) Response(oap.http.Response) HttpResponse(org.apache.http.HttpResponse) HttpRequest(org.apache.http.HttpRequest) Request(oap.http.Request) HttpResponse(org.apache.http.HttpResponse) HttpInetConnection(org.apache.http.HttpInetConnection) InetAddress(java.net.InetAddress)

Example 3 with Context

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"));
}
Also used : Context(oap.http.Context) Reflection(oap.reflect.Reflection) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test) UUID(java.util.UUID) Mockito.when(org.mockito.Mockito.when) Assert.assertNotNull(org.testng.Assert.assertNotNull) HttpRequest(org.apache.http.HttpRequest) UnknownHostException(java.net.UnknownHostException) Request(oap.http.Request) Reflect(oap.reflect.Reflect) InetAddress(java.net.InetAddress) HttpGet(org.apache.http.client.methods.HttpGet) Session(oap.http.Session) Optional(java.util.Optional) Protocol(oap.http.Protocol) Assert.assertFalse(org.testng.Assert.assertFalse) HttpResponse(oap.http.HttpResponse) Mockito.mock(org.mockito.Mockito.mock) Context(oap.http.Context) HttpRequest(org.apache.http.HttpRequest) HttpGet(org.apache.http.client.methods.HttpGet) HttpRequest(org.apache.http.HttpRequest) Request(oap.http.Request) HttpResponse(oap.http.HttpResponse) Reflection(oap.reflect.Reflection) Session(oap.http.Session) Test(org.testng.annotations.Test)

Example 4 with Context

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");
}
Also used : Context(oap.http.Context) Request(oap.http.Request) BasicHttpRequest(org.apache.http.message.BasicHttpRequest) BasicHttpRequest(org.apache.http.message.BasicHttpRequest) Test(org.testng.annotations.Test)

Example 5 with Context

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);
}
Also used : Context(oap.http.Context) Request(oap.http.Request) BasicHttpRequest(org.apache.http.message.BasicHttpRequest) BasicHttpRequest(org.apache.http.message.BasicHttpRequest)

Aggregations

Context (oap.http.Context)5 Request (oap.http.Request)5 InetAddress (java.net.InetAddress)3 Test (org.testng.annotations.Test)3 UnknownHostException (java.net.UnknownHostException)2 Optional (java.util.Optional)2 UUID (java.util.UUID)2 Protocol (oap.http.Protocol)2 Session (oap.http.Session)2 Reflect (oap.reflect.Reflect)2 Reflection (oap.reflect.Reflection)2 HttpRequest (org.apache.http.HttpRequest)2 HttpGet (org.apache.http.client.methods.HttpGet)2 BasicHttpRequest (org.apache.http.message.BasicHttpRequest)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.when (org.mockito.Mockito.when)2 Arrays.asList (java.util.Arrays.asList)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 lombok.val (lombok.val)1