Skip to content

Commit

Permalink
Updated AuthGuardEcm, AuthGuardBpm and AuthGuardService to be functio…
Browse files Browse the repository at this point in the history
…nal route guards
  • Loading branch information
swapnil-verma-gl committed Jun 26, 2024
1 parent ea57187 commit 4f4ae56
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 215 deletions.
132 changes: 0 additions & 132 deletions lib/core/src/lib/auth/guard/auth-guard-base.ts

This file was deleted.

47 changes: 19 additions & 28 deletions lib/core/src/lib/auth/guard/auth-guard-bpm.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,27 @@
* limitations under the License.
*/

import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Router, UrlTree } from '@angular/router';
import { AppConfigService } from '../../app-config/app-config.service';
import { inject } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router';
import { AuthenticationService } from '../services/authentication.service';
import { AuthGuardBase } from './auth-guard-base';
import { MatDialog } from '@angular/material/dialog';
import { StorageService } from '../../common/services/storage.service';
import { BasicAlfrescoAuthService } from '../basic-auth/basic-alfresco-auth.service';
import { OidcAuthenticationService } from '../oidc/oidc-authentication.service';
import { isLoginFragmentPresent, redirectSSOSuccessURL, redirectToUrl, withCredentials } from './auth-guard-functions';
import { Observable } from 'rxjs';

@Injectable({
providedIn: 'root'
})
export class AuthGuardBpm extends AuthGuardBase {
constructor(
authenticationService: AuthenticationService,
basicAlfrescoAuthService: BasicAlfrescoAuthService,
oidcAuthenticationService: OidcAuthenticationService,
router: Router,
appConfigService: AppConfigService,
dialog: MatDialog,
storageService: StorageService
) {
super(authenticationService, basicAlfrescoAuthService, oidcAuthenticationService, router, appConfigService, dialog, storageService);
const authenticationService = inject(AuthenticationService);

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

async checkLogin(_: ActivatedRouteSnapshot, redirectUrl: string): Promise<boolean | UrlTree> {
if (this.authenticationService.isBpmLoggedIn() || this.withCredentials) {
return true;
}
return this.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);
};
48 changes: 19 additions & 29 deletions lib/core/src/lib/auth/guard/auth-guard-ecm.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,27 @@
* limitations under the License.
*/

import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Router, UrlTree } from '@angular/router';
import { inject } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router';
import { AuthenticationService } from '../services/authentication.service';
import { AppConfigService } from '../../app-config/app-config.service';
import { AuthGuardBase } from './auth-guard-base';
import { MatDialog } from '@angular/material/dialog';
import { StorageService } from '../../common/services/storage.service';
import { BasicAlfrescoAuthService } from '../basic-auth/basic-alfresco-auth.service';
import { OidcAuthenticationService } from '../oidc/oidc-authentication.service';
import { isLoginFragmentPresent, redirectSSOSuccessURL, redirectToUrl, withCredentials } from './auth-guard-functions';
import { Observable } from 'rxjs';

@Injectable({
providedIn: 'root'
})
export class AuthGuardEcm extends AuthGuardBase {
constructor(
authenticationService: AuthenticationService,
basicAlfrescoAuthService: BasicAlfrescoAuthService,
oidcAuthenticationService: OidcAuthenticationService,
router: Router,
appConfigService: AppConfigService,
dialog: MatDialog,
storageService: StorageService
) {
super(authenticationService, basicAlfrescoAuthService, oidcAuthenticationService, router, appConfigService, dialog, storageService);
}
const authenticationService = inject(AuthenticationService);

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

return this.redirectToUrl(redirectUrl);
export const AuthGuardEcm = (
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);
};
82 changes: 82 additions & 0 deletions lib/core/src/lib/auth/guard/auth-guard-functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*!
* @license
* Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Router, UrlTree } from '@angular/router';
import { inject } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { AuthenticationService } from '../services/authentication.service';
import { AppConfigService, AppConfigValues } from '../../app-config/app-config.service';
import { OauthConfigModel } from '../models/oauth-config.model';
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 => appConfigService?.get(AppConfigValues.OAUTHCONFIG, null);

const getLoginRoute = (): string => appConfigService.get<string>(AppConfigValues.LOGIN_ROUTE, 'login');

const getProvider = (): string => appConfigService.get<string>(AppConfigValues.PROVIDERS, 'ALL');

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

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

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

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

if (!authenticationService.isOauth()) {
basicAlfrescoAuthService.setRedirect({
provider: getProvider(),
url
});

urlToRedirect = `${urlToRedirect}?redirectUrl=${url}`;
return navigate(urlToRedirect);
} else if (getOauthConfig().silentLogin && !oidcAuthenticationService.isPublicUrl()) {
if (!oidcAuthenticationService.hasValidIdToken() || !oidcAuthenticationService.hasValidAccessToken()) {
oidcAuthenticationService.ssoLogin(url);
}
} else {
return navigate(urlToRedirect);
}

return false;
};

export const redirectSSOSuccessURL = async (): Promise<boolean | UrlTree> => {
const redirectFragment = storageService.getItem('loginFragment');
if (redirectFragment && getLoginRoute() !== redirectFragment) {
await navigate(redirectFragment);
storageService.removeItem('loginFragment');
return false;
}
return true;
};
Loading

0 comments on commit 4f4ae56

Please sign in to comment.