@verisoft/security-core 18.7.0 → 19.0.0-rc001
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -56
- package/fesm2022/verisoft-security-core.mjs +380 -0
- package/fesm2022/verisoft-security-core.mjs.map +1 -0
- package/index.d.ts +1 -0
- package/lib/directives/has-permission.directive.d.ts +17 -0
- package/lib/directives/has-role.directive.d.ts +17 -0
- package/{src/lib/directives/index.ts → lib/directives/index.d.ts} +2 -2
- package/lib/guards/auth.guard.d.ts +13 -0
- package/{src/lib/guards/index.ts → lib/guards/index.d.ts} +1 -1
- package/{src/lib/index.ts → lib/index.d.ts} +6 -6
- package/{src/lib/models/authenticated-user.model.ts → lib/models/authenticated-user.model.d.ts} +8 -8
- package/{src/lib/models/config.model.ts → lib/models/config.model.d.ts} +8 -9
- package/lib/models/functions.d.ts +5 -0
- package/{src/lib/models/index.ts → lib/models/index.d.ts} +3 -3
- package/lib/provider.d.ts +14 -0
- package/lib/services/auth-context.service.d.ts +15 -0
- package/{src/lib/services/index.ts → lib/services/index.d.ts} +7 -7
- package/lib/services/local-storage-token-provider.d.ts +11 -0
- package/lib/services/login.service.d.ts +10 -0
- package/lib/services/logout.service.d.ts +8 -0
- package/lib/services/security-initializer.d.ts +5 -0
- package/lib/services/session-token-provider.d.ts +9 -0
- package/{src/lib/services/token-provider.ts → lib/services/token-provider.d.ts} +4 -5
- package/lib/state/actions.d.ts +6 -0
- package/lib/state/feature.d.ts +7 -0
- package/{src/lib/state/index.ts → lib/state/index.d.ts} +4 -4
- package/lib/state/reducers.d.ts +1 -0
- package/lib/state/selectors.d.ts +2 -0
- package/lib/state/state.d.ts +5 -0
- package/package.json +28 -11
- package/.eslintrc.json +0 -48
- package/jest.config.ts +0 -21
- package/ng-package.json +0 -7
- package/project.json +0 -36
- package/src/index.ts +0 -1
- package/src/lib/directives/has-permission.directive.ts +0 -54
- package/src/lib/directives/has-role.directive.ts +0 -54
- package/src/lib/guards/auth.guard.ts +0 -55
- package/src/lib/models/functions.spec.ts +0 -159
- package/src/lib/models/functions.ts +0 -103
- package/src/lib/provider.ts +0 -58
- package/src/lib/services/auth-context.service.ts +0 -38
- package/src/lib/services/local-storage-token-provider.ts +0 -23
- package/src/lib/services/local-token-provider.ts +0 -15
- package/src/lib/services/login.service.ts +0 -23
- package/src/lib/services/logout.service.ts +0 -15
- package/src/lib/services/security-initializer.ts +0 -26
- package/src/lib/services/session-token-provider.ts +0 -15
- package/src/lib/state/actions.ts +0 -7
- package/src/lib/state/feature.ts +0 -10
- package/src/lib/state/reducers.ts +0 -11
- package/src/lib/state/selectors.ts +0 -9
- package/src/lib/state/state.ts +0 -9
- package/src/test-setup.ts +0 -8
- package/tsconfig.json +0 -28
- package/tsconfig.lib.json +0 -17
- package/tsconfig.lib.prod.json +0 -9
- package/tsconfig.spec.json +0 -16
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Store } from '@ngrx/store';
|
|
2
|
+
import { Observable } from 'rxjs';
|
|
3
|
+
import { AuthenticatedUser } from '../models';
|
|
4
|
+
import * as i0 from "@angular/core";
|
|
5
|
+
export declare class AuthContextService {
|
|
6
|
+
private store;
|
|
7
|
+
user$: Observable<AuthenticatedUser | undefined>;
|
|
8
|
+
isAuthenticated$: Observable<boolean>;
|
|
9
|
+
constructor(store: Store);
|
|
10
|
+
setUser(user: AuthenticatedUser | undefined): void;
|
|
11
|
+
hasRequiredPermission(requiredPermissions: string | string[]): Observable<boolean>;
|
|
12
|
+
hasRequiredRole(requiredPermissions: string | string[]): Observable<boolean>;
|
|
13
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<AuthContextService, never>;
|
|
14
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<AuthContextService>;
|
|
15
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export * from './auth-context.service';
|
|
2
|
-
export * from './local-storage-token-provider';
|
|
3
|
-
export * from './security-initializer';
|
|
4
|
-
export * from './session-token-provider';
|
|
5
|
-
export * from './token-provider';
|
|
6
|
-
export * from './login.service';
|
|
7
|
-
export * from './logout.service';
|
|
1
|
+
export * from './auth-context.service';
|
|
2
|
+
export * from './local-storage-token-provider';
|
|
3
|
+
export * from './security-initializer';
|
|
4
|
+
export * from './session-token-provider';
|
|
5
|
+
export * from './token-provider';
|
|
6
|
+
export * from './login.service';
|
|
7
|
+
export * from './logout.service';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
import { TokenProvider } from './token-provider';
|
|
3
|
+
import * as i0 from "@angular/core";
|
|
4
|
+
export declare class LocalStorageTokenProvider implements TokenProvider {
|
|
5
|
+
private config;
|
|
6
|
+
getToken(): Observable<string | undefined>;
|
|
7
|
+
setToken(token: string): void;
|
|
8
|
+
removeToken(): void;
|
|
9
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<LocalStorageTokenProvider, never>;
|
|
10
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<LocalStorageTokenProvider>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import * as i0 from "@angular/core";
|
|
2
|
+
export declare class LoginService {
|
|
3
|
+
private config;
|
|
4
|
+
private tokenProvider;
|
|
5
|
+
private authService;
|
|
6
|
+
private router;
|
|
7
|
+
login(token?: string): void;
|
|
8
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<LoginService, never>;
|
|
9
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<LoginService>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import * as i0 from "@angular/core";
|
|
2
|
+
export declare class LogoutService {
|
|
3
|
+
private readonly tokenProvider;
|
|
4
|
+
private readonly authService;
|
|
5
|
+
logout(): void;
|
|
6
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<LogoutService, never>;
|
|
7
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<LogoutService>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { Router } from '@angular/router';
|
|
2
|
+
import { SecurityConfig } from '../models';
|
|
3
|
+
import { AuthContextService } from './auth-context.service';
|
|
4
|
+
import { TokenProvider } from './token-provider';
|
|
5
|
+
export declare function securityInitializerFactory(tokenProvider: TokenProvider, authService: AuthContextService, config: SecurityConfig, router: Router): () => Promise<unknown>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
import { TokenProvider } from './token-provider';
|
|
3
|
+
import * as i0 from "@angular/core";
|
|
4
|
+
export declare class SessionStorageTokenProvider implements TokenProvider {
|
|
5
|
+
private config;
|
|
6
|
+
getToken(): Observable<string | undefined>;
|
|
7
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<SessionStorageTokenProvider, never>;
|
|
8
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<SessionStorageTokenProvider>;
|
|
9
|
+
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { Observable } from "rxjs";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
1
|
+
import { Observable } from "rxjs";
|
|
2
|
+
export interface TokenProvider {
|
|
3
|
+
getToken(): Observable<string | undefined>;
|
|
4
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { AuthenticatedUser } from '../models';
|
|
2
|
+
export declare const setUser: import("@ngrx/store").ActionCreator<"[Auth] Set User", (props: {
|
|
3
|
+
user: AuthenticatedUser | undefined;
|
|
4
|
+
}) => {
|
|
5
|
+
user: AuthenticatedUser | undefined;
|
|
6
|
+
} & import("@ngrx/store").Action<"[Auth] Set User">>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const SECURITY_FEATURE_NAME = "SECURITY";
|
|
2
|
+
export declare const SecurityFeature: {
|
|
3
|
+
name: "SECURITY";
|
|
4
|
+
reducer: import("@ngrx/store").ActionReducer<import("@verisoft/security-core").AuthState, import("@ngrx/store").Action<string>>;
|
|
5
|
+
selectSECURITYState: import("@ngrx/store").MemoizedSelector<Record<string, any>, import("@verisoft/security-core").AuthState, (featureState: import("@verisoft/security-core").AuthState) => import("@verisoft/security-core").AuthState>;
|
|
6
|
+
selectUser: import("@ngrx/store").MemoizedSelector<Record<string, any>, import("@verisoft/security-core").AuthenticatedUser | undefined, (featureState: import("@verisoft/security-core").AuthState) => import("@verisoft/security-core").AuthenticatedUser | undefined>;
|
|
7
|
+
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export * from './actions';
|
|
2
|
-
export * from './state';
|
|
3
|
-
export * from './reducers';
|
|
4
|
-
export * from './selectors';
|
|
1
|
+
export * from './actions';
|
|
2
|
+
export * from './state';
|
|
3
|
+
export * from './reducers';
|
|
4
|
+
export * from './selectors';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const authReducer: import("@ngrx/store").ActionReducer<import("./state").AuthState, import("@ngrx/store").Action<string>>;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export declare const selectIsAuthenticated: import("@ngrx/store").MemoizedSelector<Record<string, any>, boolean, (s1: import("@verisoft/security-core").AuthenticatedUser | undefined) => boolean>;
|
|
2
|
+
export declare const selectUser: import("@ngrx/store").MemoizedSelector<Record<string, any>, import("@verisoft/security-core").AuthenticatedUser | undefined, (featureState: import("@verisoft/security-core").AuthState) => import("@verisoft/security-core").AuthenticatedUser | undefined>;
|
package/package.json
CHANGED
|
@@ -1,11 +1,28 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@verisoft/security-core",
|
|
3
|
-
"version": "
|
|
4
|
-
"peerDependencies": {
|
|
5
|
-
"@angular/core": "
|
|
6
|
-
"
|
|
7
|
-
"@
|
|
8
|
-
"
|
|
9
|
-
},
|
|
10
|
-
"sideEffects": false
|
|
11
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@verisoft/security-core",
|
|
3
|
+
"version": "19.0.0-rc001",
|
|
4
|
+
"peerDependencies": {
|
|
5
|
+
"@angular/core": "19.2.9",
|
|
6
|
+
"@angular/router": "19.2.9",
|
|
7
|
+
"@ngrx/store": "19.2.1",
|
|
8
|
+
"rxjs": "~7.8.0"
|
|
9
|
+
},
|
|
10
|
+
"sideEffects": false,
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
14
|
+
"module": "fesm2022/verisoft-security-core.mjs",
|
|
15
|
+
"typings": "index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
"./package.json": {
|
|
18
|
+
"default": "./package.json"
|
|
19
|
+
},
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./index.d.ts",
|
|
22
|
+
"default": "./fesm2022/verisoft-security-core.mjs"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"tslib": "^2.3.0"
|
|
27
|
+
}
|
|
28
|
+
}
|
package/.eslintrc.json
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": ["../../../../.eslintrc.base.json"],
|
|
3
|
-
"ignorePatterns": ["!**/*"],
|
|
4
|
-
"overrides": [
|
|
5
|
-
{
|
|
6
|
-
"files": ["*.ts"],
|
|
7
|
-
"extends": [
|
|
8
|
-
"plugin:@nx/angular",
|
|
9
|
-
"plugin:@angular-eslint/template/process-inline-templates"
|
|
10
|
-
],
|
|
11
|
-
"rules": {
|
|
12
|
-
"@angular-eslint/directive-selector": [
|
|
13
|
-
"error",
|
|
14
|
-
{
|
|
15
|
-
"type": "attribute",
|
|
16
|
-
"prefix": "",
|
|
17
|
-
"style": "camelCase"
|
|
18
|
-
}
|
|
19
|
-
],
|
|
20
|
-
"@angular-eslint/component-selector": [
|
|
21
|
-
"error",
|
|
22
|
-
{
|
|
23
|
-
"type": "element",
|
|
24
|
-
"prefix": "v-sec",
|
|
25
|
-
"style": "kebab-case"
|
|
26
|
-
}
|
|
27
|
-
]
|
|
28
|
-
}
|
|
29
|
-
},
|
|
30
|
-
{
|
|
31
|
-
"files": ["*.html"],
|
|
32
|
-
"extends": ["plugin:@nx/angular-template"],
|
|
33
|
-
"rules": {}
|
|
34
|
-
},
|
|
35
|
-
{
|
|
36
|
-
"files": ["*.json"],
|
|
37
|
-
"parser": "jsonc-eslint-parser",
|
|
38
|
-
"rules": {
|
|
39
|
-
"@nx/dependency-checks": [
|
|
40
|
-
"error",
|
|
41
|
-
{
|
|
42
|
-
"ignoredFiles": ["{projectRoot}/eslint.config.{js,cjs,mjs}"]
|
|
43
|
-
}
|
|
44
|
-
]
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
]
|
|
48
|
-
}
|
package/jest.config.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export default {
|
|
2
|
-
displayName: 'security-core',
|
|
3
|
-
preset: '../../../../jest.preset.js',
|
|
4
|
-
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
|
|
5
|
-
coverageDirectory: '../../../../coverage/src/libs/security/core',
|
|
6
|
-
transform: {
|
|
7
|
-
'^.+\\.(ts|mjs|js|html)$': [
|
|
8
|
-
'jest-preset-angular',
|
|
9
|
-
{
|
|
10
|
-
tsconfig: '<rootDir>/tsconfig.spec.json',
|
|
11
|
-
stringifyContentPathRegex: '\\.(html|svg)$',
|
|
12
|
-
},
|
|
13
|
-
],
|
|
14
|
-
},
|
|
15
|
-
transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
|
|
16
|
-
snapshotSerializers: [
|
|
17
|
-
'jest-preset-angular/build/serializers/no-ng-attributes',
|
|
18
|
-
'jest-preset-angular/build/serializers/ng-snapshot',
|
|
19
|
-
'jest-preset-angular/build/serializers/html-comment',
|
|
20
|
-
],
|
|
21
|
-
};
|
package/ng-package.json
DELETED
package/project.json
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "security-core",
|
|
3
|
-
"$schema": "../../../../node_modules/nx/schemas/project-schema.json",
|
|
4
|
-
"sourceRoot": "src/libs/security/core/src",
|
|
5
|
-
"prefix": "lib",
|
|
6
|
-
"projectType": "library",
|
|
7
|
-
"tags": [],
|
|
8
|
-
"targets": {
|
|
9
|
-
"build": {
|
|
10
|
-
"executor": "@nx/angular:package",
|
|
11
|
-
"outputs": ["{workspaceRoot}/dist/{projectRoot}"],
|
|
12
|
-
"options": {
|
|
13
|
-
"project": "src/libs/security/core/ng-package.json"
|
|
14
|
-
},
|
|
15
|
-
"configurations": {
|
|
16
|
-
"production": {
|
|
17
|
-
"tsConfig": "src/libs/security/core/tsconfig.lib.prod.json"
|
|
18
|
-
},
|
|
19
|
-
"development": {
|
|
20
|
-
"tsConfig": "src/libs/security/core/tsconfig.lib.json"
|
|
21
|
-
}
|
|
22
|
-
},
|
|
23
|
-
"defaultConfiguration": "production"
|
|
24
|
-
},
|
|
25
|
-
"test": {
|
|
26
|
-
"executor": "@nx/jest:jest",
|
|
27
|
-
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
|
|
28
|
-
"options": {
|
|
29
|
-
"jestConfig": "src/libs/security/core/jest.config.ts"
|
|
30
|
-
}
|
|
31
|
-
},
|
|
32
|
-
"lint": {
|
|
33
|
-
"executor": "@nx/eslint:lint"
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './lib';
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Directive,
|
|
3
|
-
Input,
|
|
4
|
-
TemplateRef,
|
|
5
|
-
ViewContainerRef,
|
|
6
|
-
OnDestroy,
|
|
7
|
-
} from '@angular/core';
|
|
8
|
-
import { distinctUntilChanged, Subscription } from 'rxjs';
|
|
9
|
-
import { AuthContextService } from '../services';
|
|
10
|
-
|
|
11
|
-
@Directive({
|
|
12
|
-
standalone: true,
|
|
13
|
-
selector: '[hasPermission]',
|
|
14
|
-
})
|
|
15
|
-
export class HasPermissionDirective<T> implements OnDestroy {
|
|
16
|
-
private requiredPermissions: string | string[] = '';
|
|
17
|
-
private sub?: Subscription;
|
|
18
|
-
|
|
19
|
-
constructor(
|
|
20
|
-
private templateRef: TemplateRef<T>,
|
|
21
|
-
private viewContainer: ViewContainerRef,
|
|
22
|
-
private authContext: AuthContextService
|
|
23
|
-
) {}
|
|
24
|
-
|
|
25
|
-
@Input()
|
|
26
|
-
set hasPermission(value: string | string[]) {
|
|
27
|
-
this.requiredPermissions = value;
|
|
28
|
-
this.updateView();
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
ngOnDestroy(): void {
|
|
32
|
-
this.unregister();
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
private unregister() {
|
|
36
|
-
this.sub?.unsubscribe();
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
private updateView(): void {
|
|
40
|
-
this.unregister();
|
|
41
|
-
if (this.requiredPermissions || this.requiredPermissions.length) {
|
|
42
|
-
this.sub = this.authContext
|
|
43
|
-
.hasRequiredPermission(this.requiredPermissions)
|
|
44
|
-
.pipe(distinctUntilChanged())
|
|
45
|
-
.subscribe((hasPerm) => {
|
|
46
|
-
if (hasPerm) {
|
|
47
|
-
this.viewContainer.createEmbeddedView(this.templateRef);
|
|
48
|
-
} else {
|
|
49
|
-
this.viewContainer.clear();
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Directive,
|
|
3
|
-
Input,
|
|
4
|
-
TemplateRef,
|
|
5
|
-
ViewContainerRef,
|
|
6
|
-
OnDestroy,
|
|
7
|
-
} from '@angular/core';
|
|
8
|
-
import { distinctUntilChanged, Subscription } from 'rxjs';
|
|
9
|
-
import { AuthContextService } from '../services';
|
|
10
|
-
|
|
11
|
-
@Directive({
|
|
12
|
-
standalone: true,
|
|
13
|
-
selector: '[hasRole]',
|
|
14
|
-
})
|
|
15
|
-
export class HasRoleDirective<T> implements OnDestroy {
|
|
16
|
-
private requiredRoles: string | string[] | undefined;
|
|
17
|
-
private sub?: Subscription;
|
|
18
|
-
|
|
19
|
-
constructor(
|
|
20
|
-
private templateRef: TemplateRef<T>,
|
|
21
|
-
private viewContainer: ViewContainerRef,
|
|
22
|
-
private authContext: AuthContextService
|
|
23
|
-
) {}
|
|
24
|
-
|
|
25
|
-
@Input()
|
|
26
|
-
set hasRole(value: string | string[]) {
|
|
27
|
-
this.requiredRoles = value;
|
|
28
|
-
this.updateView();
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
ngOnDestroy(): void {
|
|
32
|
-
this.unregister();
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
private unregister() {
|
|
36
|
-
this.sub?.unsubscribe();
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
private updateView(): void {
|
|
40
|
-
this.unregister();
|
|
41
|
-
if (this.requiredRoles || this.requiredRoles?.length) {
|
|
42
|
-
this.sub = this.authContext
|
|
43
|
-
.hasRequiredRole(this.requiredRoles)
|
|
44
|
-
.pipe(distinctUntilChanged())
|
|
45
|
-
.subscribe((hasRole) => {
|
|
46
|
-
if (hasRole) {
|
|
47
|
-
this.viewContainer.createEmbeddedView(this.templateRef);
|
|
48
|
-
} else {
|
|
49
|
-
this.viewContainer.clear();
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { inject, Injectable } from '@angular/core';
|
|
2
|
-
import {
|
|
3
|
-
ActivatedRouteSnapshot,
|
|
4
|
-
CanActivate,
|
|
5
|
-
CanActivateChild,
|
|
6
|
-
Router,
|
|
7
|
-
UrlTree,
|
|
8
|
-
} from '@angular/router';
|
|
9
|
-
import { map, Observable, of, switchMap } from 'rxjs';
|
|
10
|
-
import { SecurityConfig } from '../models';
|
|
11
|
-
import { hasRequiredPermission } from '../models/functions';
|
|
12
|
-
import { SECURITY_CONFIG } from '../provider';
|
|
13
|
-
import { AuthContextService } from '../services';
|
|
14
|
-
|
|
15
|
-
@Injectable({
|
|
16
|
-
providedIn: 'root',
|
|
17
|
-
})
|
|
18
|
-
export class AuthGuard implements CanActivate, CanActivateChild {
|
|
19
|
-
private config = inject<SecurityConfig>(SECURITY_CONFIG);
|
|
20
|
-
private router = inject(Router);
|
|
21
|
-
private authContext = inject(AuthContextService);
|
|
22
|
-
|
|
23
|
-
canActivate(route: ActivatedRouteSnapshot): Observable<boolean | UrlTree> {
|
|
24
|
-
return this.checkPermissionsAndRolesAndNavigate(route, this.config.notAuthorizedPage);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
canActivateChild(
|
|
28
|
-
childRoute: ActivatedRouteSnapshot
|
|
29
|
-
): Observable<boolean | UrlTree> {
|
|
30
|
-
return this.checkPermissionsAndRolesAndNavigate(childRoute, this.config.notAuthorizedPage);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
private checkPermissionsAndRolesAndNavigate(
|
|
34
|
-
route: ActivatedRouteSnapshot,
|
|
35
|
-
notAuthorizedUrl: string | undefined
|
|
36
|
-
): Observable<boolean | UrlTree> {
|
|
37
|
-
const requiredPermissions = route.data['permissions'] as | string | string[] | undefined;
|
|
38
|
-
const requiredRoles = route.data['roles'] as string | string[] | undefined;
|
|
39
|
-
|
|
40
|
-
return this.authContext.user$.pipe(
|
|
41
|
-
map(
|
|
42
|
-
(user) => user &&
|
|
43
|
-
(!requiredPermissions ||
|
|
44
|
-
hasRequiredPermission(user, requiredPermissions)) &&
|
|
45
|
-
(!requiredRoles || hasRequiredPermission(user, requiredRoles))
|
|
46
|
-
),
|
|
47
|
-
switchMap((hasPermission) => {
|
|
48
|
-
if (!hasPermission && notAuthorizedUrl) {
|
|
49
|
-
return of(this.router.parseUrl(notAuthorizedUrl));
|
|
50
|
-
}
|
|
51
|
-
return of(!!hasPermission);
|
|
52
|
-
})
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
import { AuthenticatedUser } from './authenticated-user.model';
|
|
2
|
-
import {
|
|
3
|
-
convertJWTToUser,
|
|
4
|
-
hasRequiredPermission,
|
|
5
|
-
hasRequiredRole,
|
|
6
|
-
} from './functions';
|
|
7
|
-
|
|
8
|
-
describe('Permissions Utils', () => {
|
|
9
|
-
let user: AuthenticatedUser;
|
|
10
|
-
|
|
11
|
-
beforeEach(() => {
|
|
12
|
-
user = {
|
|
13
|
-
userName: 'testUser',
|
|
14
|
-
email: 'test@email.cz',
|
|
15
|
-
displayName: 'Test User',
|
|
16
|
-
permissions: ['READ', 'WRITE', 'DELETE_USER'],
|
|
17
|
-
roles: ['ADMIN', 'USER'],
|
|
18
|
-
};
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
describe('hasRequiredPermission', () => {
|
|
22
|
-
it('should return false if user is undefined', () => {
|
|
23
|
-
expect(hasRequiredPermission(undefined, 'READ')).toBe(false);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it('should return false if user.permissions is undefined', () => {
|
|
27
|
-
const noPermissionsUser: AuthenticatedUser = {
|
|
28
|
-
...user,
|
|
29
|
-
permissions: undefined,
|
|
30
|
-
};
|
|
31
|
-
expect(hasRequiredPermission(noPermissionsUser, 'READ')).toBe(false);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it('should return true for a single permission the user has', () => {
|
|
35
|
-
expect(hasRequiredPermission(user, 'READ')).toBe(true);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it('should return false for a single permission the user does not have', () => {
|
|
39
|
-
expect(hasRequiredPermission(user, 'UNKNOWN')).toBe(false);
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it('should return true for comma-delimited permissions if user has ALL', () => {
|
|
43
|
-
expect(hasRequiredPermission(user, 'READ,WRITE')).toBe(true);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it('should return false for comma-delimited permissions if missing one', () => {
|
|
47
|
-
expect(hasRequiredPermission(user, 'READ,UPDATE_USER')).toBe(false);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it('should return true if user has ANY of the permissions in the array', () => {
|
|
51
|
-
expect(hasRequiredPermission(user, ['READ', 'UPDATE_USER'])).toBe(true);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it('should return false if user does not have ANY of the permissions in the array', () => {
|
|
55
|
-
expect(hasRequiredPermission(user, ['UPDATE_USER', 'CREATE_POST'])).toBe(
|
|
56
|
-
false
|
|
57
|
-
);
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
describe('hasRequiredRole', () => {
|
|
62
|
-
it('should return false if user is undefined', () => {
|
|
63
|
-
expect(hasRequiredRole(undefined, 'ADMIN')).toBe(false);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it('should return false if user.roles is undefined', () => {
|
|
67
|
-
const noRolesUser: AuthenticatedUser = { ...user, roles: undefined };
|
|
68
|
-
expect(hasRequiredRole(noRolesUser, 'ADMIN')).toBe(false);
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
it('should return true for a single role the user has', () => {
|
|
72
|
-
expect(hasRequiredRole(user, 'ADMIN')).toBe(true);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
it('should return false for a single role the user does not have', () => {
|
|
76
|
-
expect(hasRequiredRole(user, 'MANAGER')).toBe(false);
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
it('should return true for comma-delimited roles if user has ALL', () => {
|
|
80
|
-
// user has 'ADMIN' and 'USER'
|
|
81
|
-
expect(hasRequiredRole(user, 'ADMIN,USER')).toBe(true);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it('should return false for comma-delimited roles if missing one', () => {
|
|
85
|
-
// user has 'ADMIN' and 'USER' but not 'MANAGER'
|
|
86
|
-
expect(hasRequiredRole(user, 'ADMIN,MANAGER')).toBe(false);
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
it('should return true if user has ANY of the roles in the array', () => {
|
|
90
|
-
expect(hasRequiredRole(user, ['USER', 'MANAGER'])).toBe(true);
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
it('should return false if user does not have ANY of the roles in the array', () => {
|
|
94
|
-
expect(hasRequiredRole(user, ['GUEST', 'MANAGER'])).toBe(false);
|
|
95
|
-
});
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
describe('convertJWTToUser', () => {
|
|
99
|
-
it('should return undefined if base64Token is undefined', () => {
|
|
100
|
-
expect(convertJWTToUser(undefined)).toBeUndefined();
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
it('should return undefined if base64Token is invalid', () => {
|
|
104
|
-
expect(convertJWTToUser('invalid.token')).toBeUndefined();
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it('should return undefined if payload does not contain userName', () => {
|
|
108
|
-
const token =
|
|
109
|
-
btoa('header') + '.' + btoa(JSON.stringify({})) + '.signature';
|
|
110
|
-
expect(convertJWTToUser(token)).toBeUndefined();
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
it('should return a user object if payload contains userName', () => {
|
|
114
|
-
const payload = {
|
|
115
|
-
sub: 'testUser',
|
|
116
|
-
email: 'test@example.com',
|
|
117
|
-
name: 'Test User',
|
|
118
|
-
role: 'ADMIN',
|
|
119
|
-
permission: 'READ',
|
|
120
|
-
};
|
|
121
|
-
const token =
|
|
122
|
-
btoa('header') + '.' + btoa(JSON.stringify(payload)) + '.signature';
|
|
123
|
-
const user = convertJWTToUser(token);
|
|
124
|
-
expect(user).toEqual({
|
|
125
|
-
userName: 'testUser',
|
|
126
|
-
email: 'test@example.com',
|
|
127
|
-
displayName: 'Test User',
|
|
128
|
-
roles: ['ADMIN'],
|
|
129
|
-
permissions: ['READ'],
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
it('should return a user object if payload contains nameid', () => {
|
|
134
|
-
const payload = {
|
|
135
|
-
nameid: 'testUser',
|
|
136
|
-
email: 'test@example.com',
|
|
137
|
-
name: 'Test User',
|
|
138
|
-
role: 'ADMIN',
|
|
139
|
-
permission: 'READ',
|
|
140
|
-
};
|
|
141
|
-
const token =
|
|
142
|
-
btoa('header') + '.' + btoa(JSON.stringify(payload)) + '.signature';
|
|
143
|
-
const user = convertJWTToUser(token);
|
|
144
|
-
expect(user).toEqual({
|
|
145
|
-
userName: 'testUser',
|
|
146
|
-
email: 'test@example.com',
|
|
147
|
-
displayName: 'Test User',
|
|
148
|
-
roles: ['ADMIN'],
|
|
149
|
-
permissions: ['READ'],
|
|
150
|
-
});
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
it('should return undefined if an error occurs during parsing', () => {
|
|
154
|
-
const token =
|
|
155
|
-
btoa('header') + '.' + btoa('invalidPayload') + '.signature';
|
|
156
|
-
expect(convertJWTToUser(token)).toBeUndefined();
|
|
157
|
-
});
|
|
158
|
-
});
|
|
159
|
-
});
|