[java-plugin-shibd] branch main updated: WIP on storage-backed StateManager.

Codeberg noreply at shibboleth.net
Fri Apr 17 16:16:31 UTC 2026


This is an automated email from the git hooks/post-receive script.

codeberg pushed a commit to branch main
in repository java-plugin-shibd.

View the commit online:
https://codeberg.org/Shibboleth/java-plugin-shibd/commit/f8575cc1b52c29392ce7a1ab01f704ecb7524509

The following commit(s) were added to refs/heads/main by this push:
     new f8575cc  WIP on storage-backed StateManager.
f8575cc is described below

commit f8575cc1b52c29392ce7a1ab01f704ecb7524509
Author: Scott Cantor <scott at restingparrotsoftware.com>
AuthorDate: Fri Apr 17 12:14:54 2026 -0400

    WIP on storage-backed StateManager.
---
 .../sp/state/impl/StorageServiceStateManager.java  | 141 +++++++++++++++++++++
 1 file changed, 141 insertions(+)

diff --git a/sp-server-impl/src/main/java/net/shibboleth/sp/state/impl/StorageServiceStateManager.java b/sp-server-impl/src/main/java/net/shibboleth/sp/state/impl/StorageServiceStateManager.java
new file mode 100644
index 0000000..e7a62dc
--- /dev/null
+++ b/sp-server-impl/src/main/java/net/shibboleth/sp/state/impl/StorageServiceStateManager.java
@@ -0,0 +1,141 @@
+/*
+ * Licensed 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 net.shibboleth.sp.state.impl;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.time.Instant;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import org.opensaml.storage.StorageRecord;
+import org.opensaml.storage.StorageService;
+import org.slf4j.Logger;
+
+import net.shibboleth.shared.annotation.constraint.NonnullAfterInit;
+import net.shibboleth.shared.annotation.constraint.NotEmpty;
+import net.shibboleth.shared.codec.Base64Support;
+import net.shibboleth.shared.codec.DecodingException;
+import net.shibboleth.shared.codec.EncodingException;
+import net.shibboleth.shared.component.ComponentInitializationException;
+import net.shibboleth.shared.logic.Constraint;
+import net.shibboleth.shared.primitive.LoggerFactory;
+import net.shibboleth.sp.Agent;
+import net.shibboleth.sp.Application;
+import net.shibboleth.sp.state.AbstractStateManager;
+import net.shibboleth.sp.state.StateManager;
+
+/**
+ * {@link StateManager} implemented with a {@link StorageService}.
+ */
+public class StorageServiceStateManager extends AbstractStateManager {
+    
+    /** Class logger. */
+    @Nonnull private Logger log = LoggerFactory.getLogger(StorageServiceStateManager.class);
+    
+    /** Storage back-end. */
+    @NonnullAfterInit private StorageService storageService;
+    
+    /**
+     * Set {@link StorageService} to use.
+     * 
+     * @param storage storage service
+     */
+    public void setStorageService(@Nonnull final StorageService storage) {
+        checkSetterPreconditions();
+        
+        storageService = Constraint.isNotNull(storage, "StorageService cannot be null");
+    }
+    
+    /** {@inheritDoc} */
+    @Override
+    protected void doInitialize() throws ComponentInitializationException {
+        super.doInitialize();
+        
+        if (storageService == null) {
+            throw new ComponentInitializationException("StorageService cannot be null");
+        } else if (!storageService.getCapabilities().isServerSide()) {
+            throw new ComponentInitializationException("StorageService cannot be client-side");
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    @Nonnull protected String doPreserve(@Nonnull final Agent agent, @Nonnull final Application application,
+            @Nonnull final String data) throws IOException {
+        
+        final String context = getContext(agent, application);
+        final String key = generateToken();
+
+        // Encode to base64 if not already handled by DataSealer.
+        
+        String encoded;
+        try {
+            encoded = getDataSealer() != null ? data : Base64Support.encode(data.getBytes(StandardCharsets.UTF_8), false);
+        } catch (final EncodingException e) {
+            throw new IOException(e);
+        }
+        
+        if (storageService.create(context, key, encoded, Instant.now().plus(getExpiration()).toEpochMilli())) {
+            log.trace("Created state token mapping ('{}', '{}') to value '{}'", context, key, encoded);
+            return key;
+        }
+        
+        throw new IOException("Unable to create storage record for state token");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    @Nullable protected String doRecover(@Nonnull final Agent agent, @Nonnull final Application application,
+            @Nonnull @NotEmpty final String stateToken) throws IOException {
+        
+        final String context = getContext(agent, application);
+        final StorageRecord<String> record = storageService.read(context, stateToken);
+        if (record != null) {
+            try {
+                storageService.delete(context, stateToken);
+            } catch (final IOException e) {
+                log.warn("Unable to delete state token ('{}', '{}') from storage", context, stateToken, e);
+            }
+            try {
+                final String decoded = getDataSealer() != null ? record.getValue() :
+                    new String(Base64Support.decode(record.getValue()), StandardCharsets.UTF_8);
+                log.trace("Recovered state token mapping ('{}', '{}') to value '{}'", context, stateToken, decoded);
+                return decoded;
+            } catch (final DecodingException e) {
+                throw new IOException(e);
+            }
+        }
+        
+        return null;
+    }
+    
+    /**
+     * Compute the storage context to use.
+     * 
+     * @param agent calling agent
+     * @param application calling application
+     * 
+     * @return storage context for request
+     */
+    @SuppressWarnings("null")
+    @Nonnull private String getContext(@Nonnull final Agent agent, @Nonnull final Application application) {
+        final StringBuilder builder = new StringBuilder(StorageServiceStateManager.class.getName());
+        builder.append('!').append(agent.getId()).append('!').append(application.getApplicationId());
+        return builder.toString();
+    }
+
+}
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list