FarmMapsLib/projects/common/src/fm/services/auth-guard.service.ts

69 lines
2.6 KiB
TypeScript
Raw Normal View History

2020-01-31 10:24:45 +00:00
import { Injectable } from '@angular/core';
import {
CanActivate, Router, CanLoad, Route, CanActivateChild ,
ActivatedRouteSnapshot,
RouterStateSnapshot
} from '@angular/router';
2020-05-11 18:39:00 +00:00
2020-01-31 10:24:45 +00:00
import { Store } from '@ngrx/store';
import { OAuthService } from 'angular-oauth2-oidc';
import * as appCommonReducer from '../reducers/app-common.reducer'
import * as appCommonActions from '../actions/app-common.actions';
@Injectable({
providedIn: 'root',
})
export class AuthGuard implements CanActivate, CanLoad, CanActivateChild {
constructor(private oauthService: OAuthService, private router: Router, private store: Store<appCommonReducer.State> ) { }
2020-05-11 18:39:00 +00:00
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
2023-03-06 09:29:43 +00:00
console.debug("AuthGuard->canActivate", route, state);
2023-03-06 13:04:14 +00:00
const url: string = state.url;
2020-01-31 10:24:45 +00:00
return this.checkLogin(url, route);
2020-01-31 10:24:45 +00:00
}
2020-05-11 18:39:00 +00:00
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
2023-03-06 09:29:43 +00:00
console.debug("AuthGuard->canActivateChild", childRoute, state);
2023-03-06 13:04:14 +00:00
const url: string = state.url;
2020-01-31 10:24:45 +00:00
return this.checkLogin(url, childRoute);
2020-01-31 10:24:45 +00:00
}
2020-05-11 18:39:00 +00:00
canLoad(route: Route): Promise<boolean> {
2023-03-06 09:29:43 +00:00
console.debug("AuthGuard->canLoad", route);
return this.checkLogin(route.path, null);
2020-01-31 10:24:45 +00:00
}
checkLogin(url: string, route: ActivatedRouteSnapshot): Promise<boolean> {
2023-03-06 09:29:43 +00:00
console.debug("AuthGuard->checkLogin", url, route);
2020-05-11 18:39:00 +00:00
return new Promise<boolean>((resolve) => {
if (!this.oauthService.hasValidAccessToken()) {
2020-05-11 18:50:38 +00:00
console.debug("No valid token");
2020-10-30 07:22:58 +00:00
this.oauthService.initCodeFlow(url);
resolve(false);
} else {
const requiredRoleClaim = route.data.role;
if (!requiredRoleClaim) { resolve(true); }
const ownedClaims = this.oauthService.getIdentityClaims();
2023-03-03 07:40:12 +00:00
if (!ownedClaims) { console.debug("No owned claims"); resolve(false); }
const ownedRoleClaims: string[] = ownedClaims['role'];
2023-03-03 07:40:12 +00:00
if (!ownedRoleClaims) { console.debug("No owned role claims"); resolve(false); }
if (Array.isArray(ownedRoleClaims)) {
2023-03-03 07:40:12 +00:00
if (ownedRoleClaims.findIndex(r => r === requiredRoleClaim) <= -1) { console.debug("No required role claim", ownedRoleClaims, requiredRoleClaim); resolve(false); }
}
else {
2023-03-03 07:40:12 +00:00
if (ownedRoleClaims !== requiredRoleClaim) { console.debug("No required role claim", ownedRoleClaims, requiredRoleClaim); resolve(false); }
}
2023-03-03 07:40:12 +00:00
console.debug("Has required role claim", requiredRoleClaim);
2020-05-11 18:57:45 +00:00
resolve(true);
2020-05-11 18:39:00 +00:00
}
});
2020-01-31 10:24:45 +00:00
}
}