From 782564859d6f3e2418a6aa7645048ead3d1d4d22 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9r=C3=B4me=20LELEU?= <jerome.leleu@teamdlab.com>
Date: Thu, 2 Apr 2020 10:42:05 +0200
Subject: [PATCH] refactor surrogation service definition

---
 cas/cas-server/pom.xml                        | 15 ------
 ...=> IamSurrogateAuthenticationService.java} | 10 ++--
 .../fr/gouv/vitamui/cas/config/AppConfig.java |  9 ++++
 ...rogateRestAuthenticationConfiguration.java | 46 -------------------
 ...amSurrogateAuthenticationServiceTest.java} |  8 ++--
 5 files changed, 18 insertions(+), 70 deletions(-)
 rename cas/cas-server/src/main/java/fr/gouv/vitamui/cas/authentication/{IamSurrogateRestAuthenticationService.java => IamSurrogateAuthenticationService.java} (90%)
 delete mode 100644 cas/cas-server/src/main/java/org/apereo/cas/config/SurrogateRestAuthenticationConfiguration.java
 rename cas/cas-server/src/test/java/fr/gouv/vitamui/cas/authentication/{IamSurrogateRestAuthenticationServiceTest.java => IamSurrogateAuthenticationServiceTest.java} (92%)

diff --git a/cas/cas-server/pom.xml b/cas/cas-server/pom.xml
index bca48749..282462ba 100644
--- a/cas/cas-server/pom.xml
+++ b/cas/cas-server/pom.xml
@@ -138,26 +138,11 @@
             <artifactId>cas-server-support-surrogate-authentication</artifactId>
             <version>${cas.version}</version>
         </dependency>
-        <dependency>
-            <groupId>org.apereo.cas</groupId>
-            <artifactId>cas-server-support-surrogate-authentication-rest</artifactId>
-            <version>${cas.version}</version>
-        </dependency>
         <dependency>
             <groupId>org.apereo.cas</groupId>
             <artifactId>cas-server-support-surrogate-webflow</artifactId>
             <version>${cas.version}</version>
         </dependency>
-        <dependency>
-            <groupId>org.apereo.cas</groupId>
-            <artifactId>cas-server-core-configuration</artifactId>
-            <version>${cas.version}</version>
-        </dependency>
-        <dependency>
-            <groupId>org.apereo.cas</groupId>
-            <artifactId>cas-server-core-webflow</artifactId>
-            <version>${cas.version}</version>
-        </dependency>
         <dependency>
             <groupId>org.apereo.cas</groupId>
             <artifactId>cas-server-core-services-api</artifactId>
diff --git a/cas/cas-server/src/main/java/fr/gouv/vitamui/cas/authentication/IamSurrogateRestAuthenticationService.java b/cas/cas-server/src/main/java/fr/gouv/vitamui/cas/authentication/IamSurrogateAuthenticationService.java
similarity index 90%
rename from cas/cas-server/src/main/java/fr/gouv/vitamui/cas/authentication/IamSurrogateRestAuthenticationService.java
rename to cas/cas-server/src/main/java/fr/gouv/vitamui/cas/authentication/IamSurrogateAuthenticationService.java
index 072a0374..24b76175 100644
--- a/cas/cas-server/src/main/java/fr/gouv/vitamui/cas/authentication/IamSurrogateRestAuthenticationService.java
+++ b/cas/cas-server/src/main/java/fr/gouv/vitamui/cas/authentication/IamSurrogateAuthenticationService.java
@@ -52,20 +52,20 @@ import java.util.List;
 import lombok.val;
 
 /**
- * Specific surrogate REST based on the IAM API.
+ * Specific surrogate service based on the IAM API.
  *
  *
  */
-public class IamSurrogateRestAuthenticationService extends BaseSurrogateAuthenticationService {
+public class IamSurrogateAuthenticationService extends BaseSurrogateAuthenticationService {
 
-    private static final VitamUILogger LOGGER = VitamUILoggerFactory.getInstance(IamSurrogateRestAuthenticationService.class);
+    private static final VitamUILogger LOGGER = VitamUILoggerFactory.getInstance(IamSurrogateAuthenticationService.class);
 
     private final CasExternalRestClient casExternalRestClient;
 
     private final Utils utils;
 
-    public IamSurrogateRestAuthenticationService(final CasExternalRestClient casExternalRestClient,
-                                                 final ServicesManager servicesManager, final Utils utils) {
+    public IamSurrogateAuthenticationService(final CasExternalRestClient casExternalRestClient,
+                                             final ServicesManager servicesManager, final Utils utils) {
         super(servicesManager);
         this.casExternalRestClient = casExternalRestClient;
         this.utils = utils;
diff --git a/cas/cas-server/src/main/java/fr/gouv/vitamui/cas/config/AppConfig.java b/cas/cas-server/src/main/java/fr/gouv/vitamui/cas/config/AppConfig.java
index a4635ed6..2c074020 100644
--- a/cas/cas-server/src/main/java/fr/gouv/vitamui/cas/config/AppConfig.java
+++ b/cas/cas-server/src/main/java/fr/gouv/vitamui/cas/config/AppConfig.java
@@ -37,6 +37,8 @@
 package fr.gouv.vitamui.cas.config;
 
 import fr.gouv.vitamui.cas.authentication.DelegatedSurrogateAuthenticationPostProcessor;
+import fr.gouv.vitamui.cas.authentication.IamSurrogateAuthenticationService;
+import lombok.SneakyThrows;
 import org.apereo.cas.audit.AuditableExecution;
 import org.apereo.cas.authentication.*;
 import org.apereo.cas.authentication.principal.PrincipalFactory;
@@ -260,4 +262,11 @@ public class AppConfig extends BaseTicketCatalogConfigurer {
         metadata.getProperties().setStorageTimeout(apiTokenTtl);
         registerTicketDefinition(plan, metadata);
     }
+
+    @RefreshScope
+    @Bean
+    @SneakyThrows
+    public SurrogateAuthenticationService surrogateAuthenticationService() {
+        return new IamSurrogateAuthenticationService(casRestClient(), servicesManager, utils());
+    }
 }
diff --git a/cas/cas-server/src/main/java/org/apereo/cas/config/SurrogateRestAuthenticationConfiguration.java b/cas/cas-server/src/main/java/org/apereo/cas/config/SurrogateRestAuthenticationConfiguration.java
deleted file mode 100644
index c3c6586b..00000000
--- a/cas/cas-server/src/main/java/org/apereo/cas/config/SurrogateRestAuthenticationConfiguration.java
+++ /dev/null
@@ -1,46 +0,0 @@
-package org.apereo.cas.config;
-
-import fr.gouv.vitamui.cas.util.Utils;
-import lombok.extern.slf4j.Slf4j;
-import org.apereo.cas.authentication.surrogate.SurrogateAuthenticationService;
-import org.apereo.cas.configuration.CasConfigurationProperties;
-import org.apereo.cas.services.ServicesManager;
-import org.springframework.beans.factory.ObjectProvider;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Qualifier;
-import org.springframework.boot.context.properties.EnableConfigurationProperties;
-import org.springframework.cloud.context.config.annotation.RefreshScope;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-import fr.gouv.vitamui.cas.authentication.IamSurrogateRestAuthenticationService;
-import fr.gouv.vitamui.iam.external.client.CasExternalRestClient;
-
-/**
- * Overrides the configuration class from the CAS server, using the CasExternalRestClient.
- *
- *
- */
-@Configuration(value = "surrogateRestAuthenticationConfiguration", proxyBeanMethods = false)
-@EnableConfigurationProperties(CasConfigurationProperties.class)
-@Slf4j
-public class SurrogateRestAuthenticationConfiguration {
-
-    @Autowired
-    @Qualifier("servicesManager")
-    private ObjectProvider<ServicesManager> servicesManager;
-
-    // customisation:
-    @Autowired
-    private CasExternalRestClient casExternalRestClient;
-
-    @Autowired
-    private Utils utils;
-
-    @RefreshScope
-    @Bean
-    public SurrogateAuthenticationService surrogateAuthenticationService() {
-        // customisation:
-        return new IamSurrogateRestAuthenticationService(casExternalRestClient, servicesManager.getObject(), utils);
-    }
-}
diff --git a/cas/cas-server/src/test/java/fr/gouv/vitamui/cas/authentication/IamSurrogateRestAuthenticationServiceTest.java b/cas/cas-server/src/test/java/fr/gouv/vitamui/cas/authentication/IamSurrogateAuthenticationServiceTest.java
similarity index 92%
rename from cas/cas-server/src/test/java/fr/gouv/vitamui/cas/authentication/IamSurrogateRestAuthenticationServiceTest.java
rename to cas/cas-server/src/test/java/fr/gouv/vitamui/cas/authentication/IamSurrogateAuthenticationServiceTest.java
index 3407b165..b94dd0ee 100644
--- a/cas/cas-server/src/test/java/fr/gouv/vitamui/cas/authentication/IamSurrogateRestAuthenticationServiceTest.java
+++ b/cas/cas-server/src/test/java/fr/gouv/vitamui/cas/authentication/IamSurrogateAuthenticationServiceTest.java
@@ -26,20 +26,20 @@ import static org.junit.Assert.*;
 import lombok.val;
 
 /**
- * Tests {@link IamSurrogateRestAuthenticationService}.
+ * Tests {@link IamSurrogateAuthenticationService}.
  *
  *
  */
 @RunWith(SpringRunner.class)
 @ContextConfiguration(classes = ServerIdentityAutoConfiguration.class)
 @TestPropertySource(locations = "classpath:/application-test.properties")
-public final class IamSurrogateRestAuthenticationServiceTest {
+public final class IamSurrogateAuthenticationServiceTest {
 
     private static final String SURROGATE = "surrogate";
     private static final String SU_ID = "id";
     private static final String SU_EMAIL = "superUser";
 
-    private IamSurrogateRestAuthenticationService service;
+    private IamSurrogateAuthenticationService service;
 
     private CasExternalRestClient casExternalRestClient;
 
@@ -48,7 +48,7 @@ public final class IamSurrogateRestAuthenticationServiceTest {
         casExternalRestClient = mock(CasExternalRestClient.class);
 
         val utils = new Utils(casExternalRestClient, null, 0, null, null);
-        service = new IamSurrogateRestAuthenticationService(casExternalRestClient, mock(ServicesManager.class), utils);
+        service = new IamSurrogateAuthenticationService(casExternalRestClient, mock(ServicesManager.class), utils);
     }
 
     @Test
-- 
GitLab