Skip to content

Commit 776e777

Browse files
committed
This reverts commit 50dfd86
1 parent 50dfd86 commit 776e777

File tree

6 files changed

+21
-36
lines changed

6 files changed

+21
-36
lines changed
Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package me.vzhilin.auth;
22

3-
import me.vzhilin.auth.digester.*;
3+
import me.vzhilin.auth.digester.Digester;
4+
import me.vzhilin.auth.digester.Ha1;
5+
import me.vzhilin.auth.digester.Ha1Supplier;
46
import me.vzhilin.auth.parser.*;
57

68
import java.util.Set;
@@ -22,11 +24,7 @@ public DigestAuthenticator(Ha1Supplier ha1Supplier, Digester digester) {
2224
}
2325

2426
public DigestAuthenticator(String user, String 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));
27+
this((algorithm, realm) -> Ha1.hash(algorithm, user, realm, pass));
3028
}
3129

3230
public synchronized void onResponseReceived(ChallengeResponse response, int httpStatus) {
@@ -36,7 +34,9 @@ public synchronized void onResponseReceived(ChallengeResponse response, int http
3634
digester.resetNonceCount();
3735
}
3836

39-
digester.setAlgorithm(response.getAlgorithm());
37+
DigestAlgorithm algorithm = response.getAlgorithm();
38+
digester.setAlgorithm(algorithm == null ? DigestAlgorithm.MD5 : algorithm);
39+
4040
digester.setQop(chooseQop(response));
4141

4242
this.opaque = response.getOpaque();
@@ -53,14 +53,14 @@ private QopOptions chooseQop(ChallengeResponse challenge) {
5353
return QopOptions.AUTH_INT;
5454
}
5555

56-
return null;
56+
return QopOptions.AUTH;
5757
}
5858

59-
public String authorizationHeader(String method, String uri) {
60-
return authorizationHeader(method, uri, "");
59+
public String autorizationHeader(String method, String uri) {
60+
return autorizationHeader(method, uri, "");
6161
}
6262

63-
public synchronized String authorizationHeader(String method, String uri, String entityBody) {
63+
public synchronized String autorizationHeader(String method, String uri, String entityBody) {
6464
if (digester.getNonce() == null) {
6565
return null;
6666
}
@@ -73,23 +73,4 @@ public synchronized String authorizationHeader(String method, String uri, String
7373
digester.incNonceCount();
7474
return headerValue;
7575
}
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-
}
9576
}

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.authorizationHeader(method, uri);
63+
final String header = authenticator.autorizationHeader(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.authorizationHeader(request.method().name(), request.uri());
82+
final String auth = authenticator.autorizationHeader(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.authorizationHeader(method, uri));
118+
req.headers().set(HttpHeaderNames.AUTHORIZATION, authenticator.autorizationHeader(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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ private ChallengeResponse readDigestChallenge(ChallengeResponse challenge) throw
138138
}
139139
} while (hasNext());
140140

141+
if (challenge.getAlgorithm() == null) {
142+
challenge.addAlgorithm(DigestAlgorithm.MD5);
143+
}
144+
141145
return challenge;
142146
}
143147
}

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.authorizationHeader(request.method().name(), request.uri());
47+
final String authorizationHeader = auth.autorizationHeader(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.authorizationHeader("GET", uri.getPath()));
39+
request.setHeader("Authorization", authenticator.autorizationHeader("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.authorizationHeader("GET", uri.getPath()));
44+
request.setHeader("Authorization", authenticator.autorizationHeader("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)