pzampino commented on a change in pull request #440:
URL: https://github.com/apache/knox/pull/440#discussion_r623523680



##########
File path: 
gateway-provider-security-jwt/src/main/java/org/apache/knox/gateway/provider/federation/jwt/filter/SignatureVerificationCache.java
##########
@@ -0,0 +1,141 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.knox.gateway.provider.federation.jwt.filter;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.apache.knox.gateway.i18n.messages.MessagesFactory;
+import org.apache.knox.gateway.provider.federation.jwt.JWTMessages;
+
+import javax.servlet.FilterConfig;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * A shared record of tokens for which the signature has been verified.
+ */
+public class SignatureVerificationCache {
+
+    public static final String JWT_VERIFIED_CACHE_MAX = 
"jwt.verified.cache.max";
+    public static final int    JWT_VERIFIED_CACHE_MAX_DEFAULT = 250;
+
+    static final String DEFAULT_CACHE_ID = "default-cache";
+
+    static JWTMessages log = MessagesFactory.get( JWTMessages.class );
+
+    private static final Map<String, SignatureVerificationCache> instances = 
new HashMap<>();
+
+    /**
+     * Caches are topology-specific because the configuration is defined at 
the provider level.
+     *
+     * @param topology The topology for which the cache is being requested, or 
null if the default is sufficient.
+     * @param config   The FilterConfig associated with the calling provider.
+     *
+     * @return A SignatureVerificationCache for the specified topology, or the 
default one if no topology is specified.
+     */
+    @SuppressWarnings("PMD.SingletonClassReturningNewInstance")
+    public static SignatureVerificationCache getInstance(final String 
topology, final FilterConfig config) {
+        SignatureVerificationCache instance;
+        String cacheId = topology != null ? topology : DEFAULT_CACHE_ID;
+        synchronized (instances) {
+            instance = instances.get(cacheId);
+            if (instance == null) {
+                instance = new SignatureVerificationCache(config);
+                instances.put(cacheId, instance);
+                log.initializedSignatureVerificationCache(cacheId);
+            }
+        }
+        return instance;
+    }
+
+    private Cache<String, Boolean> verifiedTokens;
+
+    private SignatureVerificationCache(final FilterConfig config) {
+        initializeVerifiedTokensCache(config);
+    }
+
+    /**
+     * Initialize the cache for token verification records.
+     *
+     * @param config The configuration of the provider employing this cache.
+     */
+    private void initializeVerifiedTokensCache(final FilterConfig config) {
+        int maxCacheSize = JWT_VERIFIED_CACHE_MAX_DEFAULT;
+
+        String configValue = config.getInitParameter(JWT_VERIFIED_CACHE_MAX);
+        if (configValue != null && !configValue.isEmpty()) {
+            try {
+                maxCacheSize = Integer.parseInt(configValue);
+            } catch (NumberFormatException e) {
+                log.invalidVerificationCacheMaxConfiguration(configValue);
+            }
+        }
+
+        verifiedTokens = 
Caffeine.newBuilder().maximumSize(maxCacheSize).build();
+    }
+
+    /**
+     * Determine if the specified JWT's signature has previously been 
successfully verified.
+     *
+     * @param jwt A serialized JWT.
+     *
+     * @return true, if the specified token has been previously verified; 
Otherwise, false.
+     */
+    public boolean hasSignatureBeenVerified(final String jwt) {
+        return (verifiedTokens.getIfPresent(jwt) != null);
+    }
+
+    /**
+     * Record a successful token signature verification.
+     *
+     * @param jwt A serialized JWT for which the signature has been 
successfully verified.
+     */
+    public void recordSignatureVerification(final String jwt) {
+        verifiedTokens.put(jwt, true);
+    }
+
+    /**
+     * Explicitly evict the signature verification record from the cache if it 
exists.
+     *
+     * @param jwt The serialized JWT for which the associated signature 
verification record should be evicted.
+     */
+    public void removeSignatureVerificationRecord(final String jwt) {
+         verifiedTokens.asMap().remove(jwt);
+    }
+
+    /**
+     * @return The size of the cache.
+     */
+    public long getSize() {
+        return verifiedTokens.estimatedSize();
+    }
+
+    /**
+     * Explicitly clean up any records which should be evicted from the cache.
+     */
+    public void cleanUp() {

Review comment:
       Changed it to performMaintenance()




-- 
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.

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


Reply via email to