jdk/src/windows/classes/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.java
changeset 4157 558590fb3b49
parent 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
4061:35a627ad2443 4157:558590fb3b49
       
     1 /*
       
     2  * Copyright 2002 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package sun.net.www.protocol.http.ntlm;
       
    27 
       
    28 import java.io.IOException;
       
    29 import sun.misc.BASE64Encoder;
       
    30 import sun.misc.BASE64Decoder;
       
    31 
       
    32 /*
       
    33  * Hooks into Windows implementation of NTLM.
       
    34  * This class will be replaced if a cross-platform version of NTLM
       
    35  * is implemented in the future.
       
    36  */
       
    37 
       
    38 public class NTLMAuthSequence {
       
    39 
       
    40     private String username;
       
    41     private String password;
       
    42     private String ntdomain;
       
    43     private int state;
       
    44     private long crdHandle;
       
    45     private long ctxHandle;
       
    46 
       
    47     static {
       
    48         initFirst();
       
    49     }
       
    50 
       
    51     NTLMAuthSequence (String username, String password, String ntdomain)
       
    52     throws IOException
       
    53     {
       
    54         this.username = username;
       
    55         this.password = password;
       
    56         this.ntdomain = ntdomain;
       
    57         state = 0;
       
    58         crdHandle = getCredentialsHandle (username, ntdomain, password);
       
    59         if (crdHandle == 0) {
       
    60             throw new IOException ("could not get credentials handle");
       
    61         }
       
    62     }
       
    63 
       
    64     public String getAuthHeader (String token) throws IOException {
       
    65         byte[] input = null;
       
    66         if (token != null)
       
    67             input = (new BASE64Decoder()).decodeBuffer(token);
       
    68         byte[] b = getNextToken (crdHandle, input);
       
    69         if (b == null)
       
    70             throw new IOException ("Internal authentication error");
       
    71         return (new B64Encoder()).encode (b);
       
    72     }
       
    73 
       
    74     private native static void initFirst ();
       
    75 
       
    76     private native long getCredentialsHandle (String user, String domain, String password);
       
    77 
       
    78     private native byte[] getNextToken (long crdHandle, byte[] lastToken);
       
    79 }
       
    80 
       
    81 class B64Encoder extends BASE64Encoder {
       
    82     protected int bytesPerLine () {
       
    83         return 1024;
       
    84     }
       
    85 }