UNPKG

@jamarsto/kiunzi-micro-frontend-tools

Version:
535 lines (518 loc) 24.4 kB
import * as i0 from '@angular/core'; import { Component, NgModule, Injectable } from '@angular/core'; import * as i2 from '@angular/router'; import { RouterModule, NavigationEnd } from '@angular/router'; import { WebComponentWrapper } from '@angular-architects/module-federation-tools'; import { of, filter } from 'rxjs'; import { mergeMap, map } from 'rxjs/operators'; import * as i1 from 'angular-auth-oidc-client'; class NotFoundComponent { constructor() { } ngOnInit() { } } NotFoundComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.10", ngImport: i0, type: NotFoundComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); NotFoundComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "13.3.10", type: NotFoundComponent, selector: "ng-component", ngImport: i0, template: "<article>\r\n <header>\r\n <h2>Page Not Found</h2>\r\n </header>\r\n <main>\r\n <p><a href=\"/\">Home</a></p>\r\n </main>\r\n</article>\r\n", styles: [""] }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.10", ngImport: i0, type: NotFoundComponent, decorators: [{ type: Component, args: [{ template: "<article>\r\n <header>\r\n <h2>Page Not Found</h2>\r\n </header>\r\n <main>\r\n <p><a href=\"/\">Home</a></p>\r\n </main>\r\n</article>\r\n", styles: [""] }] }], ctorParameters: function () { return []; } }); class RootComponent { constructor() { } ngOnInit() { } } RootComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.10", ngImport: i0, type: RootComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); RootComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "13.3.10", type: RootComponent, selector: "ng-component", ngImport: i0, template: "<article class=\"d-flex flex-column overflow-hidden min-vh-minus-footer vh-minus-footer\">\r\n <main role=\"main\" class=\"container-fluid flex-grow-1 overflow-auto bg-light ps-0 pe-0\">\r\n <router-outlet></router-outlet>\r\n </main>\r\n</article>\r\n", styles: [""], directives: [{ type: i2.RouterOutlet, selector: "router-outlet", outputs: ["activate", "deactivate", "attach", "detach"], exportAs: ["outlet"] }] }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.10", ngImport: i0, type: RootComponent, decorators: [{ type: Component, args: [{ template: "<article class=\"d-flex flex-column overflow-hidden min-vh-minus-footer vh-minus-footer\">\r\n <main role=\"main\" class=\"container-fluid flex-grow-1 overflow-auto bg-light ps-0 pe-0\">\r\n <router-outlet></router-outlet>\r\n </main>\r\n</article>\r\n", styles: [""] }] }], ctorParameters: function () { return []; } }); class UnauthorisedComponent { constructor() { } ngOnInit() { } } UnauthorisedComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.10", ngImport: i0, type: UnauthorisedComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); UnauthorisedComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "13.3.10", type: UnauthorisedComponent, selector: "ng-component", ngImport: i0, template: "<article>\r\n <header>\r\n <h2>Not Authorised to Page</h2>\r\n </header>\r\n <main>\r\n <p><a href=\"/\">Home</a></p>\r\n </main>\r\n</article>\r\n", styles: [""] }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.10", ngImport: i0, type: UnauthorisedComponent, decorators: [{ type: Component, args: [{ template: "<article>\r\n <header>\r\n <h2>Not Authorised to Page</h2>\r\n </header>\r\n <main>\r\n <p><a href=\"/\">Home</a></p>\r\n </main>\r\n</article>\r\n", styles: [""] }] }], ctorParameters: function () { return []; } }); function moduleRoute(moduleRoute) { const moduleRouteComponent = typeof moduleRoute.component === 'undefined' ? RootComponent : moduleRoute.component; // provided a role without a guard. Hence don't try to guard if (typeof moduleRoute.guards === 'undefined' || moduleRoute.guards === null || moduleRoute.guards.length === 0) { return { matcher: modulePath(), component: moduleRouteComponent, children: moduleRoute.children }; } // provided guard(s) without role(s). Just do authorized check if (typeof moduleRoute.roles === 'undefined' || moduleRoute.roles === null || moduleRoute.roles.length === 0) { return { matcher: modulePath(), component: moduleRouteComponent, canLoad: moduleRoute.guards, canActivate: moduleRoute.guards, canActivateChild: moduleRoute.guards, children: moduleRoute.children }; } // provided guard(s) and role(s). Do full check return { matcher: modulePath(), component: moduleRouteComponent, canLoad: moduleRoute.guards, canActivate: moduleRoute.guards, canActivateChild: moduleRoute.guards, data: { role: moduleRoute.roles }, children: moduleRoute.children }; } function modulePath() { return (url) => { if (url.length > 0) { return ({ consumed: [url[0]] }); } return null; }; } function initialiseModuleRoutes(customRoutes) { const routes = []; customRoutes.headRoutes.forEach((route) => routes.push(route)); routes.push(moduleRoute(customRoutes.moduleRoute)); customRoutes.tailRoutes.forEach((route) => routes.push(route)); return routes; } function microFrontEndRoute(module) { // provided a role without a guard. Hence don't try to guard if (typeof module.guards === 'undefined' || module.guards === null || module.guards.length === 0 || (module.guards.length > 0 && typeof module.guards[0] === 'string')) { return { matcher: shellPath(module.prefix), component: WebComponentWrapper, data: { type: 'module', remoteEntry: '/mfe/' + module.name + '/remoteEntry.js', exposedModule: './RemoteAppModule', elementName: 'mfe-' + module.name, } }; } // provided guard(s) without role(s). Just do authorized check if (typeof module.roles === 'undefined' || module.roles === null || module.roles.length === 0) { return { matcher: shellPath(module.prefix), component: WebComponentWrapper, data: { type: 'module', remoteEntry: '/mfe/' + module.name + '/remoteEntry.js', exposedModule: './RemoteAppModule', elementName: 'mfe-' + module.name, }, canLoad: module.guards, canActivate: module.guards, canActivateChild: module.guards }; } // provided guard(s) and role(s). Do full check return { matcher: shellPath(module.prefix), component: WebComponentWrapper, data: { type: 'module', remoteEntry: '/mfe/' + module.name + '/remoteEntry.js', exposedModule: './RemoteAppModule', elementName: 'mfe-' + module.name, role: module.roles, }, canLoad: module.guards, canActivate: module.guards, canActivateChild: module.guards }; } function shellPath(shellPath) { return (url) => { if (url.length > 0 && url[0].path === shellPath) { return ({ consumed: url }); } return null; }; } function initialiseShellRoutes(customRoutes) { const routes = []; customRoutes.headRoutes.forEach((route) => routes.push(route)); customRoutes.moduleRoutes.forEach((module) => routes.push(microFrontEndRoute(module))); customRoutes.tailRoutes.forEach((route) => routes.push(route)); return routes; } class MicroFrontEndModule { } MicroFrontEndModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.10", ngImport: i0, type: MicroFrontEndModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); MicroFrontEndModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.3.10", ngImport: i0, type: MicroFrontEndModule, declarations: [NotFoundComponent, RootComponent, UnauthorisedComponent], imports: [RouterModule], exports: [NotFoundComponent, RootComponent, UnauthorisedComponent] }); MicroFrontEndModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.3.10", ngImport: i0, type: MicroFrontEndModule, providers: [], imports: [[ RouterModule ]] }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.10", ngImport: i0, type: MicroFrontEndModule, decorators: [{ type: NgModule, args: [{ declarations: [ NotFoundComponent, RootComponent, UnauthorisedComponent, ], imports: [ RouterModule ], providers: [], exports: [ NotFoundComponent, RootComponent, UnauthorisedComponent, ] }] }] }); class ActiveModulePath { constructor() { } get() { var segments = window.location.pathname.split('/'); if (segments.length > 1) { return segments[1]; } return '#'; } } ActiveModulePath.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.10", ngImport: i0, type: ActiveModulePath, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); ActiveModulePath.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.3.10", ngImport: i0, type: ActiveModulePath, providedIn: 'root' }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.10", ngImport: i0, type: ActiveModulePath, decorators: [{ type: Injectable, args: [{ providedIn: 'root' }] }], ctorParameters: function () { return []; } }); class AutoLoginAllRoutesWithRoleGuard { constructor(oidcSecurityService, router, autoLoginAllRoutesGuard) { this.oidcSecurityService = oidcSecurityService; this.router = router; this.autoLoginAllRoutesGuard = autoLoginAllRoutesGuard; } canActivate(next, state) { return this .autoLoginAllRoutesGuard .canActivate(next, state) .pipe(mergeMap(result => this.checkRoleForActivate(result, next))); } canActivateChild(next, state) { return this .autoLoginAllRoutesGuard .canActivateChild(next, state) .pipe(mergeMap(result => this.checkRoleForActivateChild(result, next))); } canLoad(route, url) { return this .autoLoginAllRoutesGuard .canLoad() .pipe(mergeMap(result => this.checkRoleForLoad(result, route, url))); } checkRoleForActivate(isAuthenticated, next) { if (typeof isAuthenticated !== 'boolean' || isAuthenticated !== true) { return of(isAuthenticated); } return this .isInRole(next.data['role']) .pipe(map(isInRole => { if (isInRole === true) { return true; } return this.router.parseUrl('/unauthorized'); })); } checkRoleForActivateChild(isAuthenticated, next) { if (typeof isAuthenticated !== 'boolean' || isAuthenticated !== true) { return of(isAuthenticated); } const data = next.parent != null ? next.parent.data : next.data; return this .isInRole(data['role']) .pipe(map(isInRole => { if (isInRole === true) { return true; } return this.router.parseUrl('/unauthorized'); })); } checkRoleForLoad(isAuthenticated, next, url) { if (typeof isAuthenticated !== 'boolean' || isAuthenticated !== true || typeof next.data === 'undefined') { return of(isAuthenticated); } return this .isInRole(next.data['role']) .pipe(map(isInRole => { if (isInRole === true) { return true; } return this.router.parseUrl('/unauthorized'); })); } isInRole(role) { if (typeof role === 'object') { return this.isInRoleArray(role); } return this.isInRoleString(role); } isInRoleArray(role) { if (role.length === 0) { return of(true); } return this .oidcSecurityService .userData$ .pipe(map(({ userData }) => { const roles = userData.roles; if (typeof roles !== 'undefined' && roles != null) { var matchedAll = true; role.forEach((strRole) => { if (!(typeof strRole === 'undefined' || strRole === null || strRole.trim() === '' || roles.indexOf(strRole) >= 0)) { matchedAll = false; } }); return matchedAll; } return false; })); } isInRoleString(role) { if (typeof role === 'undefined' || role === null || role.trim() === '') { return of(true); } return this .oidcSecurityService .userData$ .pipe(map(({ userData }) => { const roles = userData.roles; if (typeof roles !== 'undefined' && roles != null && roles.indexOf(role) >= 0) { return true; } return false; })); } } AutoLoginAllRoutesWithRoleGuard.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.10", ngImport: i0, type: AutoLoginAllRoutesWithRoleGuard, deps: [{ token: i1.OidcSecurityService }, { token: i2.Router }, { token: i1.AutoLoginAllRoutesGuard }], target: i0.ɵɵFactoryTarget.Injectable }); AutoLoginAllRoutesWithRoleGuard.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.3.10", ngImport: i0, type: AutoLoginAllRoutesWithRoleGuard, providedIn: 'root' }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.10", ngImport: i0, type: AutoLoginAllRoutesWithRoleGuard, decorators: [{ type: Injectable, args: [{ providedIn: 'root' }] }], ctorParameters: function () { return [{ type: i1.OidcSecurityService }, { type: i2.Router }, { type: i1.AutoLoginAllRoutesGuard }]; } }); class AutoLoginPartialRoutesWithRoleGuard { constructor(oidcSecurityService, router, autoLoginPartialRoutesGuard) { this.oidcSecurityService = oidcSecurityService; this.router = router; this.autoLoginPartialRoutesGuard = autoLoginPartialRoutesGuard; } canActivate(next, state) { return this .autoLoginPartialRoutesGuard .canActivate(next, state) .pipe(mergeMap(result => this.checkRoleForActivate(result, next))); } canActivateChild(next, state) { return this .autoLoginPartialRoutesGuard .canActivateChild(next, state) .pipe(mergeMap(result => this.checkRoleForActivateChild(result, next))); } canLoad(route, url) { return this .autoLoginPartialRoutesGuard .canLoad() .pipe(mergeMap(result => this.checkRoleForLoad(result, route, url))); } checkRoleForActivate(isAuthenticated, next) { if (typeof isAuthenticated !== 'boolean' || isAuthenticated !== true) { return of(isAuthenticated); } return this .isInRole(next.data['role']) .pipe(map(isInRole => { if (isInRole === true) { return true; } return this.router.parseUrl('/unauthorized'); })); } checkRoleForActivateChild(isAuthenticated, next) { if (typeof isAuthenticated !== 'boolean' || isAuthenticated !== true) { return of(isAuthenticated); } const data = next.parent != null ? next.parent.data : next.data; return this .isInRole(data['role']) .pipe(map(isInRole => { if (isInRole === true) { return true; } return this.router.parseUrl('/unauthorized'); })); } checkRoleForLoad(isAuthenticated, next, url) { if (typeof isAuthenticated !== 'boolean' || isAuthenticated !== true || typeof next.data === 'undefined') { return of(isAuthenticated); } return this .isInRole(next.data['role']) .pipe(map(isInRole => { if (isInRole === true) { return true; } return this.router.parseUrl('/unauthorized'); })); } isInRole(role) { if (typeof role === 'object') { return this.isInRoleArray(role); } return this.isInRoleString(role); } isInRoleArray(role) { if (role.length === 0) { return of(true); } return this .oidcSecurityService .userData$ .pipe(map(({ userData }) => { const roles = userData.roles; if (typeof roles !== 'undefined' && roles != null) { var matchedAll = true; role.forEach((strRole) => { if (!(typeof strRole === 'undefined' || strRole === null || strRole.trim() === '' || roles.indexOf(strRole) >= 0)) { matchedAll = false; } }); return matchedAll; } return false; })); } isInRoleString(role) { if (typeof role === 'undefined' || role === null || role.trim() === '') { return of(true); } return this .oidcSecurityService .userData$ .pipe(map(({ userData }) => { const roles = userData.roles; if (typeof roles !== 'undefined' && roles != null && roles.indexOf(role) >= 0) { return true; } return false; })); } } AutoLoginPartialRoutesWithRoleGuard.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.10", ngImport: i0, type: AutoLoginPartialRoutesWithRoleGuard, deps: [{ token: i1.OidcSecurityService }, { token: i2.Router }, { token: i1.AutoLoginPartialRoutesGuard }], target: i0.ɵɵFactoryTarget.Injectable }); AutoLoginPartialRoutesWithRoleGuard.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.3.10", ngImport: i0, type: AutoLoginPartialRoutesWithRoleGuard, providedIn: 'root' }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.10", ngImport: i0, type: AutoLoginPartialRoutesWithRoleGuard, decorators: [{ type: Injectable, args: [{ providedIn: 'root' }] }], ctorParameters: function () { return [{ type: i1.OidcSecurityService }, { type: i2.Router }, { type: i1.AutoLoginPartialRoutesGuard }]; } }); class SyncRouteModule { constructor(activeModulePath) { this.activeModulePath = activeModulePath; } sync(router, module) { router .events .pipe(filter((event) => event instanceof NavigationEnd)) .subscribe(() => this.dispatchEvent(module)); window.addEventListener('popstate', () => this.navigate(router)); window.addEventListener('shellNavigationEvent', (event) => this.updateRoute(router, event, module)); this.navigate(router); } details(module) { return { detail: module }; } dispatchEvent(module) { window.dispatchEvent(new CustomEvent('mfeNavigationEvent', this.details(module))); } navigate(router) { router.navigateByUrl(window.location.pathname); } updateRoute(router, event, module) { if (event.detail === module) { this.navigate(router); } } } SyncRouteModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.10", ngImport: i0, type: SyncRouteModule, deps: [{ token: ActiveModulePath }], target: i0.ɵɵFactoryTarget.Injectable }); SyncRouteModule.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.3.10", ngImport: i0, type: SyncRouteModule, providedIn: 'root' }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.10", ngImport: i0, type: SyncRouteModule, decorators: [{ type: Injectable, args: [{ providedIn: 'root' }] }], ctorParameters: function () { return [{ type: ActiveModulePath }]; } }); class SyncRouteShell { constructor(activeModulePath) { this.activeModulePath = activeModulePath; } sync(router, customRoutes) { const moduleByPath = this.getModuleByPath(customRoutes); const pathByModule = this.getPathByModule(customRoutes); router .events .pipe(filter((event) => event instanceof NavigationEnd)) .subscribe(() => this.dispatchEvent(moduleByPath)); window.addEventListener('popstate', () => this.navigate(router)); window.addEventListener('mfeNavigationEvent', (event) => this.updateRoute(router, event, pathByModule)); this.navigate(router); } details(moduleByPath) { return { detail: moduleByPath.get(this.activeModulePath.get()) }; } dispatchEvent(moduleByPath) { window.dispatchEvent(new CustomEvent('shellNavigationEvent', this.details(moduleByPath))); } getModuleByPath(customRoutes) { const moduleByPath = new Map(); customRoutes.moduleRoutes.forEach((item) => moduleByPath.set(item.prefix, item.name)); return moduleByPath; } getPathByModule(customRoutes) { const pathByModule = new Map(); customRoutes.moduleRoutes.forEach((item) => pathByModule.set(item.name, item.prefix)); return pathByModule; } navigate(router) { router.navigateByUrl(window.location.pathname); } updateRoute(router, event, pathByModule) { if (this.activeModulePath.get() === pathByModule.get(event.detail)) { this.navigate(router); } } } SyncRouteShell.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.10", ngImport: i0, type: SyncRouteShell, deps: [{ token: ActiveModulePath }], target: i0.ɵɵFactoryTarget.Injectable }); SyncRouteShell.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.3.10", ngImport: i0, type: SyncRouteShell, providedIn: 'root' }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.10", ngImport: i0, type: SyncRouteShell, decorators: [{ type: Injectable, args: [{ providedIn: 'root' }] }], ctorParameters: function () { return [{ type: ActiveModulePath }]; } }); /* * Public API Surface of lib-micro-front-end */ /** * Generated bundle index. Do not edit. */ export { ActiveModulePath, AutoLoginAllRoutesWithRoleGuard, AutoLoginPartialRoutesWithRoleGuard, MicroFrontEndModule, NotFoundComponent, RootComponent, SyncRouteModule, SyncRouteShell, UnauthorisedComponent, initialiseModuleRoutes, initialiseShellRoutes }; //# sourceMappingURL=jamarsto-kiunzi-micro-frontend-tools.mjs.map