smolnar82 commented on a change in pull request #486:
URL: https://github.com/apache/knox/pull/486#discussion_r698049332



##########
File path: 
gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/TokenStateDatabase.java
##########
@@ -192,24 +191,34 @@ TokenMetadata getTokenMetadata(String tokenId) throws 
SQLException {
         final Map<String, String> metadataMap = new HashMap<>();
         while (rs.next()) {
           String metadataName = rs.getString(1);
-          metadataMap.put(metadataName, 
metadataName.equals(TokenMetadata.PASSCODE) ? new 
String(Base64.decodeBase64(rs.getString(2).getBytes(UTF_8)), UTF_8) : 
rs.getString(2));
+          metadataMap.put(metadataName, decodeMetadata(metadataName, 
rs.getString(2)));
         }
         return metadataMap.isEmpty() ? null : new TokenMetadata(metadataMap);
       }
     }
   }
 
+  private static String decodeMetadata(String metadataName, String 
metadataValue) {
+    return metadataName.equals(TokenMetadata.PASSCODE) ? new 
String(Base64.decodeBase64(metadataValue.getBytes(UTF_8)), UTF_8) : 
metadataValue;
+  }
+
   Collection<KnoxToken> getTokens(String userName) throws SQLException {
-    final Collection<KnoxToken> tokens = new TreeSet<>();
+    Map<String, KnoxToken> tokenMap = new LinkedHashMap<>();
     try (Connection connection = dataSource.getConnection(); PreparedStatement 
getTokenIdsStatement = 
connection.prepareStatement(GET_TOKENS_BY_USER_NAME_SQL)) {
       getTokenIdsStatement.setString(1, userName);
       try (ResultSet rs = getTokenIdsStatement.executeQuery()) {
         while(rs.next()) {
-          tokens.add(new KnoxToken(rs.getString(1), rs.getLong(2), 
rs.getLong(3), rs.getLong(4)));
+          String tokenId = rs.getString(1);
+          long issueTime = rs.getLong(2);
+          long expiration = rs.getLong(3);
+          long maxLifeTime = rs.getLong(4);
+          String metaName = rs.getString(5);
+          String metaValue = rs.getString(6);
+          KnoxToken token = tokenMap.computeIfAbsent(tokenId, id -> new 
KnoxToken(tokenId, issueTime, expiration, maxLifeTime));

Review comment:
       There is a KnoxToken constructor that takes a TokenMetadata; I'd use 
that one and remove the constructor being used here (in fact I added the one 
w/o metadata to support the 'poor performance' getTokens method which you are 
fixing now).
   In that case, a simple TressEt is enough instead of the LinkedHashMap.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to