From ee99520fe5391a5413d09797ad637c57b72fac11 Mon Sep 17 00:00:00 2001 From: Nicholas DiPiazza Date: Fri, 19 Oct 2018 18:39:38 -0500 Subject: [PATCH 1/2] fix when servicePrincipalName is not specified. --- .../java/org/asynchttpclient/spnego/SpnegoEngine.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/client/src/main/java/org/asynchttpclient/spnego/SpnegoEngine.java b/client/src/main/java/org/asynchttpclient/spnego/SpnegoEngine.java index 7f887965ec..419e8d56f4 100644 --- a/client/src/main/java/org/asynchttpclient/spnego/SpnegoEngine.java +++ b/client/src/main/java/org/asynchttpclient/spnego/SpnegoEngine.java @@ -262,12 +262,12 @@ protected String getCompleteServicePrincipalName(String host) { if (useCanonicalHostname) { host = getCanonicalHostname(host); } - name = "HTTP/" + host; + name = "HTTP@" + host; } else { name = servicePrincipalName; - } - if (realmName != null) { - name += "@" + realmName; + if (realmName != null && !name.contains("@")) { + name += "@" + realmName; + } } log.debug("Service Principal Name is {}", name); return name; From f52c6e91a342c4586c31fb486a90363ee74e8d7e Mon Sep 17 00:00:00 2001 From: Nicholas DiPiazza Date: Fri, 19 Oct 2018 18:49:08 -0500 Subject: [PATCH 2/2] unit test the fix. --- .../asynchttpclient/spnego/SpnegoEngine.java | 4 +- .../spnego/SpnegoEngineTest.java | 38 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/client/src/main/java/org/asynchttpclient/spnego/SpnegoEngine.java b/client/src/main/java/org/asynchttpclient/spnego/SpnegoEngine.java index 419e8d56f4..515bf63184 100644 --- a/client/src/main/java/org/asynchttpclient/spnego/SpnegoEngine.java +++ b/client/src/main/java/org/asynchttpclient/spnego/SpnegoEngine.java @@ -256,7 +256,7 @@ public String generateToken(String host) throws SpnegoEngineException { } } - protected String getCompleteServicePrincipalName(String host) { + String getCompleteServicePrincipalName(String host) { String name; if (servicePrincipalName == null) { if (useCanonicalHostname) { @@ -285,7 +285,7 @@ private String getCanonicalHostname(String hostname) { return canonicalHostname; } - public CallbackHandler getUsernamePasswordHandler() { + private CallbackHandler getUsernamePasswordHandler() { if (username == null) { return null; } diff --git a/client/src/test/java/org/asynchttpclient/spnego/SpnegoEngineTest.java b/client/src/test/java/org/asynchttpclient/spnego/SpnegoEngineTest.java index bd8fbf34ea..92ff4a4d78 100644 --- a/client/src/test/java/org/asynchttpclient/spnego/SpnegoEngineTest.java +++ b/client/src/test/java/org/asynchttpclient/spnego/SpnegoEngineTest.java @@ -113,6 +113,44 @@ public void testSpnegoGenerateTokenWithCustomLoginConfig() throws Exception { Assert.assertTrue(token.startsWith("YII")); } + @Test + public void testGetCompleteServicePrincipalName() throws Exception { + { + SpnegoEngine spnegoEngine = new SpnegoEngine(null, + null, + "bob", + "service.ws.apache.org", + false, + null, + null, + null); + Assert.assertEquals("bob@service.ws.apache.org", spnegoEngine.getCompleteServicePrincipalName("localhost")); + } + { + SpnegoEngine spnegoEngine = new SpnegoEngine(null, + null, + null, + "service.ws.apache.org", + true, + null, + null, + null); + Assert.assertNotEquals("HTTP@localhost", spnegoEngine.getCompleteServicePrincipalName("localhost")); + Assert.assertTrue(spnegoEngine.getCompleteServicePrincipalName("localhost").startsWith("HTTP@")); + } + { + SpnegoEngine spnegoEngine = new SpnegoEngine(null, + null, + null, + "service.ws.apache.org", + false, + null, + null, + null); + Assert.assertEquals("HTTP@localhost", spnegoEngine.getCompleteServicePrincipalName("localhost")); + } + } + @AfterClass public static void cleanup() throws Exception { if (kerbyServer != null) {