mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/client/AbstractAuthenticationHandler.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 * ====================================================================
michael@0 3 * Licensed to the Apache Software Foundation (ASF) under one
michael@0 4 * or more contributor license agreements. See the NOTICE file
michael@0 5 * distributed with this work for additional information
michael@0 6 * regarding copyright ownership. The ASF licenses this file
michael@0 7 * to you under the Apache License, Version 2.0 (the
michael@0 8 * "License"); you may not use this file except in compliance
michael@0 9 * with the License. You may obtain a copy of the License at
michael@0 10 *
michael@0 11 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 12 *
michael@0 13 * Unless required by applicable law or agreed to in writing,
michael@0 14 * software distributed under the License is distributed on an
michael@0 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0 16 * KIND, either express or implied. See the License for the
michael@0 17 * specific language governing permissions and limitations
michael@0 18 * under the License.
michael@0 19 * ====================================================================
michael@0 20 *
michael@0 21 * This software consists of voluntary contributions made by many
michael@0 22 * individuals on behalf of the Apache Software Foundation. For more
michael@0 23 * information on the Apache Software Foundation, please see
michael@0 24 * <http://www.apache.org/>.
michael@0 25 *
michael@0 26 */
michael@0 27
michael@0 28 package ch.boye.httpclientandroidlib.impl.client;
michael@0 29
michael@0 30 import java.util.Arrays;
michael@0 31 import java.util.Collection;
michael@0 32 import java.util.Collections;
michael@0 33 import java.util.HashMap;
michael@0 34 import java.util.List;
michael@0 35 import java.util.Locale;
michael@0 36 import java.util.Map;
michael@0 37
michael@0 38 import ch.boye.httpclientandroidlib.androidextra.HttpClientAndroidLog;
michael@0 39 /* LogFactory removed by HttpClient for Android script. */
michael@0 40 import ch.boye.httpclientandroidlib.FormattedHeader;
michael@0 41 import ch.boye.httpclientandroidlib.Header;
michael@0 42 import ch.boye.httpclientandroidlib.HttpResponse;
michael@0 43 import ch.boye.httpclientandroidlib.annotation.Immutable;
michael@0 44 import ch.boye.httpclientandroidlib.auth.AuthScheme;
michael@0 45 import ch.boye.httpclientandroidlib.auth.AuthSchemeRegistry;
michael@0 46 import ch.boye.httpclientandroidlib.auth.AuthenticationException;
michael@0 47 import ch.boye.httpclientandroidlib.auth.MalformedChallengeException;
michael@0 48 import ch.boye.httpclientandroidlib.client.AuthenticationHandler;
michael@0 49 import ch.boye.httpclientandroidlib.client.params.AuthPolicy;
michael@0 50 import ch.boye.httpclientandroidlib.client.protocol.ClientContext;
michael@0 51 import ch.boye.httpclientandroidlib.protocol.HTTP;
michael@0 52 import ch.boye.httpclientandroidlib.protocol.HttpContext;
michael@0 53 import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
michael@0 54
michael@0 55 /**
michael@0 56 * Base class for {@link AuthenticationHandler} implementations.
michael@0 57 *
michael@0 58 * @since 4.0
michael@0 59 */
michael@0 60 @Immutable
michael@0 61 public abstract class AbstractAuthenticationHandler implements AuthenticationHandler {
michael@0 62
michael@0 63 public HttpClientAndroidLog log = new HttpClientAndroidLog(getClass());
michael@0 64
michael@0 65 private static final List<String> DEFAULT_SCHEME_PRIORITY =
michael@0 66 Collections.unmodifiableList(Arrays.asList(new String[] {
michael@0 67 AuthPolicy.SPNEGO,
michael@0 68 AuthPolicy.NTLM,
michael@0 69 AuthPolicy.DIGEST,
michael@0 70 AuthPolicy.BASIC
michael@0 71 }));
michael@0 72
michael@0 73 public AbstractAuthenticationHandler() {
michael@0 74 super();
michael@0 75 }
michael@0 76
michael@0 77 protected Map<String, Header> parseChallenges(
michael@0 78 final Header[] headers) throws MalformedChallengeException {
michael@0 79
michael@0 80 Map<String, Header> map = new HashMap<String, Header>(headers.length);
michael@0 81 for (Header header : headers) {
michael@0 82 CharArrayBuffer buffer;
michael@0 83 int pos;
michael@0 84 if (header instanceof FormattedHeader) {
michael@0 85 buffer = ((FormattedHeader) header).getBuffer();
michael@0 86 pos = ((FormattedHeader) header).getValuePos();
michael@0 87 } else {
michael@0 88 String s = header.getValue();
michael@0 89 if (s == null) {
michael@0 90 throw new MalformedChallengeException("Header value is null");
michael@0 91 }
michael@0 92 buffer = new CharArrayBuffer(s.length());
michael@0 93 buffer.append(s);
michael@0 94 pos = 0;
michael@0 95 }
michael@0 96 while (pos < buffer.length() && HTTP.isWhitespace(buffer.charAt(pos))) {
michael@0 97 pos++;
michael@0 98 }
michael@0 99 int beginIndex = pos;
michael@0 100 while (pos < buffer.length() && !HTTP.isWhitespace(buffer.charAt(pos))) {
michael@0 101 pos++;
michael@0 102 }
michael@0 103 int endIndex = pos;
michael@0 104 String s = buffer.substring(beginIndex, endIndex);
michael@0 105 map.put(s.toLowerCase(Locale.ENGLISH), header);
michael@0 106 }
michael@0 107 return map;
michael@0 108 }
michael@0 109
michael@0 110 /**
michael@0 111 * Returns default list of auth scheme names in their order of preference.
michael@0 112 *
michael@0 113 * @return list of auth scheme names
michael@0 114 */
michael@0 115 protected List<String> getAuthPreferences() {
michael@0 116 return DEFAULT_SCHEME_PRIORITY;
michael@0 117 }
michael@0 118
michael@0 119 /**
michael@0 120 * Returns default list of auth scheme names in their order of preference
michael@0 121 * based on the HTTP response and the current execution context.
michael@0 122 *
michael@0 123 * @param response HTTP response.
michael@0 124 * @param context HTTP execution context.
michael@0 125 *
michael@0 126 * @since 4.1
michael@0 127 */
michael@0 128 protected List<String> getAuthPreferences(
michael@0 129 final HttpResponse response,
michael@0 130 final HttpContext context) {
michael@0 131 return getAuthPreferences();
michael@0 132 }
michael@0 133
michael@0 134 public AuthScheme selectScheme(
michael@0 135 final Map<String, Header> challenges,
michael@0 136 final HttpResponse response,
michael@0 137 final HttpContext context) throws AuthenticationException {
michael@0 138
michael@0 139 AuthSchemeRegistry registry = (AuthSchemeRegistry) context.getAttribute(
michael@0 140 ClientContext.AUTHSCHEME_REGISTRY);
michael@0 141 if (registry == null) {
michael@0 142 throw new IllegalStateException("AuthScheme registry not set in HTTP context");
michael@0 143 }
michael@0 144
michael@0 145 Collection<String> authPrefs = getAuthPreferences(response, context);
michael@0 146 if (authPrefs == null) {
michael@0 147 authPrefs = DEFAULT_SCHEME_PRIORITY;
michael@0 148 }
michael@0 149
michael@0 150 if (this.log.isDebugEnabled()) {
michael@0 151 this.log.debug("Authentication schemes in the order of preference: "
michael@0 152 + authPrefs);
michael@0 153 }
michael@0 154
michael@0 155 AuthScheme authScheme = null;
michael@0 156 for (String id: authPrefs) {
michael@0 157 Header challenge = challenges.get(id.toLowerCase(Locale.ENGLISH));
michael@0 158
michael@0 159 if (challenge != null) {
michael@0 160 if (this.log.isDebugEnabled()) {
michael@0 161 this.log.debug(id + " authentication scheme selected");
michael@0 162 }
michael@0 163 try {
michael@0 164 authScheme = registry.getAuthScheme(id, response.getParams());
michael@0 165 break;
michael@0 166 } catch (IllegalStateException e) {
michael@0 167 if (this.log.isWarnEnabled()) {
michael@0 168 this.log.warn("Authentication scheme " + id + " not supported");
michael@0 169 // Try again
michael@0 170 }
michael@0 171 }
michael@0 172 } else {
michael@0 173 if (this.log.isDebugEnabled()) {
michael@0 174 this.log.debug("Challenge for " + id + " authentication scheme not available");
michael@0 175 // Try again
michael@0 176 }
michael@0 177 }
michael@0 178 }
michael@0 179 if (authScheme == null) {
michael@0 180 // If none selected, something is wrong
michael@0 181 throw new AuthenticationException(
michael@0 182 "Unable to respond to any of these challenges: "
michael@0 183 + challenges);
michael@0 184 }
michael@0 185 return authScheme;
michael@0 186 }
michael@0 187
michael@0 188 }

mercurial