You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@syncope.apache.org by sk...@apache.org on 2020/03/09 11:58:54 UTC

[syncope] branch SYNCOPE-163-1 updated: [SYNCOPE-163] Added IT case for SAML 2 service provider

This is an automated email from the ASF dual-hosted git repository.

skylark17 pushed a commit to branch SYNCOPE-163-1
in repository https://gitbox.apache.org/repos/asf/syncope.git


The following commit(s) were added to refs/heads/SYNCOPE-163-1 by this push:
     new 878c270  [SYNCOPE-163] Added IT case for SAML 2 service provider
878c270 is described below

commit 878c270f9b02589c06175098e774662b92568a8f
Author: skylark17 <sk...@apache.org>
AuthorDate: Mon Mar 9 12:04:14 2020 +0100

    [SYNCOPE-163] Added IT case for SAML 2 service provider
---
 .../java/data/OIDCRelyingPartyDataBinderImpl.java  |  5 ++-
 .../data/SAML2ServiceProviderDataBinderImpl.java   | 52 +++++++++++++++++-----
 .../org/apache/syncope/fit/AbstractITCase.java     | 23 +++++++---
 .../syncope/fit/core/OIDCRelyingPartyITCase.java   | 22 ++++-----
 .../fit/core/SAML2ServiceProviderITCase.java       | 45 ++++++++++++++++++-
 5 files changed, 116 insertions(+), 31 deletions(-)

diff --git a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/OIDCRelyingPartyDataBinderImpl.java b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/OIDCRelyingPartyDataBinderImpl.java
index d2a5d1b..c96ea2a 100644
--- a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/OIDCRelyingPartyDataBinderImpl.java
+++ b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/OIDCRelyingPartyDataBinderImpl.java
@@ -52,8 +52,8 @@ public class OIDCRelyingPartyDataBinderImpl implements OIDCRelyingPartyDataBinde
 
     @Override
     public OIDCRelyingParty update(
-        final OIDCRelyingParty toBeUpdated,
-        final OIDCRelyingPartyTO applicationTO) {
+            final OIDCRelyingParty toBeUpdated,
+            final OIDCRelyingPartyTO applicationTO) {
 
         OIDCRelyingParty application = openIdConnectRelyingPartyDAO.save(toBeUpdated);
 
@@ -101,6 +101,7 @@ public class OIDCRelyingPartyDataBinderImpl implements OIDCRelyingPartyDataBinde
             accessPolicyTO.setKey(rp.getAccessPolicy().getKey());
             applicationTO.setAccessPolicy(accessPolicyTO);
         }
+
         return applicationTO;
     }
 }
diff --git a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/SAML2ServiceProviderDataBinderImpl.java b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/SAML2ServiceProviderDataBinderImpl.java
index dda8370..bedfc98 100644
--- a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/SAML2ServiceProviderDataBinderImpl.java
+++ b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/SAML2ServiceProviderDataBinderImpl.java
@@ -16,14 +16,19 @@
  * under the License.
  *
  */
-
 package org.apache.syncope.core.provisioning.java.data;
 
+import org.apache.syncope.common.lib.SyncopeClientException;
+import org.apache.syncope.common.lib.to.AccessPolicyTO;
+import org.apache.syncope.common.lib.to.AuthenticationPolicyTO;
 import org.apache.syncope.common.lib.to.client.SAML2ServiceProviderTO;
+import org.apache.syncope.common.lib.types.ClientExceptionType;
+import org.apache.syncope.core.persistence.api.dao.authentication.AccessPolicyDAO;
 import org.apache.syncope.core.persistence.api.dao.authentication.AuthenticationPolicyDAO;
 import org.apache.syncope.core.persistence.api.dao.authentication.SAML2ServiceProviderDAO;
 import org.apache.syncope.core.persistence.api.entity.EntityFactory;
 import org.apache.syncope.core.persistence.api.entity.authentication.SAML2ServiceProvider;
+import org.apache.syncope.core.persistence.api.entity.policy.AccessPolicy;
 import org.apache.syncope.core.persistence.api.entity.policy.AuthenticationPolicy;
 import org.apache.syncope.core.provisioning.api.data.SAML2ServiceProviderDataBinder;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -31,6 +36,7 @@ import org.springframework.stereotype.Component;
 
 @Component
 public class SAML2ServiceProviderDataBinderImpl implements SAML2ServiceProviderDataBinder {
+
     @Autowired
     private SAML2ServiceProviderDAO saml2ServiceProviderDAO;
 
@@ -40,14 +46,19 @@ public class SAML2ServiceProviderDataBinderImpl implements SAML2ServiceProviderD
     @Autowired
     private AuthenticationPolicyDAO authenticationPolicyDAO;
 
+    @Autowired
+    private AccessPolicyDAO accessPolicyDAO;
+
     @Override
     public SAML2ServiceProvider create(final SAML2ServiceProviderTO applicationTO) {
         return update(entityFactory.newEntity(SAML2ServiceProvider.class), applicationTO);
     }
 
     @Override
-    public SAML2ServiceProvider update(final SAML2ServiceProvider toBeUpdated,
-                                       final SAML2ServiceProviderTO applicationTO) {
+    public SAML2ServiceProvider update(
+            final SAML2ServiceProvider toBeUpdated,
+            final SAML2ServiceProviderTO applicationTO) {
+
         SAML2ServiceProvider application = saml2ServiceProviderDAO.save(toBeUpdated);
 
         application.setDescription(applicationTO.getDescription());
@@ -56,21 +67,42 @@ public class SAML2ServiceProviderDataBinderImpl implements SAML2ServiceProviderD
         application.setMetadataLocation(applicationTO.getMetadataLocation());
 
         AuthenticationPolicy authenticationPolicy = authenticationPolicyDAO.
-            find(applicationTO.getAuthenticationPolicy().getKey());
+                find(applicationTO.getAuthenticationPolicy().getKey());
+        if (authenticationPolicy == null) {
+            SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidEntity);
+            sce.getElements().add("Unable to locate authentication policy "
+                    + applicationTO.getAuthenticationPolicy().getKey());
+            throw sce;
+        }
         application.setAuthenticationPolicy(authenticationPolicy);
 
+        AccessPolicy accessPolicy = accessPolicyDAO.find(applicationTO.getAccessPolicy().getKey());
+        application.setAccessPolicy(accessPolicy);
+
         return application;
     }
 
     @Override
-    public SAML2ServiceProviderTO getClientApplicationTO(final SAML2ServiceProvider serviceProvider) {
+    public SAML2ServiceProviderTO getClientApplicationTO(final SAML2ServiceProvider sp) {
         SAML2ServiceProviderTO applicationTO = new SAML2ServiceProviderTO();
 
-        applicationTO.setKey(serviceProvider.getKey());
-        applicationTO.setDescription(serviceProvider.getDescription());
-        applicationTO.setEntityId(serviceProvider.getEntityId());
-        applicationTO.setMetadataLocation(serviceProvider.getMetadataLocation());
-        applicationTO.setName(serviceProvider.getName());
+        applicationTO.setKey(sp.getKey());
+        applicationTO.setDescription(sp.getDescription());
+        applicationTO.setEntityId(sp.getEntityId());
+        applicationTO.setMetadataLocation(sp.getMetadataLocation());
+        applicationTO.setName(sp.getName());
+
+        AuthenticationPolicyTO authenticationPolicyTO = new AuthenticationPolicyTO();
+        authenticationPolicyTO.setDescription(sp.getAuthenticationPolicy().getDescription());
+        authenticationPolicyTO.setKey(sp.getAuthenticationPolicy().getKey());
+        applicationTO.setAuthenticationPolicy(authenticationPolicyTO);
+
+        if (sp.getAccessPolicy() != null) {
+            AccessPolicyTO accessPolicyTO = new AccessPolicyTO();
+            accessPolicyTO.setDescription(sp.getAccessPolicy().getDescription());
+            accessPolicyTO.setKey(sp.getAccessPolicy().getKey());
+            applicationTO.setAccessPolicy(accessPolicyTO);
+        }
 
         return applicationTO;
     }
diff --git a/fit/core-reference/src/test/java/org/apache/syncope/fit/AbstractITCase.java b/fit/core-reference/src/test/java/org/apache/syncope/fit/AbstractITCase.java
index ab2ddfd..b3864e7 100644
--- a/fit/core-reference/src/test/java/org/apache/syncope/fit/AbstractITCase.java
+++ b/fit/core-reference/src/test/java/org/apache/syncope/fit/AbstractITCase.java
@@ -75,6 +75,7 @@ import org.apache.syncope.common.lib.to.ProvisioningResult;
 import org.apache.syncope.common.lib.to.ReportTO;
 import org.apache.syncope.common.lib.to.RoleTO;
 import org.apache.syncope.common.lib.to.UserTO;
+import org.apache.syncope.common.lib.to.client.SAML2ServiceProviderTO;
 import org.apache.syncope.common.lib.types.AnyTypeKind;
 import org.apache.syncope.common.lib.types.PatchOperation;
 import org.apache.syncope.common.lib.types.PolicyType;
@@ -289,7 +290,7 @@ public abstract class AbstractITCase {
 
     protected static SCIMConfService scimConfService;
 
-    protected static OIDCRelyingPartyService openIdConnectRelyingPartyService;
+    protected static OIDCRelyingPartyService oidcRelyingPartyService;
 
     protected static SAML2ServiceProviderService saml2ServiceProviderService;
 
@@ -362,7 +363,7 @@ public abstract class AbstractITCase {
         oidcClientService = adminClient.getService(OIDCClientService.class);
         oidcProviderService = adminClient.getService(OIDCProviderService.class);
         scimConfService = adminClient.getService(SCIMConfService.class);
-        openIdConnectRelyingPartyService = adminClient.getService(OIDCRelyingPartyService.class);
+        oidcRelyingPartyService = adminClient.getService(OIDCRelyingPartyService.class);
         saml2ServiceProviderService = adminClient.getService(SAML2ServiceProviderService.class);
     }
 
@@ -566,16 +567,26 @@ public abstract class AbstractITCase {
         return (T) getObject(response.getLocation(), PolicyService.class, policy.getClass());
     }
 
-    protected OIDCRelyingPartyTO createOpenIdConnectRelyingParty(final OIDCRelyingPartyTO rpTO) {
-        Response response = openIdConnectRelyingPartyService.create(rpTO);
+    protected OIDCRelyingPartyTO createOIDCRelyingParty(final OIDCRelyingPartyTO rpTO) {
+        Response response = oidcRelyingPartyService.create(rpTO);
         if (response.getStatusInfo().getStatusCode() != Response.Status.CREATED.getStatusCode()) {
             Exception ex = clientFactory.getExceptionMapper().fromResponse(response);
             if (ex != null) {
                 throw (RuntimeException) ex;
             }
         }
-        return getObject(response.getLocation(), OIDCRelyingPartyService.class,
-                OIDCRelyingPartyTO.class);
+        return getObject(response.getLocation(), OIDCRelyingPartyService.class, OIDCRelyingPartyTO.class);
+    }
+
+    protected SAML2ServiceProviderTO createSAML2SP(final SAML2ServiceProviderTO saml2spTO) {
+        Response response = saml2ServiceProviderService.create(saml2spTO);
+        if (response.getStatusInfo().getStatusCode() != Response.Status.CREATED.getStatusCode()) {
+            Exception ex = clientFactory.getExceptionMapper().fromResponse(response);
+            if (ex != null) {
+                throw (RuntimeException) ex;
+            }
+        }
+        return getObject(response.getLocation(), SAML2ServiceProviderService.class, SAML2ServiceProviderTO.class);
     }
 
     protected ResourceTO createResource(final ResourceTO resourceTO) {
diff --git a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/OIDCRelyingPartyITCase.java b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/OIDCRelyingPartyITCase.java
index 3c7d0d6..96a05d5 100644
--- a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/OIDCRelyingPartyITCase.java
+++ b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/OIDCRelyingPartyITCase.java
@@ -38,15 +38,15 @@ public class OIDCRelyingPartyITCase extends AbstractITCase {
 
     @Test
     public void create() {
-        createOpenIdConnectRelyingParty(buildRelyingParty());
+        createOIDCRelyingParty(buildRelyingParty());
     }
 
     @Test
     public void read() {
         OIDCRelyingPartyTO rpTO = buildRelyingParty();
-        rpTO = createOpenIdConnectRelyingParty(rpTO);
+        rpTO = createOIDCRelyingParty(rpTO);
 
-        OIDCRelyingPartyTO found = openIdConnectRelyingPartyService.read(rpTO.getKey());
+        OIDCRelyingPartyTO found = oidcRelyingPartyService.read(rpTO.getKey());
         assertNotNull(found);
         assertFalse(StringUtils.isBlank(found.getClientId()));
         assertTrue(StringUtils.isBlank(found.getClientSecret()));
@@ -57,7 +57,7 @@ public class OIDCRelyingPartyITCase extends AbstractITCase {
     @Test
     public void update() {
         OIDCRelyingPartyTO rpTO = buildRelyingParty();
-        rpTO = createOpenIdConnectRelyingParty(rpTO);
+        rpTO = createOIDCRelyingParty(rpTO);
 
         AccessPolicyTO accessPolicyTO = new AccessPolicyTO();
         accessPolicyTO.setKey("NewAccessPolicyTest_" + getUUIDString());
@@ -68,24 +68,24 @@ public class OIDCRelyingPartyITCase extends AbstractITCase {
         rpTO.setClientId("newClientId");
         rpTO.setAccessPolicy(accessPolicyTO);
 
-        openIdConnectRelyingPartyService.update(rpTO);
-        OIDCRelyingPartyTO updated = openIdConnectRelyingPartyService.read(rpTO.getKey());
+        oidcRelyingPartyService.update(rpTO);
+        OIDCRelyingPartyTO updated = oidcRelyingPartyService.read(rpTO.getKey());
 
         assertNotNull(updated);
         assertEquals("newClientId", updated.getClientId());
-        assertNotNull(rpTO.getAccessPolicy());
-        assertEquals("New Access policy", rpTO.getAccessPolicy().getDescription());
+        assertNotNull(updated.getAccessPolicy());
+        assertEquals("New Access policy", updated.getAccessPolicy().getDescription());
     }
 
     @Test
     public void delete() {
         OIDCRelyingPartyTO rpTO = buildRelyingParty();
-        rpTO = createOpenIdConnectRelyingParty(rpTO);
+        rpTO = createOIDCRelyingParty(rpTO);
 
-        openIdConnectRelyingPartyService.delete(rpTO.getKey());
+        oidcRelyingPartyService.delete(rpTO.getKey());
 
         try {
-            openIdConnectRelyingPartyService.read(rpTO.getKey());
+            oidcRelyingPartyService.read(rpTO.getKey());
             fail("This should not happen");
         } catch (SyncopeClientException e) {
             assertNotNull(e);
diff --git a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/SAML2ServiceProviderITCase.java b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/SAML2ServiceProviderITCase.java
index 8f217979..cc6856b 100644
--- a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/SAML2ServiceProviderITCase.java
+++ b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/SAML2ServiceProviderITCase.java
@@ -18,8 +18,13 @@
  */
 package org.apache.syncope.fit.core;
 
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.fail;
 
+import org.apache.commons.lang3.StringUtils;
+import org.apache.syncope.common.lib.SyncopeClientException;
 import org.apache.syncope.common.lib.to.AccessPolicyTO;
 import org.apache.syncope.common.lib.to.AuthenticationPolicyTO;
 import org.apache.syncope.common.lib.to.client.SAML2ServiceProviderTO;
@@ -31,22 +36,58 @@ public class SAML2ServiceProviderITCase extends AbstractITCase {
 
     @Test
     public void create() {
-
+        createSAML2SP(buildSAML2SP());
     }
 
     @Test
     public void read() {
-
+        SAML2ServiceProviderTO samlSpTO = buildSAML2SP();
+        samlSpTO = createSAML2SP(samlSpTO);
+
+        SAML2ServiceProviderTO found = saml2ServiceProviderService.read(samlSpTO.getKey());
+        assertNotNull(found);
+        assertFalse(StringUtils.isBlank(found.getEntityId()));
+        assertFalse(StringUtils.isBlank(found.getMetadataLocation()));
+        assertNotNull(found.getAccessPolicy());
+        assertNotNull(found.getAuthenticationPolicy());
     }
 
     @Test
     public void update() {
+        SAML2ServiceProviderTO samlSpTO = buildSAML2SP();
+        samlSpTO = createSAML2SP(samlSpTO);
 
+        AccessPolicyTO accessPolicyTO = new AccessPolicyTO();
+        accessPolicyTO.setKey("NewAccessPolicyTest_" + getUUIDString());
+        accessPolicyTO.setDescription("New Access policy");
+        accessPolicyTO = createPolicy(PolicyType.ACCESS, accessPolicyTO);
+        assertNotNull(accessPolicyTO);
+
+        samlSpTO.setEntityId("newEntityId");
+        samlSpTO.setAccessPolicy(accessPolicyTO);
+
+        saml2ServiceProviderService.update(samlSpTO);
+        SAML2ServiceProviderTO updated = saml2ServiceProviderService.read(samlSpTO.getKey());
+
+        assertNotNull(updated);
+        assertEquals("newEntityId", updated.getEntityId());
+        assertNotNull(updated.getAccessPolicy());
+        assertEquals("New Access policy", updated.getAccessPolicy().getDescription());
     }
 
     @Test
     public void delete() {
+        SAML2ServiceProviderTO samlSpTO = buildSAML2SP();
+        samlSpTO = createSAML2SP(samlSpTO);
+
+        saml2ServiceProviderService.delete(samlSpTO.getKey());
 
+        try {
+            saml2ServiceProviderService.read(samlSpTO.getKey());
+            fail("This should not happen");
+        } catch (SyncopeClientException e) {
+            assertNotNull(e);
+        }
     }
 
     private SAML2ServiceProviderTO buildSAML2SP() {