Skip to content

Commit 3eda19f

Browse files
committed
Add separate flags for requesting and checking mutual auth status.
Update some debug/exception messages
1 parent 3b6359b commit 3eda19f

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
lines changed

httpclient5/src/main/java/org/apache/hc/client5/http/auth/gss/GssConfig.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,31 +49,34 @@ public class GssConfig implements Cloneable {
4949

5050
public static final GssConfig DEFAULT = new Builder().build();
5151
public static final GssConfig LEGACY =
52-
new Builder().setIgnoreMissingToken(true).setRequestMutualAuth(false).build();
52+
new Builder().setIgnoreMissingToken(true).setRequireMutualAuth(false).build();
5353

5454
private final boolean addPort;
5555
private final boolean useCanonicalHostname;
5656
private final boolean requestMutualAuth;
57+
private final boolean requireMutualAuth;
5758
private final boolean requestDelegCreds;
5859
private final boolean ignoreMissingToken;
5960

6061
/**
6162
* Intended for CDI compatibility
6263
*/
6364
protected GssConfig() {
64-
this(false, false, true, false, false);
65+
this(false, false, true, true, false, false);
6566
}
6667

6768
GssConfig(
6869
final boolean addPort,
6970
final boolean useCanonicalHostname,
7071
final boolean requestMutualAuth,
72+
final boolean requireMutualAuth,
7173
final boolean requestDelegCreds,
7274
final boolean ignoreMissingToken) {
7375
super();
7476
this.addPort = addPort;
7577
this.useCanonicalHostname = useCanonicalHostname;
7678
this.requestMutualAuth = requestMutualAuth;
79+
this.requireMutualAuth = requireMutualAuth;
7780
this.requestDelegCreds = requestDelegCreds;
7881
this.ignoreMissingToken = ignoreMissingToken;
7982
}
@@ -94,6 +97,10 @@ public boolean isRequestMutualAuth() {
9497
return requestMutualAuth;
9598
}
9699

100+
public boolean isRequireMutualAuth() {
101+
return requireMutualAuth;
102+
}
103+
97104
public boolean isIgnoreMissingToken() {
98105
return ignoreMissingToken;
99106
}
@@ -111,6 +118,7 @@ public String toString() {
111118
builder.append(", useCanonicalHostname=").append(useCanonicalHostname);
112119
builder.append(", requestDelegCreds=").append(requestDelegCreds);
113120
builder.append(", requestMutualAuth=").append(requestMutualAuth);
121+
builder.append(", requireMutualAuth=").append(requireMutualAuth);
114122
builder.append(", ignoreMissingToken=").append(ignoreMissingToken);
115123
builder.append("]");
116124
return builder.toString();
@@ -125,6 +133,7 @@ public static GssConfig.Builder copy(final GssConfig config) {
125133
.setAddPort(config.isAddPort())
126134
.setUseCanonicalHostname(config.isUseCanonicalHostname())
127135
.setRequestDelegCreds(config.isRequestDelegCreds())
136+
.setRequireMutualAuth(config.isRequireMutualAuth())
128137
.setRequestMutualAuth(config.isRequestMutualAuth())
129138
.setIgnoreMissingToken(config.isIgnoreMissingToken());
130139
}
@@ -134,6 +143,7 @@ public static class Builder {
134143
private boolean addPort = false;
135144
private boolean useCanonicalHostname = false;
136145
private boolean requestMutualAuth = true;
146+
private boolean requireMutualAuth = true;
137147
private boolean requestDelegCreds = false;
138148
private boolean ignoreMissingToken = false;
139149

@@ -157,6 +167,11 @@ public Builder setRequestMutualAuth(final boolean requestMutualAuth) {
157167
return this;
158168
}
159169

170+
public Builder setRequireMutualAuth(final boolean requireMutualAuth) {
171+
this.requireMutualAuth = requireMutualAuth;
172+
return this;
173+
}
174+
160175
public Builder setRequestDelegCreds(final boolean requuestDelegCreds) {
161176
this.requestDelegCreds = requuestDelegCreds;
162177
return this;
@@ -168,13 +183,17 @@ public Builder setIgnoreMissingToken(final boolean ignoreMissingToken) {
168183
}
169184

170185
public GssConfig build() {
171-
if (requestMutualAuth && ignoreMissingToken) {
172-
throw new IllegalArgumentException("If requestMutualAuth is set then ignoreMissingToken must not be set");
186+
if (requireMutualAuth && ignoreMissingToken) {
187+
throw new IllegalArgumentException("If requireMutualAuth is set then ignoreMissingToken must not be set");
188+
}
189+
if (requireMutualAuth && !requestMutualAuth) {
190+
throw new IllegalArgumentException("If requireMutualAuth is set then requestMutualAuth must also be set");
173191
}
174192
return new GssConfig(
175193
addPort,
176194
useCanonicalHostname,
177195
requestMutualAuth,
196+
requireMutualAuth,
178197
requestDelegCreds,
179198
ignoreMissingToken
180199
);

httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/gss/GssSchemeBase.java

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ enum State {
8484
private static final int MAX_GSS_CHALLENGES = 3;
8585
private final GssConfig config;
8686
private final DnsResolver dnsResolver;
87-
private final boolean mutualAuth;
87+
private final boolean requireMutualAuth;
8888
private final boolean ignoreMissingToken;
8989
private int challengesLeft = MAX_GSS_CHALLENGES;
9090

@@ -99,7 +99,7 @@ enum State {
9999
super();
100100
this.config = config != null ? config : GssConfig.DEFAULT;
101101
this.dnsResolver = dnsResolver != null ? dnsResolver : SystemDefaultDnsResolver.INSTANCE;
102-
this.mutualAuth = config.isRequestMutualAuth();
102+
this.requireMutualAuth = config.isRequireMutualAuth();
103103
this.ignoreMissingToken = config.isIgnoreMissingToken();
104104
this.state = State.UNINITIATED;
105105
}
@@ -186,7 +186,7 @@ public void processChallenge(
186186
case TOKEN_SENT:
187187
if (challengeToken == null) {
188188
if (!challenged && ignoreMissingToken) {
189-
// Got a 200 without a challenge. Old non RFC compliant server.
189+
// Got a Non 401/407 code without a challenge. Old non RFC compliant server.
190190
if (LOG.isDebugEnabled()) {
191191
final HttpClientContext clientContext = HttpClientContext.cast(context);
192192
final String exchangeId = clientContext.getExchangeId();
@@ -213,23 +213,32 @@ public void processChallenge(
213213
if (LOG.isDebugEnabled()) {
214214
final HttpClientContext clientContext = HttpClientContext.cast(context);
215215
final String exchangeId = clientContext.getExchangeId();
216-
LOG.debug("{} GSSContext is not established ", exchangeId);
216+
LOG.debug("{} GSSContext is not established.", exchangeId);
217217
}
218218
state = State.FAILED;
219219
// TODO should we have specific exception(s) for these ?
220220
throw new AuthenticationException(
221-
"requireMutualAuth is set but GSSContext is not established");
222-
} else if (mutualAuth && !gssContext.getMutualAuthState()) {
223-
if (LOG.isDebugEnabled()) {
224-
final HttpClientContext clientContext = HttpClientContext.cast(context);
225-
final String exchangeId = clientContext.getExchangeId();
226-
LOG.debug("{} requireMutualAuth is set but GSSAUthContext does not have"
227-
+ " mutualAuthState set",
228-
exchangeId);
221+
"GSSContext is not established.");
222+
} else if (!gssContext.getMutualAuthState()) {
223+
if (requireMutualAuth) {
224+
if (LOG.isDebugEnabled()) {
225+
final HttpClientContext clientContext = HttpClientContext.cast(context);
226+
final String exchangeId = clientContext.getExchangeId();
227+
LOG.debug("{} requireMutualAuth is true but GSSContext mutualAuthState is false",
228+
exchangeId);
229+
}
230+
state = State.FAILED;
231+
throw new AuthenticationException(
232+
"requireMutualAuth is true but GSSContext mutualAuthState is false");
233+
} else {
234+
if (LOG.isDebugEnabled()) {
235+
final HttpClientContext clientContext = HttpClientContext.cast(context);
236+
final String exchangeId = clientContext.getExchangeId();
237+
LOG.debug("{} GSSContext MutualAuthState is false, but continuing because GssConfig.requireMutualAuth is false.",
238+
exchangeId);
239+
}
240+
state = State.FAILED;
229241
}
230-
state = State.FAILED;
231-
throw new AuthenticationException(
232-
"requireMutualAuth is set but GSSContext mutualAuthState is not set");
233242
} else {
234243
state = State.SUCCEEDED;
235244
}
@@ -289,7 +298,7 @@ protected GSSContext createGSSContext(
289298
final GSSCredential gssCredential) throws GSSException {
290299
final GSSContext gssContext = manager.createContext(peerName.canonicalize(oid), oid, gssCredential,
291300
GSSContext.DEFAULT_LIFETIME);
292-
gssContext.requestMutualAuth(mutualAuth);
301+
gssContext.requestMutualAuth(config.isRequestMutualAuth());
293302
gssContext.requestCredDeleg(config.isRequestDelegCreds());
294303
return gssContext;
295304
}

0 commit comments

Comments
 (0)