[java-idp-plugin-webauthn] branch main updated: Add more tests
Phil Smart
philip.smart at jisc.ac.uk
Wed Jul 31 15:45:35 UTC 2024
This is an automated email from the git hooks/post-receive script.
philsmart pushed a commit to branch main
in repository java-idp-plugin-webauthn.
View the commit online:
http://git.shibboleth.net/view/?p=java-idp-plugin-webauthn.git;a=commit;h=48cdf82ab7d95f5dd40a35c74fb27816b3a0beeb
The following commit(s) were added to refs/heads/main by this push:
new 48cdf82 Add more tests
48cdf82 is described below
commit 48cdf82ab7d95f5dd40a35c74fb27816b3a0beeb
Author: Phil Smart <philip.smart at jisc.ac.uk>
AuthorDate: Wed Jul 31 16:45:32 2024 +0100
Add more tests
---
.../admin/impl/LookupCredentialsForUserTest.java | 90 ++++++++++++++
.../PopulateWebAuthnManagementContextTest.java | 82 ++++++++++++
.../PopulateWebAuthnRegistrationContextTest.java | 103 +++++++++++++++
.../admin/impl/StorePublicKeyCredentialTest.java | 138 +++++++++++++++++++++
.../authn/webauthn/impl/AbstractWebAuthnTest.java | 8 ++
5 files changed, 421 insertions(+)
diff --git a/webauthn-impl/src/test/java/net/shibboleth/idp/plugin/authn/webauthn/admin/impl/LookupCredentialsForUserTest.java b/webauthn-impl/src/test/java/net/shibboleth/idp/plugin/authn/webauthn/admin/impl/LookupCredentialsForUserTest.java
new file mode 100644
index 0000000..1cf4aef
--- /dev/null
+++ b/webauthn-impl/src/test/java/net/shibboleth/idp/plugin/authn/webauthn/admin/impl/LookupCredentialsForUserTest.java
@@ -0,0 +1,90 @@
+/*
+ * 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.idp.plugin.authn.webauthn.admin.impl;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNull;
+
+import org.springframework.webflow.execution.Event;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import net.shibboleth.idp.plugin.authn.webauthn.context.WebAuthnManagementContext;
+import net.shibboleth.idp.plugin.authn.webauthn.impl.AbstractWebAuthnTest;
+import net.shibboleth.shared.component.ComponentInitializationException;
+
+/**
+ * Tests for {@link LookupCredentialsForUser}
+ */
+public class LookupCredentialsForUserTest extends AbstractWebAuthnTest {
+
+ private LookupCredentialsForUser action;
+
+ private WebAuthnManagementContext context;
+
+ @Override
+ @BeforeMethod
+ public void setup() throws Exception {
+ super.setup();
+ context = addWebAuthnManagementContext();
+ action = new LookupCredentialsForUser();
+ action.setWebAuthnClient(client);
+ action.setCredentialRepository(credentialRepo);
+ }
+
+ @Test
+ public void testLookupUserNoCredentials() throws ComponentInitializationException {
+ action.initialize();
+
+ context.setSearchUsername("jdoe");
+ final Event result = action.execute(src);
+ assertNull(result);
+ assertEquals(context.getFoundCredentials().size(), 0);
+ }
+
+ @Test
+ public void testLookupUserNoSearchUser() throws ComponentInitializationException {
+ action.initialize();
+
+ //context.setSearchUsername("jdoe");
+ final Event result = action.execute(src);
+ assertNull(result);
+ assertEquals(context.getFoundCredentials().size(), 0);
+ }
+
+ @Test
+ public void testLookupUserOneCredential() throws Exception {
+ credentialRepo.addRegistrationByUsername("jdoe", createCredentialRegistration());
+ action.initialize();
+
+ context.setSearchUsername("jdoe");
+ final Event result = action.execute(src);
+ assertNull(result);
+ assertEquals(context.getFoundCredentials().size(), 1);
+ }
+
+ @Test
+ public void testLookupUserOTwoCredentials() throws Exception {
+ credentialRepo.addRegistrationByUsername("jdoe", createCredentialRegistration());
+ credentialRepo.addRegistrationByUsername("jdoe", createCredentialRegistration());
+ action.initialize();
+
+ context.setSearchUsername("jdoe");
+ final Event result = action.execute(src);
+ assertNull(result);
+ assertEquals(context.getFoundCredentials().size(), 2);
+ }
+
+}
diff --git a/webauthn-impl/src/test/java/net/shibboleth/idp/plugin/authn/webauthn/admin/impl/PopulateWebAuthnManagementContextTest.java b/webauthn-impl/src/test/java/net/shibboleth/idp/plugin/authn/webauthn/admin/impl/PopulateWebAuthnManagementContextTest.java
new file mode 100644
index 0000000..fb377fa
--- /dev/null
+++ b/webauthn-impl/src/test/java/net/shibboleth/idp/plugin/authn/webauthn/admin/impl/PopulateWebAuthnManagementContextTest.java
@@ -0,0 +1,82 @@
+/*
+ * 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.idp.plugin.authn.webauthn.admin.impl;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+
+import org.opensaml.profile.action.EventIds;
+import org.springframework.webflow.execution.Event;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import net.shibboleth.idp.plugin.authn.webauthn.context.WebAuthnManagementContext;
+import net.shibboleth.idp.plugin.authn.webauthn.impl.AbstractWebAuthnTest;
+import net.shibboleth.shared.component.ComponentInitializationException;
+
+/**
+ * Tests for {@link PopulateWebAuthnManagementContext}
+ */
+public class PopulateWebAuthnManagementContextTest extends AbstractWebAuthnTest {
+
+ private PopulateWebAuthnManagementContext action;
+
+ @Override
+ @BeforeMethod
+ public void setup() throws Exception {
+ super.setup();
+ action = new PopulateWebAuthnManagementContext();
+
+ }
+
+ @Test
+ public void testPopulateManagmentContext() throws ComponentInitializationException {
+ action.setPrincipalNameLookupStrategy(prc -> "jdoe");
+ action.initialize();
+
+ final Event result = action.execute(src);
+ assertNull(result);
+ final var manCtxt = prc.getSubcontext(WebAuthnManagementContext.class);
+ assertNotNull(manCtxt);
+ assert manCtxt != null;
+ assertEquals(manCtxt.getPrincipalName(), "jdoe");
+ }
+
+ @Test
+ public void test_NoPrincipalReturned() throws ComponentInitializationException {
+
+ action.setPrincipalNameLookupStrategy(prc -> null);
+ action.initialize();
+
+ final Event result = action.execute(src);
+ assertNotNull(result);
+ assert result != null;
+ assertEquals(result.getId(), EventIds.INVALID_PROFILE_CTX);
+ }
+
+ @Test
+ public void test_NoContextReturned() throws ComponentInitializationException {
+
+ action.setWebAuthnManagementContextCreationStrategy(prc -> null);
+ action.initialize();
+
+ final Event result = action.execute(src);
+ assertNotNull(result);
+ assert result != null;
+ assertEquals(result.getId(), EventIds.INVALID_PROFILE_CTX);
+ }
+
+}
diff --git a/webauthn-impl/src/test/java/net/shibboleth/idp/plugin/authn/webauthn/admin/impl/PopulateWebAuthnRegistrationContextTest.java b/webauthn-impl/src/test/java/net/shibboleth/idp/plugin/authn/webauthn/admin/impl/PopulateWebAuthnRegistrationContextTest.java
new file mode 100644
index 0000000..02156d0
--- /dev/null
+++ b/webauthn-impl/src/test/java/net/shibboleth/idp/plugin/authn/webauthn/admin/impl/PopulateWebAuthnRegistrationContextTest.java
@@ -0,0 +1,103 @@
+/*
+ * 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.idp.plugin.authn.webauthn.admin.impl;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+
+import org.opensaml.profile.action.EventIds;
+import org.springframework.webflow.execution.Event;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import net.shibboleth.idp.plugin.authn.webauthn.context.WebAuthnRegistrationContext;
+import net.shibboleth.idp.plugin.authn.webauthn.impl.AbstractWebAuthnTest;
+import net.shibboleth.shared.component.ComponentInitializationException;
+
+/**
+ * Tests for {@link PopulateWebAuthnRegistrationContext}
+ */
+public class PopulateWebAuthnRegistrationContextTest extends AbstractWebAuthnTest {
+
+ private PopulateWebAuthnRegistrationContext action;
+
+ @Override
+ @BeforeMethod
+ public void setup() throws Exception {
+ super.setup();
+ action = new PopulateWebAuthnRegistrationContext();
+
+ }
+
+ @Test
+ public void testPopulateRegistrationContext_UsernameNotRequired() throws ComponentInitializationException {
+ action.setWebAuthnRegistrationContextCreationStrategy(pc -> new WebAuthnRegistrationContext());
+ action.setUsernameRequired(false);
+ action.initialize();
+
+ final Event result = action.execute(src);
+ assertNull(result);
+ final var regCtxt = prc.getSubcontext(WebAuthnRegistrationContext.class);
+ assertNotNull(regCtxt);
+ assert regCtxt != null;
+
+ }
+
+ @Test
+ public void testPopulateRegistrationContext_NoContext() throws ComponentInitializationException {
+ action.setWebAuthnRegistrationContextCreationStrategy(pc -> null);
+ action.setUsernameRequired(false);
+ action.initialize();
+
+ final Event result = action.execute(src);
+ assert result != null;
+ assertEquals(result.getId(), EventIds.INVALID_PROFILE_CTX);
+ }
+
+ @Test
+ public void testPopulateRegistrationContext_UsernameRequired() throws ComponentInitializationException {
+ action.setWebAuthnRegistrationContextCreationStrategy(
+ pc -> prc.getSubcontext(WebAuthnRegistrationContext.class));
+ action.setUsernameLookupStrategy(pc -> "jdoe");
+ action.setUsernameRequired(true);
+ action.initialize();
+
+ final Event result = action.execute(src);
+ assertNull(result);
+ final var regCtxt = prc.getSubcontext(WebAuthnRegistrationContext.class);
+ assertNotNull(regCtxt);
+ assert regCtxt != null;
+ assertEquals(regCtxt.getUsername(), "jdoe");
+
+ }
+
+ @Test
+ public void testPopulateRegistrationContext_UsernameRequiredButNotFound() throws ComponentInitializationException {
+ action.setWebAuthnRegistrationContextCreationStrategy(pc -> new WebAuthnRegistrationContext());
+ action.setUsernameLookupStrategy(prc -> null);
+ action.setUsernameRequired(true);
+ action.initialize();
+
+ final Event result = action.execute(src);
+ assertNotNull(result);
+ assert result != null;
+ assertEquals(result.getId(), EventIds.INVALID_PROFILE_CTX);
+
+ }
+
+
+
+}
diff --git a/webauthn-impl/src/test/java/net/shibboleth/idp/plugin/authn/webauthn/admin/impl/StorePublicKeyCredentialTest.java b/webauthn-impl/src/test/java/net/shibboleth/idp/plugin/authn/webauthn/admin/impl/StorePublicKeyCredentialTest.java
new file mode 100644
index 0000000..8aac3dd
--- /dev/null
+++ b/webauthn-impl/src/test/java/net/shibboleth/idp/plugin/authn/webauthn/admin/impl/StorePublicKeyCredentialTest.java
@@ -0,0 +1,138 @@
+/*
+ * 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.idp.plugin.authn.webauthn.admin.impl;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertTrue;
+
+import java.util.Optional;
+
+import org.springframework.webflow.execution.Event;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import com.yubico.webauthn.data.AttestationType;
+import com.yubico.webauthn.data.AuthenticatorAttestationResponse;
+import com.yubico.webauthn.data.ClientRegistrationExtensionOutputs;
+import com.yubico.webauthn.data.PublicKeyCredential;
+
+import net.shibboleth.idp.plugin.authn.webauthn.admin.RegistrationResult;
+import net.shibboleth.idp.plugin.authn.webauthn.admin.WebAuthnRegistrationEventIds;
+import net.shibboleth.idp.plugin.authn.webauthn.context.WebAuthnRegistrationContext;
+import net.shibboleth.idp.plugin.authn.webauthn.impl.AbstractWebAuthnTest;
+import net.shibboleth.idp.plugin.authn.webauthn.storage.CredentialRegistration;
+import net.shibboleth.shared.component.ComponentInitializationException;
+
+/**
+ * Tests for {@link StorePublicKeyCredential}
+ */
+public class StorePublicKeyCredentialTest extends AbstractWebAuthnTest {
+
+ private StorePublicKeyCredential action;
+
+ private WebAuthnRegistrationContext regContext;
+
+ private byte[] userId;
+
+ private PublicKeyCredential<AuthenticatorAttestationResponse, ClientRegistrationExtensionOutputs> cred;
+
+ @Override
+ @BeforeMethod
+ public void setup() throws Exception {
+ super.setup();
+ action = new StorePublicKeyCredential();
+ action.setCredentialRepository(credentialRepo);
+ action.setWebAuthnClient(client);
+
+ regContext = addWebAuthnRegistrationContext();
+ regContext.setUsername("jdoe");
+ regContext.setDisplayName("John Doe");
+ userId = generateRandomBytes(16);
+ regContext.setUserId(userId);
+
+ cred = createAttestationReponse();
+ regContext.setRegistrationResult(RegistrationResult.builder()
+ .withAttestationTrusted(true)
+ .withAttestationType(AttestationType.NONE).withCredential(cred).build());
+
+ }
+
+ @Test
+ public void testStoreCredential_NoUsername() throws ComponentInitializationException {
+ regContext.setUsername(null);
+ action.initialize();
+
+ final Event result = action.execute(src);
+ assertNotNull(result);
+ assert result != null;
+ assertEquals(result.getId(), WebAuthnRegistrationEventIds.INVALID_REGISTRATION);
+ }
+
+ @Test
+ public void testStoreCredential_NoDisplayName() throws ComponentInitializationException {
+ regContext.setDisplayName(null);
+ action.initialize();
+
+ final Event result = action.execute(src);
+ assertNotNull(result);
+ assert result != null;
+ assertEquals(result.getId(), WebAuthnRegistrationEventIds.INVALID_REGISTRATION);
+ }
+
+ @Test
+ public void testStoreCredential_NoRegistration() throws ComponentInitializationException {
+ regContext.setRegistrationResult(null);
+ action.initialize();
+
+ final Event result = action.execute(src);
+ assertNotNull(result);
+ assert result != null;
+ assertEquals(result.getId(), WebAuthnRegistrationEventIds.INVALID_REGISTRATION);
+ }
+
+ @Test
+ public void testStoreCredential() throws Exception {
+ action.initialize();
+
+ final Event result = action.execute(src);
+ assertNull(result);
+ final Optional<CredentialRegistration> credReg =
+ credentialRepo.getRegistrationByUsernameAndCredentialId("jdoe", cred.getId());
+ assertTrue(credReg.isPresent());
+ assertEquals(credReg.get().getUsername(), "jdoe");
+ assertEquals(credReg.get().getCredential().getCredentialId(), cred.getId());
+
+ }
+
+ @Test
+ public void testStoreCredential_LookupDifferentUser() throws Exception {
+ final var cred = createAttestationReponse();
+ regContext.setRegistrationResult(RegistrationResult.builder()
+ .withAttestationTrusted(true)
+ .withAttestationType(AttestationType.NONE).withCredential(cred).build());
+ action.initialize();
+
+ final Event result = action.execute(src);
+ assertNull(result);
+ final Optional<CredentialRegistration> credReg =
+ credentialRepo.getRegistrationByUsernameAndCredentialId("different", cred.getId());
+ assertTrue(credReg.isEmpty());
+
+
+ }
+
+}
diff --git a/webauthn-impl/src/test/java/net/shibboleth/idp/plugin/authn/webauthn/impl/AbstractWebAuthnTest.java b/webauthn-impl/src/test/java/net/shibboleth/idp/plugin/authn/webauthn/impl/AbstractWebAuthnTest.java
index 3268dd3..0ac5c24 100644
--- a/webauthn-impl/src/test/java/net/shibboleth/idp/plugin/authn/webauthn/impl/AbstractWebAuthnTest.java
+++ b/webauthn-impl/src/test/java/net/shibboleth/idp/plugin/authn/webauthn/impl/AbstractWebAuthnTest.java
@@ -60,6 +60,7 @@ import net.shibboleth.idp.plugin.authn.webauthn.client.WebAuthnAuthenticationCli
import net.shibboleth.idp.plugin.authn.webauthn.client.impl.MockWebAuthnClient;
import net.shibboleth.idp.plugin.authn.webauthn.context.BaseWebAuthnContext;
import net.shibboleth.idp.plugin.authn.webauthn.context.WebAuthnAuthenticationContext;
+import net.shibboleth.idp.plugin.authn.webauthn.context.WebAuthnManagementContext;
import net.shibboleth.idp.plugin.authn.webauthn.context.WebAuthnRegistrationContext;
import net.shibboleth.idp.plugin.authn.webauthn.storage.CredentialRegistration;
import net.shibboleth.idp.plugin.authn.webauthn.storage.WebAuthnCredentialRepository;
@@ -207,6 +208,13 @@ public abstract class AbstractWebAuthnTest {
return prc.ensureSubcontext(WebAuthnRegistrationContext.class);
}
+ /**
+ * Add a WebAuthn management context to the profile request context
+ */
+ protected WebAuthnManagementContext addWebAuthnManagementContext() {
+ return prc.ensureSubcontext(WebAuthnManagementContext.class);
+ }
+
/**
* Add a Base WebAuthn registration context to the authentication context
*/
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list