[java-plugin-shibd] branch main updated: Unit test and some fixes for storage state manager.
Scott Cantor
cantor.2 at osu.edu
Thu Aug 15 17:39:37 UTC 2024
This is an automated email from the git hooks/post-receive script.
scantor pushed a commit to branch main
in repository java-plugin-shibd.
View the commit online:
http://git.shibboleth.net/view/?p=java-plugin-shibd.git;a=commit;h=1030f19c6a4dc549b58310f8c387af2bb40f72e2
The following commit(s) were added to refs/heads/main by this push:
new 1030f19 Unit test and some fixes for storage state manager.
1030f19 is described below
commit 1030f19c6a4dc549b58310f8c387af2bb40f72e2
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Thu Aug 15 13:39:34 2024 -0400
Unit test and some fixes for storage state manager.
---
.../shibboleth/sp/AbstractStateTokenManager.java | 16 +++--
.../sp/impl/StorageServiceStateTokenManager.java | 1 +
.../impl/StorageServiceStateTokenManagerTest.java | 80 ++++++++++++++++++++++
3 files changed, 93 insertions(+), 4 deletions(-)
diff --git a/sp-server-api/src/main/java/net/shibboleth/sp/AbstractStateTokenManager.java b/sp-server-api/src/main/java/net/shibboleth/sp/AbstractStateTokenManager.java
index f240912..9aca9da 100644
--- a/sp-server-api/src/main/java/net/shibboleth/sp/AbstractStateTokenManager.java
+++ b/sp-server-api/src/main/java/net/shibboleth/sp/AbstractStateTokenManager.java
@@ -23,6 +23,7 @@ import net.shibboleth.shared.component.AbstractIdentifiableInitializableComponen
import net.shibboleth.shared.component.ComponentInitializationException;
import net.shibboleth.shared.logic.Constraint;
import net.shibboleth.shared.security.IdentifierGenerationStrategy;
+import net.shibboleth.shared.security.IdentifierGenerationStrategy.ProviderType;
/**
* Base class for {@link StateTokenManager} implementations.
@@ -36,7 +37,9 @@ public abstract class AbstractStateTokenManager extends AbstractIdentifiableInit
/** Expiration for state token. */
@Nonnull private Duration expiration;
- /** Constructor. */
+ /**
+ * Constructor.
+ */
@SuppressWarnings("null")
public AbstractStateTokenManager() {
expiration = Duration.ofMinutes(30);
@@ -45,6 +48,8 @@ public abstract class AbstractStateTokenManager extends AbstractIdentifiableInit
/**
* Set {@link IdentifierGenerationStrategy} to use.
*
+ * <p>Defaults to a secure random source that produces 16 byte values.</p>
+ *
* @param strategy identifier generator strategy
*/
public void setIdentifierGenerationStrategy(@Nonnull final IdentifierGenerationStrategy strategy) {
@@ -75,15 +80,18 @@ public abstract class AbstractStateTokenManager extends AbstractIdentifiableInit
expiration = Constraint.isNotNull(exp, "Expiration cannot be null");
}
+ /** {@inheritDoc} */
+
/** {@inheritDoc} */
@Override
protected void doInitialize() throws ComponentInitializationException {
super.doInitialize();
-
+
if (identifierStrategy == null) {
- throw new ComponentInitializationException("IdentifierGenerationStrategy cannot be null");
+ identifierStrategy = IdentifierGenerationStrategy.getInstance(ProviderType.SECURE);
}
}
+
/**
* Generate a state token.
@@ -91,7 +99,7 @@ public abstract class AbstractStateTokenManager extends AbstractIdentifiableInit
* @return a new state token
*/
@Nonnull protected String generateToken() {
- return identifierStrategy.generateIdentifier();
+ return identifierStrategy.generateIdentifier(false);
}
}
\ No newline at end of file
diff --git a/sp-server-impl/src/main/java/net/shibboleth/sp/impl/StorageServiceStateTokenManager.java b/sp-server-impl/src/main/java/net/shibboleth/sp/impl/StorageServiceStateTokenManager.java
index 8ed36d7..df00cd0 100644
--- a/sp-server-impl/src/main/java/net/shibboleth/sp/impl/StorageServiceStateTokenManager.java
+++ b/sp-server-impl/src/main/java/net/shibboleth/sp/impl/StorageServiceStateTokenManager.java
@@ -88,6 +88,7 @@ public class StorageServiceStateTokenManager extends AbstractStateTokenManager {
return key;
}
+ // Should only happen if the randomizer failed...
throw new IOException("Unable to create storage record for state token");
}
diff --git a/sp-server-impl/src/test/java/net/shibboleth/sp/impl/StorageServiceStateTokenManagerTest.java b/sp-server-impl/src/test/java/net/shibboleth/sp/impl/StorageServiceStateTokenManagerTest.java
new file mode 100644
index 0000000..dc90a45
--- /dev/null
+++ b/sp-server-impl/src/test/java/net/shibboleth/sp/impl/StorageServiceStateTokenManagerTest.java
@@ -0,0 +1,80 @@
+/*
+ * 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.impl;
+
+import java.io.IOException;
+import java.time.Duration;
+
+import org.opensaml.storage.impl.MemoryStorageService;
+import org.testng.Assert;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import net.shibboleth.shared.component.ComponentInitializationException;
+import net.shibboleth.sp.profile.impl.BaseAgplicationActionTest;
+
+/**
+ * Unit tests for {@link StorageServiceStateTokenManager}.
+ */
+ at SuppressWarnings("javadoc")
+public class StorageServiceStateTokenManagerTest extends BaseAgplicationActionTest {
+
+ private MemoryStorageService storageService;
+ private StorageServiceStateTokenManager stateManager;
+
+ @BeforeClass
+ public void setUp() throws ComponentInitializationException {
+ storageService = new MemoryStorageService();
+ storageService.setId("test");
+ storageService.setCleanupInterval(Duration.ZERO);
+ storageService.initialize();
+
+ stateManager = new StorageServiceStateTokenManager();
+ stateManager.setStorageService(storageService);
+ stateManager.setId("test");
+ stateManager.initialize();
+ }
+
+ @AfterClass
+ public void tearDown() {
+ stateManager.destroy();
+ storageService.destroy();
+ }
+
+ @BeforeMethod
+ public void beforeMethod() throws ComponentInitializationException {
+ super.beforeMethod();
+ }
+
+ @Test
+ public void testMissing() throws IOException {
+ Assert.assertNull(stateManager.recoverFromStateToken(agent, application, "foo"));
+ }
+
+ @Test
+ public void testMapRecover() throws IOException {
+
+ final String token = stateManager.preserveToStateToken(agent, application, "foo".getBytes());
+ assert token != null;
+
+ final byte[] original = stateManager.recoverFromStateToken(agent, application, token);
+ Assert.assertEquals(original, "foo".getBytes());
+
+ Assert.assertNull(stateManager.recoverFromStateToken(agent, application, token));
+ }
+
+}
\ 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