Http { public static String authorizedUser(HttpServletRequest request, AuthorityList authorities, Authorizer authorizer, String action, String resource, String otherDomain) { Principal principal = authenticate(request, authorities); authorize(authorizer, principal, action, resource, otherDomain); return principal.getFullName(); }  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 testAuthorizedUserUserInvalidCredentials() { HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class); Authorizer authorizer = Mockito.mock(Authorizer.class); Http.AuthorityList authorities = new Http.AuthorityList(); try { Http.authorizedUser(httpServletRequest, authorities, authorizer, "action", null, null); } catch (ResourceException expected) { assertEquals(expected.getCode(), 401); } }
@Test public void testAuthorizedUser() { HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class); Principal principal = Mockito.mock(Principal.class); Mockito.when(principal.getFullName()).thenReturn("athenz.api"); Authorizer authorizer = Mockito.mock(Authorizer.class); Mockito.when(authorizer.access(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(Principal.class), ArgumentMatchers.any())).thenReturn(true); Authority authority = Mockito.mock(Authority.class); Mockito.when(authority.getCredSource()).thenReturn(Authority.CredSource.HEADER); Mockito.when(authority.getHeader()).thenReturn("Athenz-Principal-Auth"); Mockito.when(httpServletRequest.getHeader("Athenz-Principal-Auth")).thenReturn("Creds"); Mockito.when(authority.authenticate(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(principal); Http.AuthorityList authorities = new Http.AuthorityList(); authorities.add(authority); assertEquals("athenz.api", Http.authorizedUser(httpServletRequest, authorities, authorizer, "action", "resource", null)); }