From b27b1c7e43c35ab5e748b7caeed0f23d5dc12994 Mon Sep 17 00:00:00 2001
From: Alex ORLUC <alex.orluc@maarch.org>
Date: Fri, 16 Oct 2020 18:26:22 +0200
Subject: [PATCH] FEAT #13695 TIME 1:45 front logout cas

---
 .../controllers/AuthenticationController.php  | 10 ++++------
 src/frontend/service/auth.service.ts          | 20 ++++++++++++++++++-
 2 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/src/core/controllers/AuthenticationController.php b/src/core/controllers/AuthenticationController.php
index 6e3e1336217..3bdea6d7a90 100755
--- a/src/core/controllers/AuthenticationController.php
+++ b/src/core/controllers/AuthenticationController.php
@@ -313,10 +313,9 @@ class AuthenticationController
         $loggingMethod = CoreConfigModel::getLoggingMethod();
 
         if ($loggingMethod['id'] == 'cas') {
-            AuthenticationController::casDisconnection();
+            $res = AuthenticationController::casDisconnection();
         }
-
-        return $response->withStatus(204);
+        return $response->withJson(['logoutUrl' => $res['logoutUrl'], 'redirectUrl' => $res['redirectUrl']]);
     }
 
     private static function standardConnection(array $args)
@@ -461,9 +460,8 @@ class AuthenticationController
         }
         \phpCAS::setFixedServiceURL(UrlController::getCoreUrl() . 'dist/index.html');
         \phpCAS::setNoClearTicketsFromUrl();
-        \phpCAS::logout();
-
-        return true;
+        $logoutUrl = \phpCAS::getServerLogoutURL();
+        return ['logoutUrl' => $logoutUrl, 'redirectUrl' => UrlController::getCoreUrl() . 'dist/index.html'];
     }
 
     public function getRefreshedToken(Request $request, Response $response)
diff --git a/src/frontend/service/auth.service.ts b/src/frontend/service/auth.service.ts
index 6f55e47f48e..251fc73f244 100644
--- a/src/frontend/service/auth.service.ts
+++ b/src/frontend/service/auth.service.ts
@@ -5,6 +5,7 @@ import { LocalStorageService } from './local-storage.service';
 import { NotificationService } from './notification/notification.service';
 import { HeaderService } from './header.service';
 import { Observable, Subject } from 'rxjs';
+import { tap } from 'rxjs/operators';
 
 @Injectable({
     providedIn: 'root'
@@ -89,11 +90,28 @@ export class AuthService {
     }
 
     async logout(cleanUrl: boolean = true) {
+        if (['cas', 'keycloak'].indexOf(this.authMode) > -1) {
+            this.SsoLogout(cleanUrl);
+        } else {
+            this.redirectAfterLogout(cleanUrl);
+            await this.router.navigate(['/login']);
+        }
+    }
+
+    SsoLogout(cleanUrl: boolean = true) {
+        this.http.get('../rest/authenticate/logout').pipe(
+            tap(async (data: any) => {
+                this.redirectAfterLogout(cleanUrl);
+                window.location.href = data.logoutUrl + '?service=' + encodeURI(data.redirectUrl);
+            })
+        ).subscribe();
+    }
+
+    redirectAfterLogout(cleanUrl: boolean = true) {
         if (this.getToken() !== null && cleanUrl) {
             this.cleanUrl(JSON.parse(atob(this.getToken().split('.')[1])).user.id);
         }
         this.headerService.setUser();
-        await this.router.navigate(['/login']);
         this.clearTokens();
     }
 
-- 
GitLab