1 /* 2 * ==================================================================== 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * ==================================================================== 20 * 21 * This software consists of voluntary contributions made by many 22 * individuals on behalf of the Apache Software Foundation. For more 23 * information on the Apache Software Foundation, please see 24 * <http://www.apache.org/>. 25 * 26 */ 27 28 package org.apache.http.client; 29 30 import java.util.Map; 31 import java.util.Queue; 32 33 import org.apache.http.Header; 34 import org.apache.http.HttpHost; 35 import org.apache.http.HttpResponse; 36 import org.apache.http.auth.AuthOption; 37 import org.apache.http.auth.AuthScheme; 38 import org.apache.http.auth.MalformedChallengeException; 39 import org.apache.http.protocol.HttpContext; 40 41 /** 42 /** 43 * A handler for determining if an HTTP response represents an authentication challenge that was 44 * sent back to the client as a result of authentication failure. 45 * <p> 46 * Implementations of this interface must be thread-safe. Access to shared data must be 47 * synchronized as methods of this interface may be executed from multiple threads. 48 * 49 * @since 4.2 50 */ 51 public interface AuthenticationStrategy { 52 53 /** 54 * Determines if the given HTTP response response represents 55 * an authentication challenge that was sent back as a result 56 * of authentication failure. 57 * 58 * @param authhost authentication host. 59 * @param response HTTP response. 60 * @param context HTTP context. 61 * @return {@code true} if user authentication is required, 62 * {@code false} otherwise. 63 */ 64 boolean isAuthenticationRequested( 65 HttpHost authhost, 66 HttpResponse response, 67 HttpContext context); 68 69 /** 70 * Extracts from the given HTTP response a collection of authentication 71 * challenges, each of which represents an authentication scheme supported 72 * by the authentication host. 73 * 74 * @param authhost authentication host. 75 * @param response HTTP response. 76 * @param context HTTP context. 77 * @return a collection of challenges keyed by names of corresponding 78 * authentication schemes. 79 * @throws MalformedChallengeException if one of the authentication 80 * challenges is not valid or malformed. 81 */ 82 Map<String, Header> getChallenges( 83 HttpHost authhost, 84 HttpResponse response, 85 HttpContext context) throws MalformedChallengeException; 86 87 /** 88 * Selects one authentication challenge out of all available and 89 * creates and generates {@link AuthOption} instance capable of 90 * processing that challenge. 91 * 92 * @param challenges collection of challenges. 93 * @param authhost authentication host. 94 * @param response HTTP response. 95 * @param context HTTP context. 96 * @return authentication auth schemes that can be used for authentication. Can be empty. 97 * @throws MalformedChallengeException if one of the authentication 98 * challenges is not valid or malformed. 99 */ 100 Queue<AuthOption> select( 101 Map<String, Header> challenges, 102 HttpHost authhost, 103 HttpResponse response, 104 HttpContext context) throws MalformedChallengeException; 105 106 /** 107 * Callback invoked in case of successful authentication. 108 * 109 * @param authhost authentication host. 110 * @param authScheme authentication scheme used. 111 * @param context HTTP context. 112 */ 113 void authSucceeded( 114 HttpHost authhost, 115 AuthScheme authScheme, 116 HttpContext context); 117 118 /** 119 * Callback invoked in case of unsuccessful authentication. 120 * 121 * @param authhost authentication host. 122 * @param authScheme authentication scheme used. 123 * @param context HTTP context. 124 */ 125 void authFailed( 126 HttpHost authhost, 127 AuthScheme authScheme, 128 HttpContext context); 129 130 }