01: /*
02: * Copyright 2002 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * 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;
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: this .username = username;
54: this .password = password;
55: this .ntdomain = ntdomain;
56: state = 0;
57: crdHandle = getCredentialsHandle(username, ntdomain, password);
58: if (crdHandle == 0) {
59: throw new IOException("could not get credentials handle");
60: }
61: }
62:
63: public String getAuthHeader(String token) throws IOException {
64: byte[] input = null;
65: if (token != null)
66: input = (new BASE64Decoder()).decodeBuffer(token);
67: byte[] b = getNextToken(crdHandle, input);
68: if (b == null)
69: throw new IOException("Internal authentication error");
70: return (new B64Encoder()).encode(b);
71: }
72:
73: private native static void initFirst();
74:
75: private native long getCredentialsHandle(String user,
76: 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: }
|