Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/http/HttpURLConnection/NTLMAuthWithSM.java
41154 views
1
/*
2
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import com.sun.net.httpserver.HttpExchange;
25
import com.sun.net.httpserver.HttpHandler;
26
import com.sun.net.httpserver.HttpServer;
27
import java.io.BufferedReader;
28
import java.io.InputStreamReader;
29
import java.io.IOException;
30
import java.io.InputStream;
31
import java.net.Authenticator;
32
import java.net.InetAddress;
33
import java.net.InetSocketAddress;
34
import java.net.PasswordAuthentication;
35
import java.net.URL;
36
import java.net.URLConnection;
37
import java.util.List;
38
import sun.net.www.protocol.http.ntlm.NTLMAuthenticationCallback;
39
40
/*
41
* @test
42
* @bug 8137174
43
* @modules java.base/sun.net.www.protocol.http.ntlm
44
* jdk.httpserver
45
* @summary Checks if NTLM auth works fine if security manager set
46
* @run main/othervm/java.security.policy=NTLMAuthWithSM.policy NTLMAuthWithSM
47
*/
48
public class NTLMAuthWithSM {
49
50
public static void main(String[] args) throws Exception {
51
// security manager is required
52
if (System.getSecurityManager() == null) {
53
throw new RuntimeException("Security manager not specified");
54
}
55
56
if (System.getProperty("os.name").startsWith("Windows")) {
57
// disable transparent NTLM authentication on Windows
58
NTLMAuthenticationCallback.setNTLMAuthenticationCallback(
59
new NTLMAuthenticationCallbackImpl());
60
}
61
62
try (LocalHttpServer server = LocalHttpServer.startServer()) {
63
// set authenticator
64
Authenticator.setDefault(new AuthenticatorImpl());
65
66
String url = String.format("http://%s/test/",
67
server.getAuthority());
68
69
// load a document which is protected with NTML authentication
70
System.out.println("load() called: " + url);
71
URLConnection conn = new URL(url).openConnection();
72
try (BufferedReader reader = new BufferedReader(
73
new InputStreamReader(conn.getInputStream()))) {
74
75
String line = reader.readLine();
76
if (line == null) {
77
throw new IOException("Couldn't read a response");
78
}
79
do {
80
System.out.println(line);
81
} while ((line = reader.readLine()) != null);
82
}
83
}
84
85
System.out.println("Test passed");
86
}
87
88
private static class AuthenticatorImpl extends Authenticator {
89
90
@Override
91
public PasswordAuthentication getPasswordAuthentication() {
92
System.out.println("getPasswordAuthentication() called, scheme: "
93
+ getRequestingScheme());
94
if (getRequestingScheme().equalsIgnoreCase("ntlm")) {
95
return new PasswordAuthentication("test", "test".toCharArray());
96
}
97
return null;
98
}
99
}
100
101
// local http server which pretends to support NTLM auth
102
static class LocalHttpServer implements HttpHandler, AutoCloseable {
103
104
private final HttpServer server;
105
106
private LocalHttpServer(HttpServer server) {
107
this.server = server;
108
}
109
110
static LocalHttpServer startServer() throws IOException {
111
InetAddress loopback = InetAddress.getLoopbackAddress();
112
HttpServer httpServer = HttpServer.create(
113
new InetSocketAddress(loopback, 0), 0);
114
LocalHttpServer localHttpServer = new LocalHttpServer(httpServer);
115
localHttpServer.start();
116
117
return localHttpServer;
118
}
119
120
void start() {
121
server.createContext("/test", this);
122
server.start();
123
System.out.println("HttpServer: started on port " + getPort());
124
}
125
126
void stop() {
127
server.stop(0);
128
System.out.println("HttpServer: stopped");
129
}
130
131
String getAuthority() {
132
InetAddress address = server.getAddress().getAddress();
133
String hostaddr = address.isAnyLocalAddress()
134
? "localhost" : address.getHostAddress();
135
if (hostaddr.indexOf(':') > -1) hostaddr = "[" + hostaddr + "]";
136
return hostaddr + ":" + getPort();
137
}
138
139
int getPort() {
140
return server.getAddress().getPort();
141
}
142
143
@Override
144
public void handle(HttpExchange t) throws IOException {
145
System.out.println("HttpServer: handle connection");
146
147
// read a request
148
try (InputStream is = t.getRequestBody()) {
149
while (is.read() > 0);
150
}
151
152
try {
153
List<String> headers = t.getRequestHeaders()
154
.get("Authorization");
155
if (headers != null && !headers.isEmpty()
156
&& headers.get(0).trim().contains("NTLM")) {
157
byte[] output = "hello".getBytes();
158
t.sendResponseHeaders(200, output.length);
159
t.getResponseBody().write(output);
160
System.out.println("HttpServer: return 200");
161
} else {
162
t.getResponseHeaders().set("WWW-Authenticate", "NTLM");
163
byte[] output = "forbidden".getBytes();
164
t.sendResponseHeaders(401, output.length);
165
t.getResponseBody().write(output);
166
System.out.println("HttpServer: return 401");
167
}
168
} catch (IOException e) {
169
System.out.println("HttpServer: exception: " + e);
170
System.out.println("HttpServer: return 500");
171
t.sendResponseHeaders(500, 0);
172
} finally {
173
t.close();
174
}
175
}
176
177
@Override
178
public void close() {
179
stop();
180
}
181
}
182
183
private static class NTLMAuthenticationCallbackImpl
184
extends NTLMAuthenticationCallback {
185
186
// don't trust any site, so that no transparent NTLM auth happens
187
@Override
188
public boolean isTrustedSite(URL url) {
189
System.out.println(
190
"NTLMAuthenticationCallbackImpl.isTrustedSite() called: "
191
+ "return false");
192
return false;
193
}
194
}
195
}
196
197