/** * Returns the RFC 7235 authorization challenges appropriate for this response's code. If the * response code is 401 unauthorized, this returns the "WWW-Authenticate" challenges. If the * response code is 407 proxy unauthorized, this returns the "Proxy-Authenticate" challenges. * Otherwise this returns an empty list of challenges. * * <p>If a challenge uses the {@code token68} variant instead of auth params, there is exactly one * auth param in the challenge at key {@code null}. Invalid headers and challenges are ignored. * No semantic validation is done, for example that {@code Basic} auth must have a {@code realm} * auth param, this is up to the caller that interprets these challenges. */ public List<Challenge> challenges() { String responseField; if (code == HTTP_UNAUTHORIZED) { responseField = "WWW-Authenticate"; } else if (code == HTTP_PROXY_AUTH) { responseField = "Proxy-Authenticate"; } else { return Collections.emptyList(); } return HttpHeaders.parseChallenges(headers(), responseField); }
/** * Returns the RFC 7235 authorization challenges appropriate for this response's code. If the * response code is 401 unauthorized, this returns the "WWW-Authenticate" challenges. If the * response code is 407 proxy unauthorized, this returns the "Proxy-Authenticate" challenges. * Otherwise this returns an empty list of challenges. * * <p>If a challenge uses the {@code token68} variant instead of auth params, there is exactly one * auth param in the challenge at key {@code null}. Invalid headers and challenges are ignored. * No semantic validation is done, for example that {@code Basic} auth must have a {@code realm} * auth param, this is up to the caller that interprets these challenges. */ public List<Challenge> challenges() { String responseField; if (code == HTTP_UNAUTHORIZED) { responseField = "WWW-Authenticate"; } else if (code == HTTP_PROXY_AUTH) { responseField = "Proxy-Authenticate"; } else { return Collections.emptyList(); } return HttpHeaders.parseChallenges(headers(), responseField); }
/** * Returns the authorization challenges appropriate for this response's code. If the response code * is 401 unauthorized, this returns the "WWW-Authenticate" challenges. If the response code is * 407 proxy unauthorized, this returns the "Proxy-Authenticate" challenges. Otherwise this * returns an empty list of challenges. */ public List<Challenge> challenges() { String responseField; if (code == HTTP_UNAUTHORIZED) { responseField = "WWW-Authenticate"; } else if (code == HTTP_PROXY_AUTH) { responseField = "Proxy-Authenticate"; } else { return Collections.emptyList(); } return HttpHeaders.parseChallenges(headers(), responseField); }
/** * Returns the RFC 7235 authorization challenges appropriate for this response's code. If the * response code is 401 unauthorized, this returns the "WWW-Authenticate" challenges. If the * response code is 407 proxy unauthorized, this returns the "Proxy-Authenticate" challenges. * Otherwise this returns an empty list of challenges. * * <p>If a challenge uses the {@code token68} variant instead of auth params, there is exactly one * auth param in the challenge at key {@code null}. Invalid headers and challenges are ignored. * No semantic validation is done, for example that {@code Basic} auth must have a {@code realm} * auth param, this is up to the caller that interprets these challenges. */ public List<Challenge> challenges() { String responseField; if (code == HTTP_UNAUTHORIZED) { responseField = "WWW-Authenticate"; } else if (code == HTTP_PROXY_AUTH) { responseField = "Proxy-Authenticate"; } else { return Collections.emptyList(); } return HttpHeaders.parseChallenges(headers(), responseField); }
@Test public void testWithWrongOrder() { // Not strict RFC 2617 header. Headers headers = new Headers.Builder() .add("WWW-Authenticate", "Digest qop=\"auth\", realm=\"myrealm\", nonce=\"fjalskdflwejrlaskdfjlaskdjflaksjdflkasdf\", stale=\"FALSE\"").build(); List<Challenge> challenges = HttpHeaders.parseChallenges(headers, "WWW-Authenticate"); assertEquals(1, challenges.size()); assertEquals(1, challenges.size()); assertEquals("Digest", challenges.get(0).scheme()); assertEquals("myrealm", challenges.get(0).realm()); } }
/** * See: https://github.com/square/okhttp/issues/2780 */ @Test public void testWithCorrectOrder() { // Strict RFC 2617 header Headers headers = new Headers.Builder() .add("WWW-Authenticate", "Digest realm=\"myrealm\", nonce=\"fjalskdflwejrlaskdfjlaskdjflaksjdflkasdf\", qop=\"auth\", stale=\"FALSE\"").build(); List<Challenge> challenges = HttpHeaders.parseChallenges(headers, "WWW-Authenticate"); assertEquals(1, challenges.size()); assertEquals("Digest", challenges.get(0).scheme()); assertEquals("myrealm", challenges.get(0).realm()); }