exceptionfactory commented on a change in pull request #5262:
URL: https://github.com/apache/nifi/pull/5262#discussion_r680403257



##########
File path: 
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-security/src/main/java/org/apache/nifi/web/security/jwt/key/service/StandardVerificationKeyService.java
##########
@@ -0,0 +1,188 @@
+/*
+ * 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.nifi.web.security.jwt.key.service;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
+import org.apache.nifi.components.state.Scope;
+import org.apache.nifi.components.state.StateManager;
+import org.apache.nifi.components.state.StateMap;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.security.Key;
+import java.security.KeyFactory;
+import java.security.NoSuchAlgorithmException;
+import java.security.spec.InvalidKeySpecException;
+import java.security.spec.KeySpec;
+import java.security.spec.X509EncodedKeySpec;
+import java.time.Instant;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+/**
+ * Standard Verification Key Service implemented using State Manager
+ */
+public class StandardVerificationKeyService implements VerificationKeyService {
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(StandardVerificationKeyService.class);
+
+    private static final ObjectMapper OBJECT_MAPPER = new 
ObjectMapper().registerModule(new JavaTimeModule());
+
+    private static final Scope SCOPE = Scope.LOCAL;
+
+    private final StateManager stateManager;
+
+    public StandardVerificationKeyService(final StateManager stateManager) {
+        this.stateManager = stateManager;
+    }
+
+    /**
+     * Find Key using specified Key Identifier
+     *
+     * @param id Key Identifier
+     * @return Optional Key
+     */
+    @Override
+    public Optional<Key> findById(final String id) {
+        final Optional<String> serializedKey = findSerializedKey(id);
+        return serializedKey.map(this::getVerificationKey).map(this::getKey);
+    }
+
+    /**
+     * Delete Expired Verification Keys is synchronized to avoid losing 
updates from other methods
+     */
+    @Override
+    public synchronized void deleteExpired() {
+        final Map<String, String> state = getStateMap().toMap();
+
+        final Instant now = Instant.now();
+        final Map<String, String> updatedState = state
+                .values()
+                .stream()
+                .map(this::getVerificationKey)
+                .filter(verificationKey -> 
verificationKey.getExpiration().isAfter(now))
+                .collect(Collectors.toMap(VerificationKey::getId, 
this::serializeVerificationKey));
+
+        if (updatedState.equals(state)) {
+            LOGGER.debug("Expired Verification Keys not found");
+        } else {
+            try {
+                stateManager.setState(updatedState, SCOPE);
+            } catch (final IOException e) {
+                throw new UncheckedIOException("Delete Expired Verification 
Keys Failed", e);
+            }
+            LOGGER.debug("Delete Expired Verification Keys: Before [{}] After 
[{}]", state.size(), updatedState.size());

Review comment:
       Thanks, will adjust the message.




-- 
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: issues-unsubscr...@nifi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to