mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/auth/NTLMScheme.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * ====================================================================
michael@0 3 *
michael@0 4 * Licensed to the Apache Software Foundation (ASF) under one or more
michael@0 5 * contributor license agreements. See the NOTICE file distributed with
michael@0 6 * this work for additional information regarding copyright ownership.
michael@0 7 * The ASF licenses this file to You under the Apache License, Version 2.0
michael@0 8 * (the "License"); you may not use this file except in compliance with
michael@0 9 * 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, software
michael@0 14 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 16 * See the License for the specific language governing permissions and
michael@0 17 * limitations under the License.
michael@0 18 * ====================================================================
michael@0 19 *
michael@0 20 * This software consists of voluntary contributions made by many
michael@0 21 * individuals on behalf of the Apache Software Foundation. For more
michael@0 22 * information on the Apache Software Foundation, please see
michael@0 23 * <http://www.apache.org/>.
michael@0 24 *
michael@0 25 */
michael@0 26
michael@0 27 package ch.boye.httpclientandroidlib.impl.auth;
michael@0 28
michael@0 29 import ch.boye.httpclientandroidlib.annotation.NotThreadSafe;
michael@0 30
michael@0 31 import ch.boye.httpclientandroidlib.Header;
michael@0 32 import ch.boye.httpclientandroidlib.HttpRequest;
michael@0 33 import ch.boye.httpclientandroidlib.auth.AUTH;
michael@0 34 import ch.boye.httpclientandroidlib.auth.AuthenticationException;
michael@0 35 import ch.boye.httpclientandroidlib.auth.Credentials;
michael@0 36 import ch.boye.httpclientandroidlib.auth.InvalidCredentialsException;
michael@0 37 import ch.boye.httpclientandroidlib.auth.MalformedChallengeException;
michael@0 38 import ch.boye.httpclientandroidlib.auth.NTCredentials;
michael@0 39 import ch.boye.httpclientandroidlib.impl.auth.AuthSchemeBase;
michael@0 40 import ch.boye.httpclientandroidlib.message.BufferedHeader;
michael@0 41 import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
michael@0 42
michael@0 43 /**
michael@0 44 * NTLM is a proprietary authentication scheme developed by Microsoft
michael@0 45 * and optimized for Windows platforms.
michael@0 46 *
michael@0 47 * @since 4.0
michael@0 48 */
michael@0 49 @NotThreadSafe
michael@0 50 public class NTLMScheme extends AuthSchemeBase {
michael@0 51
michael@0 52 enum State {
michael@0 53 UNINITIATED,
michael@0 54 CHALLENGE_RECEIVED,
michael@0 55 MSG_TYPE1_GENERATED,
michael@0 56 MSG_TYPE2_RECEVIED,
michael@0 57 MSG_TYPE3_GENERATED,
michael@0 58 FAILED,
michael@0 59 }
michael@0 60
michael@0 61 private final NTLMEngine engine;
michael@0 62
michael@0 63 private State state;
michael@0 64 private String challenge;
michael@0 65
michael@0 66 public NTLMScheme(final NTLMEngine engine) {
michael@0 67 super();
michael@0 68 if (engine == null) {
michael@0 69 throw new IllegalArgumentException("NTLM engine may not be null");
michael@0 70 }
michael@0 71 this.engine = engine;
michael@0 72 this.state = State.UNINITIATED;
michael@0 73 this.challenge = null;
michael@0 74 }
michael@0 75
michael@0 76 public String getSchemeName() {
michael@0 77 return "ntlm";
michael@0 78 }
michael@0 79
michael@0 80 public String getParameter(String name) {
michael@0 81 // String parameters not supported
michael@0 82 return null;
michael@0 83 }
michael@0 84
michael@0 85 public String getRealm() {
michael@0 86 // NTLM does not support the concept of an authentication realm
michael@0 87 return null;
michael@0 88 }
michael@0 89
michael@0 90 public boolean isConnectionBased() {
michael@0 91 return true;
michael@0 92 }
michael@0 93
michael@0 94 @Override
michael@0 95 protected void parseChallenge(
michael@0 96 final CharArrayBuffer buffer,
michael@0 97 int beginIndex, int endIndex) throws MalformedChallengeException {
michael@0 98 String challenge = buffer.substringTrimmed(beginIndex, endIndex);
michael@0 99 if (challenge.length() == 0) {
michael@0 100 if (this.state == State.UNINITIATED) {
michael@0 101 this.state = State.CHALLENGE_RECEIVED;
michael@0 102 } else {
michael@0 103 this.state = State.FAILED;
michael@0 104 }
michael@0 105 this.challenge = null;
michael@0 106 } else {
michael@0 107 this.state = State.MSG_TYPE2_RECEVIED;
michael@0 108 this.challenge = challenge;
michael@0 109 }
michael@0 110 }
michael@0 111
michael@0 112 public Header authenticate(
michael@0 113 final Credentials credentials,
michael@0 114 final HttpRequest request) throws AuthenticationException {
michael@0 115 NTCredentials ntcredentials = null;
michael@0 116 try {
michael@0 117 ntcredentials = (NTCredentials) credentials;
michael@0 118 } catch (ClassCastException e) {
michael@0 119 throw new InvalidCredentialsException(
michael@0 120 "Credentials cannot be used for NTLM authentication: "
michael@0 121 + credentials.getClass().getName());
michael@0 122 }
michael@0 123 String response = null;
michael@0 124 if (this.state == State.CHALLENGE_RECEIVED || this.state == State.FAILED) {
michael@0 125 response = this.engine.generateType1Msg(
michael@0 126 ntcredentials.getDomain(),
michael@0 127 ntcredentials.getWorkstation());
michael@0 128 this.state = State.MSG_TYPE1_GENERATED;
michael@0 129 } else if (this.state == State.MSG_TYPE2_RECEVIED) {
michael@0 130 response = this.engine.generateType3Msg(
michael@0 131 ntcredentials.getUserName(),
michael@0 132 ntcredentials.getPassword(),
michael@0 133 ntcredentials.getDomain(),
michael@0 134 ntcredentials.getWorkstation(),
michael@0 135 this.challenge);
michael@0 136 this.state = State.MSG_TYPE3_GENERATED;
michael@0 137 } else {
michael@0 138 throw new AuthenticationException("Unexpected state: " + this.state);
michael@0 139 }
michael@0 140 CharArrayBuffer buffer = new CharArrayBuffer(32);
michael@0 141 if (isProxy()) {
michael@0 142 buffer.append(AUTH.PROXY_AUTH_RESP);
michael@0 143 } else {
michael@0 144 buffer.append(AUTH.WWW_AUTH_RESP);
michael@0 145 }
michael@0 146 buffer.append(": NTLM ");
michael@0 147 buffer.append(response);
michael@0 148 return new BufferedHeader(buffer);
michael@0 149 }
michael@0 150
michael@0 151 public boolean isComplete() {
michael@0 152 return this.state == State.MSG_TYPE3_GENERATED || this.state == State.FAILED;
michael@0 153 }
michael@0 154
michael@0 155 }

mercurial