Search in sources :

Example 6 with HttpResponse

use of oap.http.HttpResponse 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 7 with HttpResponse

use of oap.http.HttpResponse in project oap by oaplatform.

the class LoginWS method login.

@WsMethod(method = GET, path = "/")
public HttpResponse login(@WsParam(from = QUERY) String email, @WsParam(from = QUERY) String password) {
    final Optional<Token> optionalToken = authService.generateToken(email, password);
    if (optionalToken.isPresent()) {
        final Token token = optionalToken.get();
        final HttpResponse ok = HttpResponse.ok(token);
        return withAuthorization(ok, token);
    } else {
        return HttpResponse.status(HTTP_UNAUTHORIZED, "Username or password is invalid");
    }
}
Also used : HttpResponse(oap.http.HttpResponse) WsMethod(oap.ws.WsMethod)

Example 8 with HttpResponse

use of oap.http.HttpResponse in project oap by oaplatform.

the class SecurityInterceptor method intercept.

@Override
public Optional<HttpResponse> intercept(Request request, Session session, Reflection.Method method, Function<Reflection.Parameter, Object> getParameterValueFunc) {
    final Optional<WsSecurity> annotation = method.findAnnotation(WsSecurity.class);
    if (annotation.isPresent()) {
        if (session == null) {
            final HttpResponse httpResponse = HttpResponse.status(500, "Session doesn't exist; check if service is session aware");
            log.error(httpResponse.toString());
            return Optional.of(httpResponse);
        }
        User user = (User) session.get("user").orElse(null);
        if (user != null) {
            log.trace("User [{}] found in session", user.getEmail());
            final Role methodRole = annotation.get().role();
            if (user.getRole().precedence > methodRole.precedence) {
                final HttpResponse httpResponse = HttpResponse.status(403, format("User [%s] has no access to method [%s]", user.getEmail(), method.name()));
                log.debug(httpResponse.toString());
                return Optional.of(httpResponse);
            }
        } else {
            final String sessionToken = request.header("Authorization").orElse(request.cookie("Authorization").orElse(null));
            if (sessionToken == null) {
                final HttpResponse httpResponse = HttpResponse.status(401, "Session token is missing in header or cookie");
                log.debug(httpResponse.toString());
                return Optional.of(httpResponse);
            }
            final Token token = tokenService.getToken(sessionToken).orElse(null);
            if (token == null) {
                final HttpResponse httpResponse = HttpResponse.status(401, format("Token id [%s] expired or was " + "not created", sessionToken));
                log.debug(httpResponse.toString());
                return Optional.of(httpResponse);
            }
            user = token.user;
            session.set("user", user);
            session.set(USER_ID, user.getEmail());
            final Role methodRole = annotation.get().role();
            if (user.getRole().precedence > methodRole.precedence) {
                final HttpResponse httpResponse = HttpResponse.status(403, format("User [%s] has no access to method [%s]", user.getEmail(), method.name()));
                log.debug(httpResponse.toString());
                return Optional.of(httpResponse);
            }
        }
    }
    return Optional.empty();
}
Also used : HttpResponse(oap.http.HttpResponse)

Aggregations

HttpResponse (oap.http.HttpResponse)8 Optional (java.util.Optional)3 UUID (java.util.UUID)3 Request (oap.http.Request)3 Session (oap.http.Session)3 Reflect (oap.reflect.Reflect)3 Reflection (oap.reflect.Reflection)3 DateTime (org.joda.time.DateTime)3 Test (org.testng.annotations.Test)3 InetAddress (java.net.InetAddress)2 UnknownHostException (java.net.UnknownHostException)2 lombok.val (lombok.val)2 Context (oap.http.Context)2 Protocol (oap.http.Protocol)2 HttpRequest (org.apache.http.HttpRequest)2 HttpGet (org.apache.http.client.methods.HttpGet)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.when (org.mockito.Mockito.when)2 Assert.assertFalse (org.testng.Assert.assertFalse)2 Assert.assertNotNull (org.testng.Assert.assertNotNull)2