Skip to content

Commit

Permalink
Refactored AuthGuards to remove unneeded parameters. Updated usages t…
Browse files Browse the repository at this point in the history
…o inject() to be within InjectionContext
  • Loading branch information
swapnil-verma-gl committed Jul 2, 2024
1 parent 1c8393b commit 5fe67f3
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 71 deletions.
21 changes: 7 additions & 14 deletions lib/core/src/lib/auth/guard/auth-guard-bpm.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,19 @@
*/

import { inject } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router';
import { RouterStateSnapshot, UrlTree } from '@angular/router';
import { AuthenticationService } from '../services/authentication.service';
import { isLoginFragmentPresent, redirectSSOSuccessURL, redirectToUrl, withCredentials } from './auth-guard-functions';
import { Observable } from 'rxjs';

const authenticationService = inject(AuthenticationService);

const checkLogin = async (_: ActivatedRouteSnapshot, redirectUrl: string): Promise<boolean | UrlTree> => {
export const AuthGuardBpm = (state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree => {
const authenticationService = inject(AuthenticationService);
if (authenticationService.isLoggedIn() && authenticationService.isOauth() && isLoginFragmentPresent()) {
return redirectSSOSuccessURL();
}
if (authenticationService.isBpmLoggedIn() || withCredentials) {
return true;
}
return redirectToUrl(redirectUrl);
};

export const AuthGuardBpm = (
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree => {
if (authenticationService.isLoggedIn() && authenticationService.isOauth() && isLoginFragmentPresent()) {
return redirectSSOSuccessURL();
}
return checkLogin(route, state.url);
return redirectToUrl(state.url);
};
22 changes: 7 additions & 15 deletions lib/core/src/lib/auth/guard/auth-guard-ecm.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,18 @@
*/

import { inject } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router';
import { RouterStateSnapshot, UrlTree } from '@angular/router';
import { AuthenticationService } from '../services/authentication.service';
import { isLoginFragmentPresent, redirectSSOSuccessURL, redirectToUrl, withCredentials } from './auth-guard-functions';
import { Observable } from 'rxjs';

const authenticationService = inject(AuthenticationService);

const checkLogin = async (_: ActivatedRouteSnapshot, redirectUrl: string): Promise<boolean | UrlTree> => {
if (authenticationService.isEcmLoggedIn() || withCredentials()) {
return true;
}
return redirectToUrl(redirectUrl);
};

export const AuthGuardEcm = (
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree => {
export const AuthGuardEcm = (state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree => {
const authenticationService = inject(AuthenticationService);
if (authenticationService.isLoggedIn() && authenticationService.isOauth() && isLoginFragmentPresent()) {
return redirectSSOSuccessURL();
}
return checkLogin(route, state.url);
if (authenticationService.isEcmLoggedIn() || withCredentials()) {
return true;
}
return redirectToUrl(state.url);
};
27 changes: 11 additions & 16 deletions lib/core/src/lib/auth/guard/auth-guard-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,29 @@ import { StorageService } from '../../common/services/storage.service';
import { BasicAlfrescoAuthService } from '../basic-auth/basic-alfresco-auth.service';
import { OidcAuthenticationService } from '../oidc/oidc-authentication.service';

const authenticationService = inject(AuthenticationService);
const basicAlfrescoAuthService = inject(BasicAlfrescoAuthService);
const oidcAuthenticationService = inject(OidcAuthenticationService);
const router = inject(Router);
const appConfigService = inject(AppConfigService);
const dialog = inject(MatDialog);
const storageService = inject(StorageService);
const getOauthConfig = (): OauthConfigModel => inject(AppConfigService)?.get(AppConfigValues.OAUTHCONFIG, null);

const getOauthConfig = (): OauthConfigModel => appConfigService?.get(AppConfigValues.OAUTHCONFIG, null);
const getLoginRoute = (): string => inject(AppConfigService).get<string>(AppConfigValues.LOGIN_ROUTE, 'login');

const getLoginRoute = (): string => appConfigService.get<string>(AppConfigValues.LOGIN_ROUTE, 'login');
const getProvider = (): string => inject(AppConfigService).get<string>(AppConfigValues.PROVIDERS, 'ALL');

const getProvider = (): string => appConfigService.get<string>(AppConfigValues.PROVIDERS, 'ALL');
export const isLoginFragmentPresent = (): boolean => !!inject(StorageService).getItem('loginFragment');

export const isLoginFragmentPresent = (): boolean => !!storageService.getItem('loginFragment');

export const withCredentials = (): boolean => appConfigService.get<boolean>('auth.withCredentials', false);
export const withCredentials = (): boolean => inject(AppConfigService).get<boolean>('auth.withCredentials', false);

export const navigate = async (url: string): Promise<boolean> => {
dialog.closeAll();
inject(MatDialog).closeAll();
const router = inject(Router);
await router.navigateByUrl(router.parseUrl(url));
return false;
};

export const redirectToUrl = async (url: string): Promise<boolean | UrlTree> => {
let urlToRedirect = `/${getLoginRoute()}`;
const oidcAuthenticationService = inject(OidcAuthenticationService);

if (!authenticationService.isOauth()) {
basicAlfrescoAuthService.setRedirect({
if (!inject(AuthenticationService).isOauth()) {
inject(BasicAlfrescoAuthService).setRedirect({
provider: getProvider(),
url
});
Expand All @@ -72,6 +66,7 @@ export const redirectToUrl = async (url: string): Promise<boolean | UrlTree> =>
};

export const redirectSSOSuccessURL = async (): Promise<boolean | UrlTree> => {
const storageService = inject(StorageService);
const redirectFragment = storageService.getItem('loginFragment');
if (redirectFragment && getLoginRoute() !== redirectFragment) {
await navigate(redirectFragment);
Expand Down
9 changes: 4 additions & 5 deletions lib/core/src/lib/auth/guard/auth-guard-sso-role.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ import { ActivatedRouteSnapshot, Router } from '@angular/router';
import { MatDialog } from '@angular/material/dialog';
import { UserAccessService } from '../services/user-access.service';

const userAccessService = inject(UserAccessService);
const router = inject(Router);
const dialog = inject(MatDialog);
let userAccessService: UserAccessService;

/**
* Function to validate if the current user has/does not have the provided set of roles
Expand All @@ -48,6 +46,7 @@ function hasRoles(roles: string[] = []): boolean {
}

export const AuthGuardSsoRoleService = (route: ActivatedRouteSnapshot): boolean => {
userAccessService = inject(UserAccessService);
userAccessService.fetchUserAccess();
let hasRealmRole = false;
let hasClientRole = true;
Expand All @@ -71,11 +70,11 @@ export const AuthGuardSsoRoleService = (route: ActivatedRouteSnapshot): boolean
const hasRole = hasRealmRole && hasClientRole;

if (!hasRole && route?.data && route.data['redirectUrl']) {
router.navigate(['/' + route.data['redirectUrl']]);
inject(Router).navigate(['/' + route.data['redirectUrl']]);
}

if (!hasRole) {
dialog.closeAll();
inject(MatDialog).closeAll();
}

return hasRole;
Expand Down
27 changes: 8 additions & 19 deletions lib/core/src/lib/auth/guard/auth-guard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,19 @@
*/

import { inject, Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Router, RouterStateSnapshot, UrlTree } from '@angular/router';
import { Router, RouterStateSnapshot, UrlTree } from '@angular/router';
import { AuthenticationService } from '../services/authentication.service';
import { isLoginFragmentPresent, navigate, redirectSSOSuccessURL, redirectToUrl, withCredentials } from './auth-guard-functions';
import { JwtHelperService } from '../services/jwt-helper.service';
import { Observable } from 'rxjs';

const authenticationService = inject(AuthenticationService);

@Injectable({
providedIn: 'root'
})
class TicketChangeRedirectService {
ticketChangeBind: any;

constructor(
private jwtHelperService: JwtHelperService,
private router: Router
) {
constructor(private jwtHelperService: JwtHelperService, private router: Router) {
this.ticketChangeBind = this.ticketChange.bind(this);
window.addEventListener('storage', this.ticketChangeBind);
}
Expand Down Expand Up @@ -65,20 +60,14 @@ class TicketChangeRedirectService {
}
}

const checkLogin = async (_: ActivatedRouteSnapshot, redirectUrl: string): Promise<boolean | UrlTree> => {
if (authenticationService.isLoggedIn() || withCredentials()) {
return true;
}
return redirectToUrl(redirectUrl);
};

export const AuthGuard = (
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree => {
export const AuthGuard = (state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree => {
inject(TicketChangeRedirectService);
const authenticationService = inject(AuthenticationService);
if (authenticationService.isLoggedIn() && authenticationService.isOauth() && isLoginFragmentPresent()) {
return redirectSSOSuccessURL();
}
return checkLogin(route, state.url);
if (authenticationService.isLoggedIn() || withCredentials()) {
return true;
}
return redirectToUrl(state.url);
};
5 changes: 3 additions & 2 deletions lib/core/src/lib/auth/oidc/oidc-auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import { Observable } from 'rxjs';
import { AuthService } from './auth.service';

const ROUTE_DEFAULT = '/';
const auth = inject(AuthService);
const router = inject(Router);

export const OidcAuthGuard = (): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree => {
const auth = inject(AuthService);
const router = inject(Router);

if (auth.authenticated) {
return true;
}
Expand Down

0 comments on commit 5fe67f3

Please sign in to comment.