Skip to content

Commit 3b3a7da

Browse files
nddipiazzaslandelle
authored andcommitted
Fix SpnegoEngine.getCompleteServicePrincipalName when servicePrincipalName is not specified. (AsyncHttpClient#1588)
* fix when servicePrincipalName is not specified. * unit test the fix.
1 parent 7908701 commit 3b3a7da

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

client/src/main/java/org/asynchttpclient/spnego/SpnegoEngine.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,18 +256,18 @@ public String generateToken(String host) throws SpnegoEngineException {
256256
}
257257
}
258258

259-
protected String getCompleteServicePrincipalName(String host) {
259+
String getCompleteServicePrincipalName(String host) {
260260
String name;
261261
if (servicePrincipalName == null) {
262262
if (useCanonicalHostname) {
263263
host = getCanonicalHostname(host);
264264
}
265-
name = "HTTP/" + host;
265+
name = "HTTP@" + host;
266266
} else {
267267
name = servicePrincipalName;
268-
}
269-
if (realmName != null) {
270-
name += "@" + realmName;
268+
if (realmName != null && !name.contains("@")) {
269+
name += "@" + realmName;
270+
}
271271
}
272272
log.debug("Service Principal Name is {}", name);
273273
return name;
@@ -285,7 +285,7 @@ private String getCanonicalHostname(String hostname) {
285285
return canonicalHostname;
286286
}
287287

288-
public CallbackHandler getUsernamePasswordHandler() {
288+
private CallbackHandler getUsernamePasswordHandler() {
289289
if (username == null) {
290290
return null;
291291
}

client/src/test/java/org/asynchttpclient/spnego/SpnegoEngineTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,44 @@ public void testSpnegoGenerateTokenWithCustomLoginConfig() throws Exception {
113113
Assert.assertTrue(token.startsWith("YII"));
114114
}
115115

116+
@Test
117+
public void testGetCompleteServicePrincipalName() throws Exception {
118+
{
119+
SpnegoEngine spnegoEngine = new SpnegoEngine(null,
120+
null,
121+
"bob",
122+
"service.ws.apache.org",
123+
false,
124+
null,
125+
null,
126+
null);
127+
Assert.assertEquals("[email protected]", spnegoEngine.getCompleteServicePrincipalName("localhost"));
128+
}
129+
{
130+
SpnegoEngine spnegoEngine = new SpnegoEngine(null,
131+
null,
132+
null,
133+
"service.ws.apache.org",
134+
true,
135+
null,
136+
null,
137+
null);
138+
Assert.assertNotEquals("HTTP@localhost", spnegoEngine.getCompleteServicePrincipalName("localhost"));
139+
Assert.assertTrue(spnegoEngine.getCompleteServicePrincipalName("localhost").startsWith("HTTP@"));
140+
}
141+
{
142+
SpnegoEngine spnegoEngine = new SpnegoEngine(null,
143+
null,
144+
null,
145+
"service.ws.apache.org",
146+
false,
147+
null,
148+
null,
149+
null);
150+
Assert.assertEquals("HTTP@localhost", spnegoEngine.getCompleteServicePrincipalName("localhost"));
151+
}
152+
}
153+
116154
@AfterClass
117155
public static void cleanup() throws Exception {
118156
if (kerbyServer != null) {

0 commit comments

Comments
 (0)