Skip to content

Commit

Permalink
[WIP]fix(login): fix public access with cas inpn authentification
Browse files Browse the repository at this point in the history
Several fixes:
- Fix direct public access with query param when INPN CAS authentification is configured
- [WIP] Fix flaky login and logout // ref #3353
  • Loading branch information
VincentCauchois committed Feb 4, 2025
1 parent c47651e commit a8b95d9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
14 changes: 7 additions & 7 deletions frontend/src/app/modules/login/login/login.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ export class LoginComponent implements OnInit {
}

ngOnInit() {
if (this.config.CAS_PUBLIC.CAS_AUTHENTIFICATION) {
// if token not here here, redirection to CAS login page
const url_redirection_cas = `${this.config.CAS_PUBLIC.CAS_URL_LOGIN}?service=${this.config.API_ENDPOINT}/gn_auth/login_cas`;
if (!this._authService.isLoggedIn()) {
document.location.href = url_redirection_cas;
}
}
// if (this.config.CAS_PUBLIC.CAS_AUTHENTIFICATION) {
// // if token not here here, redirection to CAS login page
// const url_redirection_cas = `${this.config.CAS_PUBLIC.CAS_URL_LOGIN}?service=${this.config.API_ENDPOINT}/gn_auth/login_cas`;
// if (!this._authService.isLoggedIn()) {
// document.location.href = url_redirection_cas;
// }
// }
}

async register(user) {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/app/routing/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const defaultRoutes: Routes = [
{
path: '',
component: NavHomeComponent,
canActivate: [UserCasGuard],
// canActivate: [UserCasGuard],
canActivateChild: [AuthGuard],
children: [
{
Expand Down
30 changes: 27 additions & 3 deletions frontend/src/app/routing/auth-guard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
RouterStateSnapshot,
} from '@angular/router';
import { Injectable, Injector } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import { AuthService } from '@geonature/components/auth/auth.service';
import { ModuleService } from '@geonature/services/module.service';
import { ConfigService } from '@geonature/services/config.service';
Expand All @@ -15,7 +17,10 @@ import { RoutingService } from './routing.service';
export class AuthGuard implements CanActivate, CanActivateChild {
constructor(
private _router: Router,
private _injector: Injector
private _injector: Injector,
private _httpclient: HttpClient,
private config: ConfigService,
private _authService: AuthService
) {}

async redirectAuth(route, state) {
Expand All @@ -38,12 +43,20 @@ export class AuthGuard implements CanActivate, CanActivateChild {
return false;
});
if (data) {
await authService.manageUser(data).toPromise();
authService.manageUser(data);
const modules = await moduleService.loadModules().toPromise();
routingService.loadRoutes(modules, route._routerState.url);
routingService.loadRoutes(modules, this.getNewURLWithoutAccessPublic(route));
} else {
return false;
}
} else if (configService.CAS_PUBLIC.CAS_AUTHENTIFICATION) {
const url_redirection_cas = `${configService.CAS_PUBLIC.CAS_URL_LOGIN}?service=${configService.API_ENDPOINT}/gn_auth/login_cas`;
document.location.href = url_redirection_cas;
let data = await this._httpclient
.get(`${this.config.API_ENDPOINT}/auth/get_current_user`)
.toPromise();
data = { ...data };
this._authService.manageUser(data);
} else {
this._router.navigate(['/login'], {
queryParams: { ...route.queryParams, ...{ route: state.url.split('?')[0] } },
Expand All @@ -64,4 +77,15 @@ export class AuthGuard implements CanActivate, CanActivateChild {
canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
return this.redirectAuth(route, state);
}

getNewURLWithoutAccessPublic(route) {
const queryParams = { ...route.queryParams };
delete queryParams.access;
const path = route.url[0] ? route.url[0].path : '';
const urlTree = this._router.createUrlTree([path], {
queryParams,
queryParamsHandling: 'merge',
});
return this._router.serializeUrl(urlTree);
}
}

0 comments on commit a8b95d9

Please sign in to comment.