From d8de72a7cbf05447f9e39d3723d64a49392cf095 Mon Sep 17 00:00:00 2001
From: Alex ORLUC <alex.orluc@maarch.org>
Date: Mon, 26 Oct 2020 19:43:27 +0100
Subject: [PATCH] FEAT #13695 TIME 1 fix sso logout redirect

---
 src/frontend/app/login/login.component.ts     |  2 +-
 src/frontend/service/app-light.guard.ts       |  2 +-
 src/frontend/service/app.guard.ts             |  2 +-
 .../service/auth-interceptor.service.ts       | 31 +++++++------------
 src/frontend/service/auth.service.ts          |  4 +--
 5 files changed, 16 insertions(+), 25 deletions(-)

diff --git a/src/frontend/app/login/login.component.ts b/src/frontend/app/login/login.component.ts
index fdd7eb719f3..d0b8dbbf58c 100644
--- a/src/frontend/app/login/login.component.ts
+++ b/src/frontend/app/login/login.component.ts
@@ -51,7 +51,7 @@ export class LoginComponent implements OnInit {
         });
 
         this.environment = environment;
-        if (this.authService.isAuth()) {
+        if (this.authService.getToken() !== null) {
             if (!this.functionsService.empty(this.authService.getUrl(JSON.parse(atob(this.authService.getToken().split('.')[1])).user.id))) {
                 this.router.navigate([this.authService.getUrl(JSON.parse(atob(this.authService.getToken().split('.')[1])).user.id)]);
             } else {
diff --git a/src/frontend/service/app-light.guard.ts b/src/frontend/service/app-light.guard.ts
index 2b2e7edef58..f2e066d243f 100644
--- a/src/frontend/service/app-light.guard.ts
+++ b/src/frontend/service/app-light.guard.ts
@@ -35,7 +35,7 @@ export class AppLightGuard implements CanActivate {
         this.headerService.resetSideNavSelection();
 
         return this.authService.getLoginInformations(state.url).pipe(
-            exhaustMap(() => this.authService.getToken() !== null ? this.authService.getCurrentUserInfo() : of(false)),
+            exhaustMap(() => this.authService.getToken() !== null && state.url !== '/login' ? this.authService.getCurrentUserInfo() : of(false)),
             map(() => true),
             catchError((err: any) => {
                 return of(true);
diff --git a/src/frontend/service/app.guard.ts b/src/frontend/service/app.guard.ts
index 891e4f7d68f..6c12f36e673 100644
--- a/src/frontend/service/app.guard.ts
+++ b/src/frontend/service/app.guard.ts
@@ -38,7 +38,7 @@ export class AppGuard implements CanActivate {
                     } else if (this.authService.getToken() === null) {
                         this.authService.setCachedUrl(state.url.replace(/^\//g, ''));
                         console.debug('Aucun token trouvé ! Redirection sur login ...');
-                        this.authService.logout(false);
+                        this.authService.logout(false, true);
                         return false;
                     }
                 }),
diff --git a/src/frontend/service/auth-interceptor.service.ts b/src/frontend/service/auth-interceptor.service.ts
index b172fe2762d..ef7f26009b4 100644
--- a/src/frontend/service/auth-interceptor.service.ts
+++ b/src/frontend/service/auth-interceptor.service.ts
@@ -12,33 +12,33 @@ export class AuthInterceptor implements HttpInterceptor {
     byPassToken: any[] = [
         {
             route: '../rest/prerequisites',
-            method : ['GET']
+            method: ['GET']
         },
         {
             route: '../rest/authenticate',
-            method : ['POST']
+            method: ['POST']
         },
         {
             route: '../rest/authenticate/token',
-            method : ['GET']
+            method: ['GET']
         },
         {
             route: '../rest/authenticationInformation',
-            method : ['GET']
+            method: ['GET']
         },
         {
             route: '../rest/passwordRules',
-            method : ['GET']
+            method: ['GET']
         },
         {
             route: '../rest/languages',
-            method : ['GET']
+            method: ['GET']
         }
     ];
     byPassHandleErrors: any[] = [
         {
             route: '/password',
-            method : ['PUT']
+            method: ['PUT']
         }
     ];
     constructor(
@@ -61,7 +61,7 @@ export class AuthInterceptor implements HttpInterceptor {
     }
 
     logout() {
-        this.authService.logout(false);
+        this.authService.logout(false, true);
         this.notificationService.error(this.translate.instant('lang.sessionExpired'));
     }
 
@@ -105,12 +105,7 @@ export class AuthInterceptor implements HttpInterceptor {
                                     catchError(err => {
                                         // Disconnect user if bad token process
                                         if (err.status === 401) {
-                                            if (this.router.url !== '/login' && this.router.url !== '/') {
-                                                this.logout();
-                                            } else {
-                                                return next.handle(request);
-                                            }
-
+                                            this.logout();
                                             return of(false);
                                         }
                                     })
@@ -120,12 +115,8 @@ export class AuthInterceptor implements HttpInterceptor {
                             catchError(err => {
                                 // Disconnect user if bad token process
                                 if (err.status === 401) {
-                                    console.debug('Refresh token failed !', request.url);
-                                    if (this.router.url !== '/login' && this.router.url !== '/') {
-                                        this.logout();
-                                    } else {
-                                        return next.handle(request);
-                                    }
+                                    console.debug('Refresh token failed !', request.url, this.router.url);
+                                    this.logout();
                                 }
                                 return of(false);
                             })
diff --git a/src/frontend/service/auth.service.ts b/src/frontend/service/auth.service.ts
index 8d7ac467eed..fd1e63a528f 100644
--- a/src/frontend/service/auth.service.ts
+++ b/src/frontend/service/auth.service.ts
@@ -102,8 +102,8 @@ export class AuthService {
         this.localStorage.remove('MaarchCourrierRefreshToken');
     }
 
-    async logout(cleanUrl: boolean = true) {
-        if (['cas', 'keycloak'].indexOf(this.authMode) > -1) {
+    async logout(cleanUrl: boolean = true, forcePageLogin: boolean = false) {
+        if (['cas', 'keycloak'].indexOf(this.authMode) > -1 && !forcePageLogin) {
             this.SsoLogout(cleanUrl);
         } else {
             this.redirectAfterLogout(cleanUrl);
-- 
GitLab