Skip to content

Commit e09dee2

Browse files
committed
expose the authenticator independent of the proxy
1 parent 4ab136c commit e09dee2

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* The MIT License
3+
*
4+
* Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining
7+
* a copy of this software and associated documentation files (the
8+
* "Software"), to deal in the Software without restriction, including
9+
* without limitation the rights to use, copy, modify, merge, publish,
10+
* distribute, sublicense, and/or sell copies of the Software, and to
11+
* permit persons to whom the Software is furnished to do so, subject to
12+
* the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be
15+
* included in all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
package BehaviorTests;
27+
28+
import kong.unirest.core.HttpResponse;
29+
import kong.unirest.core.Unirest;
30+
import org.junit.jupiter.api.Test;
31+
32+
import java.net.Authenticator;
33+
import java.net.PasswordAuthentication;
34+
35+
import static org.assertj.core.api.Assertions.assertThat;
36+
37+
public class AuthenticatorTest extends BddTest {
38+
@Test
39+
void getBlockedWithoutAuth(){
40+
assertThat(Unirest.get(MockServer.BASICAUTH).asEmpty())
41+
.returns(401, HttpResponse::getStatus)
42+
.returns("Basic realm=\"User Visible Realm\", charset=\"UTF-8\"", r -> r.getHeaders().getFirst("WWW-Authenticate"));
43+
}
44+
45+
@Test
46+
void setAuthenticator(){
47+
Unirest.config().authenticator(new Authenticator() {
48+
@Override
49+
protected PasswordAuthentication getPasswordAuthentication() {
50+
return new PasswordAuthentication("Fred", "Flintstone".toCharArray());
51+
}
52+
});
53+
54+
Unirest.get(MockServer.BASICAUTH)
55+
.asObject(RequestCapture.class)
56+
.getBody()
57+
.assertBasicAuth("Fred", "Flintstone");
58+
59+
}
60+
}

unirest-bdd-tests/src/test/java/BehaviorTests/MockServer.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import io.javalin.websocket.WsConfig;
3535
import jakarta.servlet.ServletOutputStream;
3636
import org.eclipse.jetty.util.UrlEncoded;
37+
import org.jetbrains.annotations.NotNull;
3738

3839
import java.io.ByteArrayOutputStream;
3940
import java.io.File;
@@ -86,6 +87,7 @@ public class MockServer {
8687
public static final String ALTGET = "http://127.0.0.1:" + PORT + "/get";
8788
public static final String ECHO_RAW = HOST + "/raw";
8889
public static final String TIMEOUT = HOST + "/timeout";
90+
public static final String BASICAUTH = HOST + "/basic";
8991
private static Javalin app;
9092
private static int errorCode = 400;
9193
private static RequestCapture lastRequest;
@@ -118,6 +120,12 @@ public static void reset() {
118120
timesCalled++;
119121
lastRequest = new RequestCapture(c);
120122
});
123+
app.before("/basic", ctx -> {
124+
if (ctx.basicAuthCredentials() == null) {
125+
ctx.header("WWW-Authenticate", "Basic realm=\"User Visible Realm\", charset=\"UTF-8\"");
126+
ctx.status(401);
127+
}
128+
});
121129
app.ws("/websocket", ws);
122130
app.sse("/sse", new TestSSEConsumer());
123131
app.delete("/delete", MockServer::jsonResponse);
@@ -144,6 +152,7 @@ public static void reset() {
144152
app.get("/error", MockServer::error);
145153
app.get("/hello", MockServer::helloWOrld);
146154
app.get("/timeout", MockServer::timeout);
155+
app.get("/basic", MockServer::basic);
147156
Runtime.getRuntime().addShutdownHook(new Thread(app::stop));
148157
try {
149158
new CountDownLatch(1).await(2, TimeUnit.SECONDS);
@@ -152,6 +161,12 @@ public static void reset() {
152161
}
153162
}
154163

164+
private static void basic(@NotNull Context context) {
165+
if(!(context.status().getCode() == 401)){
166+
jsonResponse(context);
167+
}
168+
}
169+
155170
private static void timeout(Context context) {
156171
try {
157172
Thread.sleep(1000);

unirest/src/main/java/kong/unirest/core/Config.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import javax.net.ssl.SSLContext;
3232
import java.io.InputStream;
33+
import java.net.Authenticator;
3334
import java.net.http.*;
3435
import java.net.http.HttpRequest;
3536
import java.net.http.HttpResponse;
@@ -74,6 +75,7 @@ public class Config {
7475
private CacheManager cache;
7576
private HttpClient.Version version = HttpClient.Version.HTTP_2;
7677
private RetryStrategy retry;
78+
private Authenticator authenticator;
7779

7880
public Config() {
7981
setDefaults();
@@ -101,6 +103,7 @@ private void setDefaults() {
101103
retry = null;
102104
objectMapper = () -> CoreFactory.getCore().getObjectMapper();
103105
version = HttpClient.Version.HTTP_2;
106+
authenticator = null;
104107

105108
try {
106109
clientBuilder = JavaClient::new;
@@ -913,4 +916,21 @@ public Config disableHostNameVerification(boolean enabled) {
913916
System.setProperty(JDK_HTTPCLIENT_DISABLE_HOST_NAME_VERIFICATION, String.valueOf(enabled));
914917
return this;
915918
}
919+
920+
/**
921+
* Sets a authenticator object for the client
922+
* @param auth
923+
* @return this config
924+
*/
925+
public Config authenticator(Authenticator auth) {
926+
this.authenticator = auth;
927+
return this;
928+
}
929+
930+
/**
931+
* @return the authenticator registered with the config
932+
*/
933+
public Authenticator getAuthenticator(){
934+
return authenticator;
935+
}
916936
}

unirest/src/main/java/kong/unirest/core/java/JavaClientBuilder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ public HttpClient apply(Config config) {
6262
if(config.useSystemProperties()){
6363
builder.proxy(ProxySelector.getDefault());
6464
}
65+
if(config.getAuthenticator() != null){
66+
builder.authenticator(config.getAuthenticator());
67+
}
68+
6569
return builder.build();
6670
}
6771

0 commit comments

Comments
 (0)