Home | History | Annotate | Download | only in auth
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/auth/NTLMScheme.java $
      3  * $Revision: 655048 $
      4  * $Date: 2008-05-10 04:22:12 -0700 (Sat, 10 May 2008) $
      5  *
      6  * ====================================================================
      7  *
      8  *  Licensed to the Apache Software Foundation (ASF) under one or more
      9  *  contributor license agreements.  See the NOTICE file distributed with
     10  *  this work for additional information regarding copyright ownership.
     11  *  The ASF licenses this file to You under the Apache License, Version 2.0
     12  *  (the "License"); you may not use this file except in compliance with
     13  *  the License.  You may obtain a copy of the License at
     14  *
     15  *      http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  *  Unless required by applicable law or agreed to in writing, software
     18  *  distributed under the License is distributed on an "AS IS" BASIS,
     19  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     20  *  See the License for the specific language governing permissions and
     21  *  limitations under the License.
     22  * ====================================================================
     23  *
     24  * This software consists of voluntary contributions made by many
     25  * individuals on behalf of the Apache Software Foundation.  For more
     26  * information on the Apache Software Foundation, please see
     27  * <http://www.apache.org/>.
     28  *
     29  */
     30 
     31 package org.apache.http.impl.auth;
     32 
     33 import org.apache.http.Header;
     34 import org.apache.http.HttpRequest;
     35 import org.apache.http.auth.AUTH;
     36 import org.apache.http.auth.AuthenticationException;
     37 import org.apache.http.auth.Credentials;
     38 import org.apache.http.auth.InvalidCredentialsException;
     39 import org.apache.http.auth.MalformedChallengeException;
     40 import org.apache.http.auth.NTCredentials;
     41 import org.apache.http.impl.auth.AuthSchemeBase;
     42 import org.apache.http.message.BufferedHeader;
     43 import org.apache.http.util.CharArrayBuffer;
     44 /**
     45  * @deprecated Please use {@link java.net.URL#openConnection} instead.
     46  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
     47  *     for further details.
     48 */
     49 
     50 @Deprecated
     51 public class NTLMScheme extends AuthSchemeBase {
     52 
     53     enum State {
     54         UNINITIATED,
     55         CHALLENGE_RECEIVED,
     56         MSG_TYPE1_GENERATED,
     57         MSG_TYPE2_RECEVIED,
     58         MSG_TYPE3_GENERATED,
     59         FAILED,
     60     }
     61 
     62     private final NTLMEngine engine;
     63 
     64     private State state;
     65     private String challenge;
     66 
     67     public NTLMScheme(final NTLMEngine engine) {
     68         super();
     69         if (engine == null) {
     70             throw new IllegalArgumentException("NTLM engine may not be null");
     71         }
     72         this.engine = engine;
     73         this.state = State.UNINITIATED;
     74         this.challenge = null;
     75     }
     76 
     77     public String getSchemeName() {
     78         return "ntlm";
     79     }
     80 
     81     public String getParameter(String name) {
     82         // String parameters not supported
     83         return null;
     84     }
     85 
     86     public String getRealm() {
     87         // NTLM does not support the concept of an authentication realm
     88         return null;
     89     }
     90 
     91     public boolean isConnectionBased() {
     92         return true;
     93     }
     94 
     95     @Override
     96     protected void parseChallenge(
     97             final CharArrayBuffer buffer, int pos, int len) throws MalformedChallengeException {
     98         String challenge = buffer.substringTrimmed(pos, len);
     99         if (challenge.length() == 0) {
    100             if (this.state == State.UNINITIATED) {
    101                 this.state = State.CHALLENGE_RECEIVED;
    102             } else {
    103                 this.state = State.FAILED;
    104             }
    105             this.challenge = null;
    106         } else {
    107             this.state = State.MSG_TYPE2_RECEVIED;
    108             this.challenge = challenge;
    109         }
    110     }
    111 
    112     public Header authenticate(
    113             final Credentials credentials,
    114             final HttpRequest request) throws AuthenticationException {
    115         NTCredentials ntcredentials = null;
    116         try {
    117             ntcredentials = (NTCredentials) credentials;
    118         } catch (ClassCastException e) {
    119             throw new InvalidCredentialsException(
    120              "Credentials cannot be used for NTLM authentication: "
    121               + credentials.getClass().getName());
    122         }
    123         String response = null;
    124         if (this.state == State.CHALLENGE_RECEIVED || this.state == State.FAILED) {
    125             response = this.engine.generateType1Msg(
    126                     ntcredentials.getDomain(),
    127                     ntcredentials.getWorkstation());
    128             this.state = State.MSG_TYPE1_GENERATED;
    129         } else if (this.state == State.MSG_TYPE2_RECEVIED) {
    130             response = this.engine.generateType3Msg(
    131                     ntcredentials.getUserName(),
    132                     ntcredentials.getPassword(),
    133                     ntcredentials.getDomain(),
    134                     ntcredentials.getWorkstation(),
    135                     this.challenge);
    136             this.state = State.MSG_TYPE3_GENERATED;
    137         } else {
    138             throw new AuthenticationException("Unexpected state: " + this.state);
    139         }
    140         CharArrayBuffer buffer = new CharArrayBuffer(32);
    141         if (isProxy()) {
    142             buffer.append(AUTH.PROXY_AUTH_RESP);
    143         } else {
    144             buffer.append(AUTH.WWW_AUTH_RESP);
    145         }
    146         buffer.append(": NTLM ");
    147         buffer.append(response);
    148         return new BufferedHeader(buffer);
    149     }
    150 
    151     public boolean isComplete() {
    152         return this.state == State.MSG_TYPE3_GENERATED || this.state == State.FAILED;
    153     }
    154 
    155 }
    156