Authorizer implements LoggableResource { public Future<Boolean> authorize(final HttpServerRequest request) { Future<Boolean> future = Future.future(); handleUserUriRequest(request, future); if (!future.isComplete()) { roleAuthorizer.handleIsAuthorized(request, future); } if (!future.isComplete()) { handleConfigurationUriRequest(request, future, aclUriPattern, roleAuthorizer); } if (!future.isComplete()) { handleConfigurationUriRequest(request, future, roleMapperUriPattern, roleMapper); } if (!future.isComplete()) { future.complete(Boolean.TRUE); } return future; } Authorizer(Vertx vertx, final ResourceStorage storage, String securityRoot, String rolePattern);  Authorizer(Vertx vertx, final ResourceStorage storage, String securityRoot, String rolePattern, String rolePrefix, Map<String, Object> properties); @Override void enableResourceLogging(boolean resourceLoggingEnabled); Future<Boolean> authorize(final HttpServerRequest request); void authorize(final HttpServerRequest request, final Handler<Void> handler); static final Logger log; }
@Test public void testAuthorizeUserUriGETRequest(TestContext context) { String requestUri = "/gateleen/server/security/v1/user"; CaseInsensitiveHeaders headers = new CaseInsensitiveHeaders(); headers.add("x-rp-usr", "user_1234"); headers.add("x-rp-grp", "z-gateleen-admin,z-gateleen-authenticated,z-gateleen-developer"); DummyHttpServerResponse response = Mockito.spy(new DummyHttpServerResponse()); AuthorizerRequest req = new AuthorizerRequest(HttpMethod.GET, requestUri, headers, response); authorizer.authorize(req).setHandler(event -> { context.assertTrue(event.succeeded()); context.assertFalse(event.result()); }); Mockito.verify(response, timeout(1000).times(1)) .end(eq("{\"userId\":\"user_1234\",\"roles\":[\"authenticated\",\"everyone\",\"admin\",\"developer\"]}")); }
@Test public void testAuthorizeUserUriGETRequestWithCASName(TestContext context) { String requestUri = "/gateleen/server/security/v1/user"; CaseInsensitiveHeaders headers = new CaseInsensitiveHeaders(); headers.add("cas_name", "cas_user_1234"); headers.add("x-rp-grp", "z-gateleen-admin,z-gateleen-authenticated,z-gateleen-developer"); DummyHttpServerResponse response = Mockito.spy(new DummyHttpServerResponse()); AuthorizerRequest req = new AuthorizerRequest(HttpMethod.GET, requestUri, headers, response); authorizer.authorize(req).setHandler(event -> { context.assertTrue(event.succeeded()); context.assertFalse(event.result()); }); Mockito.verify(response, timeout(1000).times(1)) .end(eq("{\"userId\":\"cas_user_1234\",\"roles\":[\"authenticated\",\"everyone\",\"admin\",\"developer\"]}")); }
@Test public void testAuthorizeUserUriDELETERequest(TestContext context) { String requestUri = "/gateleen/server/security/v1/user"; CaseInsensitiveHeaders headers = new CaseInsensitiveHeaders(); headers.add("x-rp-usr", "user_1234"); headers.add("x-rp-grp", "z-gateleen-admin,z-gateleen-authenticated,z-gateleen-developer"); DummyHttpServerResponse response = Mockito.spy(new DummyHttpServerResponse()); AuthorizerRequest req = new AuthorizerRequest(HttpMethod.DELETE, requestUri, headers, response); authorizer.authorize(req).setHandler(event -> { context.assertTrue(event.succeeded()); context.assertFalse(event.result()); }); Mockito.verify(response, timeout(1000).times(1)).setStatusCode(eq(StatusCode.METHOD_NOT_ALLOWED.getStatusCode())); Mockito.verify(response, timeout(1000).times(1)).setStatusMessage(eq(StatusCode.METHOD_NOT_ALLOWED.getStatusMessage())); Mockito.verify(response, timeout(1000).times(1)).end(); }
@Test public void testAuthorizeUserUriPUTRequest(TestContext context) { String requestUri = "/gateleen/server/security/v1/user"; CaseInsensitiveHeaders headers = new CaseInsensitiveHeaders(); headers.add("x-rp-usr", "user_1234"); headers.add("x-rp-grp", "z-gateleen-admin,z-gateleen-authenticated,z-gateleen-developer"); DummyHttpServerResponse response = Mockito.spy(new DummyHttpServerResponse()); AuthorizerRequest req = new AuthorizerRequest(HttpMethod.PUT, requestUri, headers, response); authorizer.authorize(req).setHandler(event -> { context.assertTrue(event.succeeded()); context.assertFalse(event.result()); }); Mockito.verify(response, timeout(1000).times(1)).setStatusCode(eq(StatusCode.METHOD_NOT_ALLOWED.getStatusCode())); Mockito.verify(response, timeout(1000).times(1)).setStatusMessage(eq(StatusCode.METHOD_NOT_ALLOWED.getStatusMessage())); Mockito.verify(response, timeout(1000).times(1)).end(); }
@Test public void testAuthorizeAclUriGETRequest(TestContext context) { String requestUri = "/gateleen/server/security/v1/acls/admin"; DummyHttpServerResponse response = Mockito.spy(new DummyHttpServerResponse()); AuthorizerRequest req = new AuthorizerRequest(HttpMethod.GET, requestUri, new CaseInsensitiveHeaders(), response); authorizer.authorize(req).setHandler(event -> { context.assertTrue(event.succeeded()); context.assertTrue(event.result()); }); Mockito.verifyZeroInteractions(response); }
@Test public void testAuthorizeAclUriDELETERequest(TestContext context) { String requestUri = "/gateleen/server/security/v1/acls/admin"; DummyHttpServerResponse response = Mockito.spy(new DummyHttpServerResponse()); AuthorizerRequest req = new AuthorizerRequest(HttpMethod.DELETE, requestUri, new CaseInsensitiveHeaders(), response); authorizer.authorize(req).setHandler(event -> { context.assertTrue(event.succeeded()); context.assertFalse(event.result()); }); Mockito.verify(response, timeout(1000).times(1)).end(); }
@Test public void testAuthorizeAclUriPUTRequest(TestContext context) { String requestUri = "/gateleen/server/security/v1/acls/admin"; DummyHttpServerResponse response = Mockito.spy(new DummyHttpServerResponse()); AuthorizerRequest req = new AuthorizerRequest(HttpMethod.PUT, requestUri, new CaseInsensitiveHeaders(), "{}", response); authorizer.authorize(req).setHandler(event -> { context.assertTrue(event.succeeded()); context.assertFalse(event.result()); }); Mockito.verify(response, timeout(1000).times(1)).end(); }
@Test public void testAuthorizeAclUriPUTRequestInvalid(TestContext context) { String requestUri = "/gateleen/server/security/v1/acls/admin"; DummyHttpServerResponse response = Mockito.spy(new DummyHttpServerResponse()); AuthorizerRequest req = new AuthorizerRequest(HttpMethod.PUT, requestUri, new CaseInsensitiveHeaders(), "{invalidJson}", response); authorizer.authorize(req).setHandler(event -> { context.assertTrue(event.succeeded()); context.assertFalse(event.result()); }); Mockito.verify(response, timeout(1000).times(1)).setStatusCode(eq(StatusCode.BAD_REQUEST.getStatusCode())); Mockito.verify(response, timeout(1000).times(1)).setStatusMessage(eq("Bad Request Unable to parse json")); Mockito.verify(response, timeout(1000).times(1)).end("Unable to parse json"); }
@Test public void testHandleIsAuthorized(TestContext context) { String requestUri = "/gateleen/server/tests/someResource"; CaseInsensitiveHeaders headers = new CaseInsensitiveHeaders(); headers.add("x-rp-grp", "z-gateleen-admin,z-gateleen-authenticated,z-gateleen-developer"); DummyHttpServerResponse response = Mockito.spy(new DummyHttpServerResponse()); AuthorizerRequest req = new AuthorizerRequest(HttpMethod.PUT, requestUri, headers, response); authorizer.authorize(req).setHandler(event -> { context.assertTrue(event.succeeded()); context.assertTrue(event.result()); }); Mockito.verifyZeroInteractions(response); }
@Test public void testHandleNotIsAuthorized(TestContext context) { String requestUri = "/gateleen/server/tests/someResource"; CaseInsensitiveHeaders headers = new CaseInsensitiveHeaders(); headers.add("x-rp-grp", "z-gateleen-authenticated,z-gateleen-developer"); DummyHttpServerResponse response = Mockito.spy(new DummyHttpServerResponse()); AuthorizerRequest req = new AuthorizerRequest(HttpMethod.PUT, requestUri, headers, response); authorizer.authorize(req).setHandler(event -> { context.assertTrue(event.succeeded()); context.assertFalse(event.result()); }); Mockito.verify(response, timeout(1000).times(1)).setStatusCode(eq(StatusCode.FORBIDDEN.getStatusCode())); Mockito.verify(response, timeout(1000).times(1)).setStatusMessage(eq(StatusCode.FORBIDDEN.getStatusMessage())); Mockito.verify(response, timeout(1000).times(1)).end(eq(StatusCode.FORBIDDEN.getStatusMessage())); }
@Test public void testHandleAclRoleMapper(TestContext context) { String requestUri = "/gateleen/domain/tests/someResource"; CaseInsensitiveHeaders headers = new CaseInsensitiveHeaders(); headers.add("x-rp-grp", "z-gateleen-domain-admin-stage-int"); DummyHttpServerResponse response = Mockito.spy(new DummyHttpServerResponse()); AuthorizerRequest req = new AuthorizerRequest(HttpMethod.PUT, requestUri, headers, response); authorizer.authorize(req).setHandler(event -> { context.assertTrue(event.succeeded()); context.assertTrue(event.result()); context.assertTrue(req != null); Set<String> roles = new RoleExtractor().extractRoles(req); context.assertTrue(roles.size() == 3); context.assertTrue(roles.contains("z-gateleen-domain-admin-stage-int")); context.assertTrue(roles.contains("z-gateleen-domain-admin-int")); context.assertTrue(roles.contains("z-gateleen-everyone")); }); Mockito.verifyZeroInteractions(response); }