Skip to content

Commit 03a268e

Browse files
committed
add basic and digest auth... test first
1 parent 38f6a15 commit 03a268e

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

pom.xml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
24

35
<parent>
46
<groupId>info.unterrainer.commons</groupId>
@@ -8,7 +10,7 @@
810

911
<modelVersion>4.0.0</modelVersion>
1012
<artifactId>rest-client</artifactId>
11-
<version>0.1.2</version>
13+
<version>0.1.3</version>
1214
<name>RestClient</name>
1315
<packaging>jar</packaging>
1416

@@ -36,5 +38,10 @@
3638
<artifactId>okio</artifactId>
3739
<version>2.10.0</version>
3840
</dependency>
41+
<dependency>
42+
<groupId>io.github.rburgst</groupId>
43+
<artifactId>okhttp-digest</artifactId>
44+
<version>3.0</version>
45+
</dependency>
3946
</dependencies>
4047
</project>

src/main/java/info/unterrainer/commons/restclient/RestClient.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
package info.unterrainer.commons.restclient;
22

33
import java.io.IOException;
4+
import java.util.Map;
45
import java.util.Random;
6+
import java.util.concurrent.ConcurrentHashMap;
57
import java.util.concurrent.TimeUnit;
68

9+
import com.burgstaller.okhttp.AuthenticationCacheInterceptor;
10+
import com.burgstaller.okhttp.CachingAuthenticatorDecorator;
11+
import com.burgstaller.okhttp.DispatchingAuthenticator;
12+
import com.burgstaller.okhttp.basic.BasicAuthenticator;
13+
import com.burgstaller.okhttp.digest.CachingAuthenticator;
14+
import com.burgstaller.okhttp.digest.Credentials;
15+
import com.burgstaller.okhttp.digest.DigestAuthenticator;
16+
717
import info.unterrainer.commons.restclient.exceptions.RestClientException;
818
import info.unterrainer.commons.serialization.jsonmapper.JsonMapper;
919
import lombok.experimental.Accessors;
1020
import lombok.extern.slf4j.Slf4j;
1121
import okhttp3.Call;
12-
import okhttp3.Credentials;
1322
import okhttp3.Headers;
1423
import okhttp3.MediaType;
1524
import okhttp3.OkHttpClient;
@@ -50,11 +59,22 @@ public RestClient(final JsonMapper jsonMapper, final String userName, final Stri
5059
.writeTimeout(writeTimeoutInMillis, TimeUnit.MILLISECONDS)
5160
.addInterceptor(new GzipInterceptor())
5261
.followRedirects(true);
53-
if (userName != null || password != null)
54-
c.authenticator((route, response) -> {
55-
String credential = Credentials.basic(userName, password);
56-
return response.request().newBuilder().header("Authorization", credential).build();
57-
});
62+
if (userName != null || password != null) {
63+
final Map<String, CachingAuthenticator> authCache = new ConcurrentHashMap<>();
64+
65+
Credentials credentials = new Credentials(userName, password);
66+
final BasicAuthenticator basicAuthenticator = new BasicAuthenticator(credentials);
67+
final DigestAuthenticator digestAuthenticator = new DigestAuthenticator(credentials);
68+
69+
// note that all auth schemes should be registered as lowercase!
70+
DispatchingAuthenticator authenticator = new DispatchingAuthenticator.Builder()
71+
.with("digest", digestAuthenticator)
72+
.with("basic", basicAuthenticator)
73+
.build();
74+
75+
c.authenticator(new CachingAuthenticatorDecorator(authenticator, authCache))
76+
.addInterceptor(new AuthenticationCacheInterceptor(authCache));
77+
}
5878
client = c.build();
5979
}
6080

0 commit comments

Comments
 (0)