1 30 31 package org.apache.commons.httpclient.auth; 32 33 import java.util.Collection ; 34 import java.util.Iterator ; 35 import java.util.Map ; 36 37 import org.apache.commons.httpclient.params.HttpParams; 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 41 49 public final class AuthChallengeProcessor { 50 51 private static final Log LOG = LogFactory.getLog(AuthChallengeProcessor.class); 52 53 private HttpParams params = null; 54 55 61 public AuthChallengeProcessor(final HttpParams params) { 62 super(); 63 if (params == null) { 64 throw new IllegalArgumentException ("Parameter collection may not be null"); 65 } 66 this.params = params; 67 } 68 69 80 public AuthScheme selectAuthScheme(final Map challenges) throws AuthChallengeException { 81 if (challenges == null) { 82 throw new IllegalArgumentException ("Challenge map may not be null"); 83 } 84 Collection authPrefs = (Collection ) this.params.getParameter( 85 AuthPolicy.AUTH_SCHEME_PRIORITY); 86 if (authPrefs == null || authPrefs.isEmpty()) { 87 authPrefs = AuthPolicy.getDefaultAuthPrefs(); 88 } 89 if (LOG.isDebugEnabled()) { 90 LOG.debug("Supported authentication schemes in the order of preference: " 91 + authPrefs); 92 } 93 AuthScheme authscheme = null; 94 String challenge = null; 95 Iterator item = authPrefs.iterator(); 96 while (item.hasNext()) { 97 String id = (String ) item.next(); 98 challenge = (String ) challenges.get(id.toLowerCase()); 99 if (challenge != null) { 100 if (LOG.isInfoEnabled()) { 101 LOG.info(id + " authentication scheme selected"); 102 } 103 try { 104 authscheme = AuthPolicy.getAuthScheme(id); 105 } catch (IllegalStateException e) { 106 throw new AuthChallengeException(e.getMessage()); 107 } 108 break; 109 } else { 110 if (LOG.isDebugEnabled()) { 111 LOG.debug("Challenge for " + id + " authentication scheme not available"); 112 } 114 } 115 } 116 if (authscheme == null) { 117 throw new AuthChallengeException( 119 "Unable to respond to any of these challenges: " 120 + challenges); 121 } 122 return authscheme; 123 } 124 125 138 public AuthScheme processChallenge(final AuthState state, final Map challenges) 139 throws MalformedChallengeException, AuthenticationException 140 { 141 if (state == null) { 142 throw new IllegalArgumentException ("Authentication state may not be null"); 143 } 144 if (challenges == null) { 145 throw new IllegalArgumentException ("Challenge map may not be null"); 146 } 147 148 if (state.isPreemptive() || state.getAuthScheme() == null) { 149 state.setAuthScheme(selectAuthScheme(challenges)); 151 } 152 AuthScheme authscheme = state.getAuthScheme(); 153 String id = authscheme.getSchemeName(); 154 if (LOG.isDebugEnabled()) { 155 LOG.debug("Using authentication scheme: " + id); 156 } 157 String challenge = (String ) challenges.get(id.toLowerCase()); 158 if (challenge == null) { 159 throw new AuthenticationException(id + 160 " authorization challenge expected, but not found"); 161 } 162 authscheme.processChallenge(challenge); 163 LOG.debug("Authorization challenge processed"); 164 return authscheme; 165 } 166 } 167 | Popular Tags |