You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by co...@apache.org on 2017/06/19 12:25:51 UTC
directory-kerby git commit: Change the TokenCache to write to a
specified file name instead of a default location
Repository: directory-kerby
Updated Branches:
refs/heads/trunk 106299efb -> a276e5f41
Change the TokenCache to write to a specified file name instead of a default location
Project: http://git-wip-us.apache.org/repos/asf/directory-kerby/repo
Commit: http://git-wip-us.apache.org/repos/asf/directory-kerby/commit/a276e5f4
Tree: http://git-wip-us.apache.org/repos/asf/directory-kerby/tree/a276e5f4
Diff: http://git-wip-us.apache.org/repos/asf/directory-kerby/diff/a276e5f4
Branch: refs/heads/trunk
Commit: a276e5f413989bbd7a55362a2521e2c460ad21c4
Parents: 106299e
Author: Colm O hEigeartaigh <co...@apache.org>
Authored: Mon Jun 19 13:11:33 2017 +0100
Committer: Colm O hEigeartaigh <co...@apache.org>
Committed: Mon Jun 19 13:11:33 2017 +0100
----------------------------------------------------------------------
.../test/jaas/TokenAuthLoginModule.java | 24 ++++++++++----------
.../kerb/integration/test/jaas/TokenCache.java | 16 +++++--------
.../integration/test/TokenLoginTestBase.java | 13 ++++++++---
3 files changed, 28 insertions(+), 25 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a276e5f4/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/jaas/TokenAuthLoginModule.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/jaas/TokenAuthLoginModule.java b/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/jaas/TokenAuthLoginModule.java
index ab67f10..f12f9f0 100644
--- a/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/jaas/TokenAuthLoginModule.java
+++ b/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/jaas/TokenAuthLoginModule.java
@@ -72,7 +72,7 @@ public class TokenAuthLoginModule implements LoginModule {
public static final String ARMOR_CACHE = "armorCache";
public static final String CREDENTIAL_CACHE = "credentialCache";
public static final String SIGN_KEY_FILE = "signKeyFile";
-
+
private static final Logger LOG = LoggerFactory.getLogger(TokenAuthLoginModule.class);
/** initial state*/
@@ -112,7 +112,7 @@ public class TokenAuthLoginModule implements LoginModule {
}
if ((String) options.get(CREDENTIAL_CACHE) != null) {
cCache = new File((String) options.get(CREDENTIAL_CACHE));
- }
+ }
if ((String) options.get(SIGN_KEY_FILE) != null) {
signKeyFile = new File((String) options.get(SIGN_KEY_FILE));
}
@@ -135,7 +135,7 @@ public class TokenAuthLoginModule implements LoginModule {
@Override
public boolean commit() throws LoginException {
- if (succeeded == false) {
+ if (!succeeded) {
return false;
} else {
KerberosTicket ticket = null;
@@ -229,11 +229,11 @@ public class TokenAuthLoginModule implements LoginModule {
}
private void validateConfiguration() throws LoginException {
-
+
if (armorCache == null) {
throw new LoginException("An armor cache must be specified via the armorCache configuration option");
}
-
+
if (cCache == null) {
throw new LoginException("A credential cache must be specified via the credentialCache"
+ " configuration option");
@@ -260,7 +260,7 @@ public class TokenAuthLoginModule implements LoginModule {
}
krbToken = new KrbToken();
-
+
// Sign the token.
if (signKeyFile != null) {
try {
@@ -272,7 +272,7 @@ public class TokenAuthLoginModule implements LoginModule {
}
krbToken = new KrbToken(authToken, TokenFormat.JWT);
TokenEncoder tokenEncoder = KrbRuntime.getTokenProvider().createTokenEncoder();
-
+
if (tokenEncoder instanceof JwtTokenEncoder) {
PrivateKey signKey = null;
try {
@@ -284,10 +284,10 @@ public class TokenAuthLoginModule implements LoginModule {
} catch (Exception e) {
LOG.error(e.toString());
}
-
+
((JwtTokenEncoder) tokenEncoder).setSignKey((RSAPrivateKey) signKey);
}
-
+
krbToken.setTokenValue(tokenEncoder.encodeAsBytes(authToken));
} catch (KrbException e) {
throw new RuntimeException("Failed to encode AuthToken", e);
@@ -295,7 +295,7 @@ public class TokenAuthLoginModule implements LoginModule {
} else {
// Otherwise just write out the token (which could be already signed)
krbToken.setTokenValue(tokenStr.getBytes());
-
+
try {
JWT jwt = JWTParser.parse(tokenStr);
authToken = new JwtAuthToken(jwt.getJWTClaimsSet());
@@ -304,7 +304,7 @@ public class TokenAuthLoginModule implements LoginModule {
throw new RuntimeException("Failed to parse JWT token string", e);
}
}
-
+
krbToken.setInnerToken(authToken);
krbToken.setTokenType();
krbToken.setTokenFormat(TokenFormat.JWT);
@@ -319,7 +319,7 @@ public class TokenAuthLoginModule implements LoginModule {
} catch (KrbException | IOException e) {
LOG.error("KrbClient init failed. " + e.toString());
}
-
+
KrbTokenClient tokenClient = new KrbTokenClient(krbClient);
try {
tgtTicket = tokenClient.requestTgt(krbToken,
http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a276e5f4/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/jaas/TokenCache.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/jaas/TokenCache.java b/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/jaas/TokenCache.java
index db8ff73..f925bfc 100644
--- a/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/jaas/TokenCache.java
+++ b/kerby-kerb/integration-test/src/main/java/org/apache/kerby/kerberos/kerb/integration/test/jaas/TokenCache.java
@@ -25,13 +25,12 @@ import org.apache.kerby.kerberos.kerb.KrbException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
-import java.io.InputStreamReader;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
+import java.util.List;
/**
* This class provides APIs for converting token cache file with token string.
@@ -66,12 +65,9 @@ public class TokenCache {
String token = null;
try {
- BufferedReader reader = new BufferedReader(
- new InputStreamReader(Files.newInputStream(cacheFile.toPath()), StandardCharsets.UTF_8));
- String line = reader.readLine();
- reader.close();
- if (line != null) {
- token = line;
+ List<String> lines = Files.readAllLines(cacheFile.toPath());
+ if (lines != null && !lines.isEmpty()) {
+ token = lines.get(0);
}
} catch (IOException ex) {
LOG.error("Failed to read file: " + cacheFile.getName());
@@ -85,8 +81,8 @@ public class TokenCache {
*
* @param token The token string
*/
- public static void writeToken(String token) {
- File cacheFile = getDefaultTokenCache();
+ public static void writeToken(String token, String tokenCacheFile) {
+ File cacheFile = new File(tokenCacheFile);
try {
Writer writer = new FileWriterWithEncoding(cacheFile, StandardCharsets.UTF_8);
http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a276e5f4/kerby-kerb/integration-test/src/test/java/org/apache/kerby/kerberos/kerb/integration/test/TokenLoginTestBase.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/integration-test/src/test/java/org/apache/kerby/kerberos/kerb/integration/test/TokenLoginTestBase.java b/kerby-kerb/integration-test/src/test/java/org/apache/kerby/kerberos/kerb/integration/test/TokenLoginTestBase.java
index e31effc..2ceaa23 100644
--- a/kerby-kerb/integration-test/src/test/java/org/apache/kerby/kerberos/kerb/integration/test/TokenLoginTestBase.java
+++ b/kerby-kerb/integration-test/src/test/java/org/apache/kerby/kerberos/kerb/integration/test/TokenLoginTestBase.java
@@ -32,6 +32,7 @@ import org.apache.kerby.kerberos.kerb.type.ticket.TgtTicket;
import org.apache.kerby.kerberos.provider.token.JwtTokenProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.junit.After;
import org.junit.Before;
import javax.security.auth.Subject;
@@ -62,6 +63,12 @@ public class TokenLoginTestBase extends LoginTestBase {
armorCache = new File(getTestDir(), "armorcache.cc");
tgtCache = new File(getTestDir(), "tgtcache.cc");
signKeyFile = new File(this.getClass().getResource("/private_key.pem").getPath());
+ tokenCache = File.createTempFile("tokencache", null);
+ }
+
+ @After
+ public void cleanup() throws Exception {
+ tokenCache.delete();
}
@Override
@@ -88,9 +95,9 @@ public class TokenLoginTestBase extends LoginTestBase {
}
AuthToken token = issueToken(getClientPrincipal());
String tokenStr = tokenEncoder.encodeAsString(token);
- TokenCache.writeToken(tokenStr);
+
+ TokenCache.writeToken(tokenStr, tokenCache.getPath());
// System.out.println("Issued token: " + tokenStr);
- tokenCache = TokenCache.getDefaultTokenCache();
TgtTicket tgt = getKrbClient().requestTgt(getClientPrincipal(),
getClientPassword());
@@ -152,7 +159,7 @@ public class TokenLoginTestBase extends LoginTestBase {
createTokenAndArmorCache();
checkSubject(loginClientUsingTokenCache(tokenCache, armorCache, tgtCache, signKeyFile));
}
-
+
protected Subject testLoginWithTokenCacheAndRetSubject() throws Exception {
createTokenAndArmorCache();
Subject subj = loginClientUsingTokenCache(tokenCache, armorCache, tgtCache, signKeyFile);