ResourceContext { public void sendAuthenticateChallenges(ResourceException exc) { if (exc.getCode() != ResourceException.UNAUTHORIZED) { return; } Set<String> authChallenges = (Set<String>) request.getAttribute(Http.AUTH_CHALLENGES); if (authChallenges == null) { return; } if (SEND_MULTIPLE_WWW_AUTHENTICATE_HEADERS) { for (String challenge : authChallenges) { response.addHeader(Http.WWW_AUTHENTICATE, challenge); } } else { response.addHeader(Http.WWW_AUTHENTICATE, String.join(", ", authChallenges)); } } ResourceContext(HttpServletRequest request, HttpServletResponse response,
            Http.AuthorityList authorities, Authorizer authorizer); static void setSendMultipleWwwAuthenticateHeaders(boolean bSendMultipleHeaders); HttpServletRequest request(); HttpServletResponse response(); Principal principal(); Principal authenticate(); Principal authenticate(boolean optionalAuth); void authorize(String action, String resource, String trustedDomain); void sendAuthenticateChallenges(ResourceException exc); String getRequestDomain(); void setRequestDomain(String requestDomain);  }
@Test public void testSendAuthenticateChallengesNot401() { HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class); HttpServletResponse httpServletResponse = Mockito.mock(HttpServletResponse.class); Authorizer authorizer = Mockito.mock(Authorizer.class); Http.AuthorityList authorities = new Http.AuthorityList(); ResourceContext context = new ResourceContext(httpServletRequest, httpServletResponse, authorities, authorizer); ResourceException exc = new ResourceException(403); context.sendAuthenticateChallenges(exc); Mockito.verify(httpServletRequest, times(0)).getAttribute("com.yahoo.athenz.auth.credential.challenges"); Mockito.verify(httpServletResponse, times(0)).addHeader("WWW-Authenticate", "Negotiate"); }
@Test public void testSendAuthenticateChallengesNoChallenge() { HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class); HttpServletResponse httpServletResponse = Mockito.mock(HttpServletResponse.class); Authorizer authorizer = Mockito.mock(Authorizer.class); Http.AuthorityList authorities = new Http.AuthorityList(); ResourceContext context = new ResourceContext(httpServletRequest, httpServletResponse, authorities, authorizer); ResourceException exc = new ResourceException(401); Mockito.when(httpServletRequest.getAttribute("com.yahoo.athenz.auth.credential.challenges")).thenReturn(null); context.sendAuthenticateChallenges(exc); Mockito.verify(httpServletRequest, times(1)).getAttribute("com.yahoo.athenz.auth.credential.challenges"); Mockito.verify(httpServletResponse, times(0)).addHeader("WWW-Authenticate", "Negotiate"); }
@Test public void testSendAuthenticateChallengesMultipleHeaders() { HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class); HttpServletResponse httpServletResponse = Mockito.mock(HttpServletResponse.class); Authorizer authorizer = Mockito.mock(Authorizer.class); Http.AuthorityList authorities = new Http.AuthorityList(); ResourceContext context = new ResourceContext(httpServletRequest, httpServletResponse, authorities, authorizer); ResourceException exc = new ResourceException(401); Set<String> challenges = new HashSet<>(); challenges.add("Negotiate"); challenges.add("AthenzX509Certificate"); Mockito.when(httpServletRequest.getAttribute("com.yahoo.athenz.auth.credential.challenges")).thenReturn(challenges); context.sendAuthenticateChallenges(exc); Mockito.verify(httpServletRequest, times(1)).getAttribute("com.yahoo.athenz.auth.credential.challenges"); Mockito.verify(httpServletResponse, times(1)).addHeader("WWW-Authenticate", "Negotiate"); Mockito.verify(httpServletResponse, times(1)).addHeader("WWW-Authenticate", "AthenzX509Certificate"); }