Skip to content

Commit

Permalink
fix(auth): fix authentication via CAS INPN login
Browse files Browse the repository at this point in the history
Fix front `isAuthenticated`, redirection CAS, local storage 'expires_at' setup, backend login_user.

Signed-off-by: VincentCauchois <[email protected]>
  • Loading branch information
VincentCauchois committed Jan 19, 2024
1 parent 0cabf07 commit 5fcbf6e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
9 changes: 9 additions & 0 deletions backend/geonature/core/auth/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ def loginCas():
"id_organisme": organism_id,
}
response.set_cookie("current_user", str(current_user), expires=cookie_exp)

# Log the user in
user = db.session.execute(
sa.select(models.User)
.where(models.User.identifiant == current_user["user_login"])
.where(models.User.filter_by_app())
).scalar_one()
login_user(user)

return response
else:
log.info("Erreur d'authentification lié au CAS, voir log du CAS")
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/app/components/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export class AuthService {
}

isAuthenticated(): boolean {
return this._cookie.get('token') !== null;
return this._cookie.check('token') && this._cookie.get('token') !== null;
}

handleLoginError() {
Expand Down
7 changes: 6 additions & 1 deletion frontend/src/app/modules/login/login/login.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ConfigService } from '@geonature/services/config.service';
import { ModuleService } from '@geonature/services/module.service';
import { ActivatedRoute, Router } from '@angular/router';
import { RoutingService } from '@geonature/routing/routing.service';
import * as moment from 'moment';

@Component({
selector: 'pnx-login',
Expand Down Expand Up @@ -47,7 +48,11 @@ export class LoginComponent implements OnInit {
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`;
document.location.href = url_redirection_cas;
if (!this._authService.isAuthenticated()) {
// TODO: set the local storage item 'expires_at' in the API route "gn_auth/login_cas"
localStorage.setItem('expires_at', moment().add(1, 'days').toISOString());
document.location.href = url_redirection_cas;
}
}
}

Expand Down

0 comments on commit 5fcbf6e

Please sign in to comment.