Http { public static Principal authorize(Authorizer authorizer, Principal principal, String action, String resource, String otherDomain) { if (action == null || resource == null) { throw new ResourceException(ResourceException.BAD_REQUEST, "Missing 'action' and/or 'resource' parameters"); } if (principal.getMtlsRestricted()) { throw new ResourceException(ResourceException.FORBIDDEN, "mTLS Restricted"); } if (authorizer != null) { if (!authorizer.access(action, resource, principal, otherDomain)) { throw new ResourceException(ResourceException.FORBIDDEN, "Forbidden"); } } else { throw new ResourceException(ResourceException.INTERNAL_SERVER_ERROR, "No authorizer configured in service"); } return principal; }  static Principal authenticate(HttpServletRequest request,
            AuthorityList authorities); static Principal authenticate(HttpServletRequest request,
            AuthorityList authorities, boolean optionalAuth); static String authenticatedUser(HttpServletRequest request,
            AuthorityList authorities); static String authorizedUser(HttpServletRequest request,
            AuthorityList authorities, Authorizer authorizer, String action,
            String resource, String otherDomain); static Principal authorize(Authorizer authorizer, Principal principal,
            String action, String resource, String otherDomain); static final String WWW_AUTHENTICATE; static final String INVALID_CRED_ATTR; static final String AUTH_CHALLENGES; static final String JAVAX_CERT_ATTR; }
@Test public void testAuthorizedBadRequest() { Authorizer authorizer = Mockito.mock(Authorizer.class); Principal principal = Mockito.mock(Principal.class); try { Http.authorize(authorizer, principal, "action", null, null); } catch (ResourceException expected) { assertEquals(expected.getCode(), 400); } }
@Test public void testAuthorizedInternalServerError() { Principal principal = Mockito.mock(Principal.class); try { Http.authorize(null, principal, "action", "resource", null); } catch (ResourceException expected) { assertEquals(expected.getCode(), 500); } }
@Test public void testAuthorizedForbidden() { Authorizer authorizer = Mockito.mock(Authorizer.class); Principal principal = Mockito.mock(Principal.class); try { Http.authorize(authorizer, principal, "action", "resource", null); } catch (ResourceException expected) { assertEquals(expected.getCode(), 403); assertEquals(expected.getMessage(), "ResourceException (403): Forbidden"); } }
@Test public void testAuthorizedForbiddenMtlsRestricted() { Authorizer authorizer = Mockito.mock(Authorizer.class); Principal principal = Mockito.mock(Principal.class); Mockito.when(principal.getMtlsRestricted()).thenReturn(true); try { Http.authorize(authorizer, principal, "action", "resource", null); } catch (ResourceException expected) { assertEquals(expected.getCode(), 403); assertEquals(expected.getMessage(), "ResourceException (403): mTLS Restricted"); } }