From 5cc7d555685f813b7dcd65bcb6c33d2267c0d755 Mon Sep 17 00:00:00 2001 From: uboness Date: Thu, 4 Sep 2014 09:16:19 +0200 Subject: [PATCH] cleaned up UsernamePasswordToken Removed the caching of the token on the request context. Caching is now handled by the InternalAuthenticationService Original commit: elastic/x-pack-elasticsearch@d60bc7af678318606b75f278868e5e4fb390fa1e --- .../shield/authc/InternalAuthenticationService.java | 2 +- .../shield/authc/support/UsernamePasswordToken.java | 13 +------------ .../authc/support/UsernamePasswordTokenTests.java | 4 ---- 3 files changed, 2 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/elasticsearch/shield/authc/InternalAuthenticationService.java b/src/main/java/org/elasticsearch/shield/authc/InternalAuthenticationService.java index aae79cacf82..d08f63cd91e 100644 --- a/src/main/java/org/elasticsearch/shield/authc/InternalAuthenticationService.java +++ b/src/main/java/org/elasticsearch/shield/authc/InternalAuthenticationService.java @@ -52,7 +52,7 @@ public class InternalAuthenticationService extends AbstractComponent implements @Override @SuppressWarnings("unchecked") public AuthenticationToken token(String action, TransportMessage message, AuthenticationToken defaultToken) { - AuthenticationToken token = (AuthenticationToken) message.getContext().get(TOKEN_CTX_KEY); + AuthenticationToken token = message.getFromContext(TOKEN_CTX_KEY); if (token != null) { return token; } diff --git a/src/main/java/org/elasticsearch/shield/authc/support/UsernamePasswordToken.java b/src/main/java/org/elasticsearch/shield/authc/support/UsernamePasswordToken.java index d4fcbbf8eb7..8469ae19a54 100644 --- a/src/main/java/org/elasticsearch/shield/authc/support/UsernamePasswordToken.java +++ b/src/main/java/org/elasticsearch/shield/authc/support/UsernamePasswordToken.java @@ -49,17 +49,8 @@ public class UsernamePasswordToken implements AuthenticationToken { } public static UsernamePasswordToken extractToken(TransportMessage message, UsernamePasswordToken defaultToken) { - UsernamePasswordToken token = (UsernamePasswordToken) message.getContext().get(TOKEN_KEY); - if (token != null) { - return token; - } - String authStr = message.getHeader(BASIC_AUTH_HEADER); if (authStr == null) { - if (defaultToken == null) { - return null; - } - message.putInContext(TOKEN_KEY, defaultToken); return defaultToken; } @@ -73,9 +64,7 @@ public class UsernamePasswordToken implements AuthenticationToken { if (i < 0) { throw new AuthenticationException("Invalid basic authentication header value"); } - token = new UsernamePasswordToken(userpasswd.substring(0, i), userpasswd.substring(i+1).toCharArray()); - message.putInContext(TOKEN_KEY, token); - return token; + return new UsernamePasswordToken(userpasswd.substring(0, i), userpasswd.substring(i+1).toCharArray()); } public static void putTokenHeader(TransportRequest request, UsernamePasswordToken token) { diff --git a/src/test/java/org/elasticsearch/shield/authc/support/UsernamePasswordTokenTests.java b/src/test/java/org/elasticsearch/shield/authc/support/UsernamePasswordTokenTests.java index bea262a3dea..a6a08a22ea8 100644 --- a/src/test/java/org/elasticsearch/shield/authc/support/UsernamePasswordTokenTests.java +++ b/src/test/java/org/elasticsearch/shield/authc/support/UsernamePasswordTokenTests.java @@ -49,10 +49,6 @@ public class UsernamePasswordTokenTests extends ElasticsearchTestCase { assertThat(token, notNullValue()); assertThat(token.principal(), equalTo("user1")); assertThat(new String(token.credentials()), equalTo("test123")); - - // making sure that indeed, once resolved the instance is reused across multiple resolve calls - UsernamePasswordToken token2 = UsernamePasswordToken.extractToken(request, null); - assertThat(token, is(token2)); } @Test