Skip to content

Commit 9a4441a

Browse files
committed
#8: Default Digest Algorithm
1 parent cca8288 commit 9a4441a

File tree

6 files changed

+35
-19
lines changed

6 files changed

+35
-19
lines changed

src/main/java/me/vzhilin/auth/DigestAuthenticator.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package me.vzhilin.auth;
22

3-
import me.vzhilin.auth.digester.Digester;
4-
import me.vzhilin.auth.digester.Ha1;
5-
import me.vzhilin.auth.digester.Ha1Supplier;
6-
import me.vzhilin.auth.parser.ChallengeResponse;
7-
import me.vzhilin.auth.parser.QopOptions;
3+
import me.vzhilin.auth.digester.*;
4+
import me.vzhilin.auth.parser.*;
85

96
import java.util.Set;
107

@@ -25,7 +22,11 @@ public DigestAuthenticator(Ha1Supplier ha1Supplier, Digester digester) {
2522
}
2623

2724
public DigestAuthenticator(String user, String pass) {
28-
this((algorithm, realm) -> Ha1.hash(algorithm, user, realm, pass));
25+
this(user, pass, DigestAlgorithm.MD5);
26+
}
27+
28+
public DigestAuthenticator(String user, String pass, DigestAlgorithm defaultAlgorithm) {
29+
this(new FallbackHa1Supplier((algorithm, realm) -> Ha1.hash(algorithm, user, realm, pass), defaultAlgorithm));
2930
}
3031

3132
public synchronized void onResponseReceived(ChallengeResponse response, int httpStatus) {
@@ -55,11 +56,11 @@ private QopOptions chooseQop(ChallengeResponse challenge) {
5556
return null;
5657
}
5758

58-
public String autorizationHeader(String method, String uri) {
59-
return autorizationHeader(method, uri, "");
59+
public String authorizationHeader(String method, String uri) {
60+
return authorizationHeader(method, uri, "");
6061
}
6162

62-
public synchronized String autorizationHeader(String method, String uri, String entityBody) {
63+
public synchronized String authorizationHeader(String method, String uri, String entityBody) {
6364
if (digester.getNonce() == null) {
6465
return null;
6566
}
@@ -72,4 +73,23 @@ public synchronized String autorizationHeader(String method, String uri, String
7273
digester.incNonceCount();
7374
return headerValue;
7475
}
76+
77+
private static final class FallbackHa1Supplier implements Ha1Supplier {
78+
private final Ha1Supplier delegate;
79+
private final DigestAlgorithm defaultAlgorithm;
80+
81+
public FallbackHa1Supplier(Ha1Supplier delegate, DigestAlgorithm defaultAlgorithm) {
82+
this.delegate = delegate;
83+
this.defaultAlgorithm = defaultAlgorithm;
84+
}
85+
86+
@Override
87+
public Ha1 hash(DigestAlgorithm algorithm, String realm) {
88+
if (algorithm == null) {
89+
return delegate.hash(defaultAlgorithm, realm);
90+
} else {
91+
return delegate.hash(algorithm, realm);
92+
}
93+
}
94+
}
7595
}

src/main/java/me/vzhilin/auth/netty/DigestNettyHttpAuthenticator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
6060

6161
String method = req.method().name();
6262
String uri = req.uri();
63-
final String header = authenticator.autorizationHeader(method, uri);
63+
final String header = authenticator.authorizationHeader(method, uri);
6464
if (header != null) {
6565
req.headers().set(HttpHeaderNames.AUTHORIZATION, header);
6666
}

src/main/java/me/vzhilin/auth/netty/TransparentDigestNettyHttpAuthenticator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
7979
if (authenticateHeader != null) {
8080
authenticator.onResponseReceived(ChallengeResponse.of(authenticateHeader), status.code());
8181
}
82-
final String auth = authenticator.autorizationHeader(request.method().name(), request.uri());
82+
final String auth = authenticator.authorizationHeader(request.method().name(), request.uri());
8383
if (auth != null) {
8484
request.headers().set(HttpHeaderNames.AUTHORIZATION, auth);
8585
}
@@ -115,7 +115,7 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
115115
String method = req.method().name();
116116
String uri = req.uri();
117117

118-
req.headers().set(HttpHeaderNames.AUTHORIZATION, authenticator.autorizationHeader(method, uri));
118+
req.headers().set(HttpHeaderNames.AUTHORIZATION, authenticator.authorizationHeader(method, uri));
119119
}
120120
// keep the client request
121121
// When server responds 401 Unauthorized, resend the request with authentication header

src/main/java/me/vzhilin/auth/parser/ChallengeResponseParser.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,6 @@ private ChallengeResponse readDigestChallenge(ChallengeResponse challenge) throw
138138
}
139139
} while (hasNext());
140140

141-
if (challenge.getAlgorithm() == null) {
142-
challenge.addAlgorithm(DigestAlgorithm.MD5);
143-
}
144-
145141
return challenge;
146142
}
147143
}

src/test/java/me/vzhilin/demo/webflux/WebFluxDemo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
4444
HttpRequest request = (HttpRequest) msg;
4545
String authorization = request.headers().get(HttpHeaderNames.AUTHORIZATION);
4646
if (authorization == null) {
47-
final String authorizationHeader = auth.autorizationHeader(request.method().name(), request.uri());
47+
final String authorizationHeader = auth.authorizationHeader(request.method().name(), request.uri());
4848
if (authorizationHeader != null) {
4949
request.headers().set(HttpHeaderNames.AUTHORIZATION, authorizationHeader);
5050
}

src/test/java/me/vzhilin/test/DigestAuthenticatorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ public void digestAuth() throws Exception {
3636
authenticator.onResponseReceived(ChallengeResponse.of(firstResponse.getFirstHeader("WWW-Authenticate").getValue()),
3737
firstResponse.getStatusLine().getStatusCode());
3838

39-
request.setHeader("Authorization", authenticator.autorizationHeader("GET", uri.getPath()));
39+
request.setHeader("Authorization", authenticator.authorizationHeader("GET", uri.getPath()));
4040
CloseableHttpResponse secondResponse = httpClient.execute(request);
4141
EntityUtils.consume(secondResponse.getEntity());
4242
assertEquals("expected authorized", 200, secondResponse.getStatusLine().getStatusCode());
4343

44-
request.setHeader("Authorization", authenticator.autorizationHeader("GET", uri.getPath()));
44+
request.setHeader("Authorization", authenticator.authorizationHeader("GET", uri.getPath()));
4545
CloseableHttpResponse thirdResponse = httpClient.execute(request);
4646
EntityUtils.consume(thirdResponse.getEntity());
4747
assertEquals("ensure that digester is working", 200, thirdResponse.getStatusLine().getStatusCode());

0 commit comments

Comments
 (0)