@verisoft/security-core 20.1.1 → 20.1.2
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/fesm2022/verisoft-security-core.mjs +380 -0
- package/fesm2022/verisoft-security-core.mjs.map +1 -0
- package/index.d.ts +153 -0
- package/package.json +17 -3
- 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/directives/index.ts +0 -2
- package/src/lib/guards/auth.guard.ts +0 -55
- package/src/lib/guards/index.ts +0 -1
- package/src/lib/index.ts +0 -6
- package/src/lib/models/authenticated-user.model.ts +0 -8
- package/src/lib/models/config.model.ts +0 -9
- package/src/lib/models/functions.spec.ts +0 -159
- package/src/lib/models/functions.ts +0 -103
- package/src/lib/models/index.ts +0 -3
- package/src/lib/provider.ts +0 -52
- package/src/lib/services/auth-context.service.ts +0 -38
- package/src/lib/services/index.ts +0 -7
- 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/services/token-provider.ts +0 -5
- package/src/lib/state/actions.ts +0 -7
- package/src/lib/state/feature.ts +0 -10
- package/src/lib/state/index.ts +0 -4
- 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
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import { AuthenticatedUser } from './authenticated-user.model';
|
|
2
|
-
|
|
3
|
-
export function hasRequiredPermission(
|
|
4
|
-
user: AuthenticatedUser | undefined,
|
|
5
|
-
permission: string | string[]
|
|
6
|
-
): boolean {
|
|
7
|
-
return hasItems(user?.permissions, permission);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export function hasRequiredRole(
|
|
11
|
-
user: AuthenticatedUser | undefined,
|
|
12
|
-
role: string | string[]
|
|
13
|
-
): boolean {
|
|
14
|
-
return hasItems(user?.roles, role);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function hasItems(
|
|
18
|
-
userItems: string[] | undefined,
|
|
19
|
-
neededItems: string | string[]
|
|
20
|
-
): boolean {
|
|
21
|
-
if (!neededItems || !neededItems.length) {
|
|
22
|
-
return true;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if (!userItems || !userItems.length) {
|
|
26
|
-
return false;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const userItemsSet = new Set(userItems);
|
|
30
|
-
if (Array.isArray(neededItems)) {
|
|
31
|
-
return neededItems.some((item) => hasItems(userItems, item));
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (neededItems.includes(',')) {
|
|
35
|
-
const splitItems = neededItems.split(',').map((i) => i.trim());
|
|
36
|
-
return splitItems.every((i) => userItemsSet.has(i));
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return userItemsSet.has(neededItems);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export function convertJWTToUser(
|
|
43
|
-
base64Token?: string
|
|
44
|
-
): AuthenticatedUser | undefined {
|
|
45
|
-
if (!base64Token) {
|
|
46
|
-
return undefined;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
try {
|
|
50
|
-
const parts = base64Token.split('.');
|
|
51
|
-
if (parts.length < 2) {
|
|
52
|
-
return undefined;
|
|
53
|
-
}
|
|
54
|
-
const payload = decodeJwtPayload(parts[1]);
|
|
55
|
-
|
|
56
|
-
const userName = payload['unique_name'] ?? payload['nameid'];
|
|
57
|
-
if (!userName) {
|
|
58
|
-
return undefined;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const user: AuthenticatedUser = {
|
|
62
|
-
userName,
|
|
63
|
-
userId: payload['nameid'],
|
|
64
|
-
email: payload['email'],
|
|
65
|
-
displayName: payload['name'],
|
|
66
|
-
roles: convertToArray(payload, 'role'),
|
|
67
|
-
permissions: convertToArray(payload, 'permission'),
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
return user;
|
|
71
|
-
} catch (error) {
|
|
72
|
-
return undefined;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export function decodeJwtPayload(jwt: string): any {
|
|
77
|
-
const base64 = jwt.replace(/-/g, '+').replace(/_/g, '/');
|
|
78
|
-
|
|
79
|
-
const jsonPayload = decodeURIComponent(
|
|
80
|
-
atob(base64)
|
|
81
|
-
.split('')
|
|
82
|
-
.map(c => '%' + c.charCodeAt(0).toString(16).padStart(2, '0'))
|
|
83
|
-
.join('')
|
|
84
|
-
);
|
|
85
|
-
|
|
86
|
-
return JSON.parse(jsonPayload);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function convertToArray(
|
|
90
|
-
item: { [key: string]: string | string[] },
|
|
91
|
-
propertyName: string
|
|
92
|
-
): string[] | undefined {
|
|
93
|
-
const rawValue = item[propertyName];
|
|
94
|
-
if (!rawValue) {
|
|
95
|
-
return undefined;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (Array.isArray(rawValue)) {
|
|
99
|
-
return rawValue;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return [rawValue];
|
|
103
|
-
}
|
package/src/lib/models/index.ts
DELETED
package/src/lib/provider.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { EnvironmentProviders, InjectionToken, ModuleWithProviders, NgModule, Provider, inject, provideAppInitializer } from '@angular/core';
|
|
2
|
-
import { Router } from '@angular/router';
|
|
3
|
-
import { StoreModule } from '@ngrx/store';
|
|
4
|
-
import { AuthGuard } from './guards';
|
|
5
|
-
import { SecurityConfig } from './models';
|
|
6
|
-
import {
|
|
7
|
-
AuthContextService,
|
|
8
|
-
LocalStorageTokenProvider,
|
|
9
|
-
securityInitializerFactory,
|
|
10
|
-
TokenProvider,
|
|
11
|
-
} from './services';
|
|
12
|
-
import { SecurityFeature } from './state/feature';
|
|
13
|
-
|
|
14
|
-
export function provideSecurity(
|
|
15
|
-
config: Partial<SecurityConfig> | undefined = undefined
|
|
16
|
-
): (Provider | EnvironmentProviders)[] {
|
|
17
|
-
const securityConfig: SecurityConfig = {
|
|
18
|
-
tokenStorageKey: 'APP_TOKEN',
|
|
19
|
-
notAuthorizedPage: '/not-authorized',
|
|
20
|
-
...(config ?? {}),
|
|
21
|
-
};
|
|
22
|
-
return [
|
|
23
|
-
AuthGuard,
|
|
24
|
-
SECURITY_INITIALIZER_PROVIDER,
|
|
25
|
-
{ provide: SECURITY_CONTEXT_TOKEN_PROVIDER, useClass: LocalStorageTokenProvider },
|
|
26
|
-
{ provide: SECURITY_CONFIG, useValue: securityConfig },
|
|
27
|
-
];
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
@NgModule({
|
|
31
|
-
imports: [StoreModule.forFeature(SecurityFeature)],
|
|
32
|
-
})
|
|
33
|
-
export class SecurityModule {
|
|
34
|
-
static forRoot(
|
|
35
|
-
config?: Partial<SecurityConfig>
|
|
36
|
-
): ModuleWithProviders<SecurityModule> {
|
|
37
|
-
return {
|
|
38
|
-
ngModule: SecurityModule,
|
|
39
|
-
providers: [...provideSecurity(config)],
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export const SECURITY_CONTEXT_TOKEN_PROVIDER = new InjectionToken('SECURITY_CONTEXT_TOKEN_PROVIDER');
|
|
45
|
-
|
|
46
|
-
export const SECURITY_CONFIG = new InjectionToken('SECURITY_CONFIG');
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
export const SECURITY_INITIALIZER_PROVIDER: EnvironmentProviders = provideAppInitializer(() => {
|
|
50
|
-
const initializerFn = (securityInitializerFactory)(inject<TokenProvider>(SECURITY_CONTEXT_TOKEN_PROVIDER), inject(AuthContextService), inject<SecurityConfig>(SECURITY_CONFIG), inject(Router));
|
|
51
|
-
return initializerFn();
|
|
52
|
-
});
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { Injectable } from '@angular/core';
|
|
2
|
-
import { Store } from '@ngrx/store';
|
|
3
|
-
import { map, Observable } from 'rxjs';
|
|
4
|
-
import { AuthenticatedUser } from '../models';
|
|
5
|
-
import { hasRequiredPermission, hasRequiredRole } from '../models/functions';
|
|
6
|
-
import { setUser } from '../state/actions';
|
|
7
|
-
import { selectIsAuthenticated, selectUser } from '../state/selectors';
|
|
8
|
-
|
|
9
|
-
@Injectable({
|
|
10
|
-
providedIn: 'root',
|
|
11
|
-
})
|
|
12
|
-
export class AuthContextService {
|
|
13
|
-
user$: Observable<AuthenticatedUser | undefined>;
|
|
14
|
-
isAuthenticated$: Observable<boolean>;
|
|
15
|
-
|
|
16
|
-
constructor(private store: Store) {
|
|
17
|
-
this.user$ = this.store.select(selectUser);
|
|
18
|
-
this.isAuthenticated$ = this.store.select(selectIsAuthenticated);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
setUser(user: AuthenticatedUser | undefined): void {
|
|
22
|
-
this.store.dispatch(setUser({ user }));
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
hasRequiredPermission(
|
|
26
|
-
requiredPermissions: string | string[]
|
|
27
|
-
): Observable<boolean> {
|
|
28
|
-
return this.user$.pipe(
|
|
29
|
-
map((user) => hasRequiredPermission(user, requiredPermissions))
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
hasRequiredRole(requiredPermissions: string | string[]): Observable<boolean> {
|
|
34
|
-
return this.user$.pipe(
|
|
35
|
-
map((user) => hasRequiredRole(user, requiredPermissions))
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
@@ -1,7 +0,0 @@
|
|
|
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,23 +0,0 @@
|
|
|
1
|
-
import { inject, Injectable } from '@angular/core';
|
|
2
|
-
import { Observable, of } from 'rxjs';
|
|
3
|
-
import { SecurityConfig } from '../models';
|
|
4
|
-
import { SECURITY_CONFIG } from '../provider';
|
|
5
|
-
import { TokenProvider } from './token-provider';
|
|
6
|
-
|
|
7
|
-
@Injectable()
|
|
8
|
-
export class LocalStorageTokenProvider implements TokenProvider {
|
|
9
|
-
private config = inject<SecurityConfig>(SECURITY_CONFIG);
|
|
10
|
-
|
|
11
|
-
getToken(): Observable<string | undefined> {
|
|
12
|
-
const token = localStorage.getItem(this.config.tokenStorageKey);
|
|
13
|
-
return of(token ?? undefined);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
setToken(token: string): void {
|
|
17
|
-
localStorage.setItem(this.config.tokenStorageKey, token);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
removeToken(): void {
|
|
21
|
-
localStorage.clear();
|
|
22
|
-
}
|
|
23
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { inject, Injectable } from '@angular/core';
|
|
2
|
-
import { Observable, of } from 'rxjs';
|
|
3
|
-
import { SecurityConfig } from '../models';
|
|
4
|
-
import { SECURITY_CONFIG } from '../provider';
|
|
5
|
-
import { TokenProvider } from './token-provider';
|
|
6
|
-
|
|
7
|
-
@Injectable()
|
|
8
|
-
export class LocalStorageTokenProvider implements TokenProvider {
|
|
9
|
-
private config = inject<SecurityConfig>(SECURITY_CONFIG);
|
|
10
|
-
|
|
11
|
-
getToken(): Observable<string | undefined> {
|
|
12
|
-
const token = localStorage.getItem(this.config.tokenStorageKey);
|
|
13
|
-
return of(token ?? undefined);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { inject, Injectable } from '@angular/core';
|
|
2
|
-
import { Router } from '@angular/router';
|
|
3
|
-
import { convertJWTToUser, SecurityConfig } from '../models';
|
|
4
|
-
import { SECURITY_CONFIG, SECURITY_CONTEXT_TOKEN_PROVIDER } from '../provider';
|
|
5
|
-
import { AuthContextService } from './auth-context.service';
|
|
6
|
-
import { LocalStorageTokenProvider } from './local-storage-token-provider';
|
|
7
|
-
|
|
8
|
-
@Injectable()
|
|
9
|
-
export class LoginService {
|
|
10
|
-
private config = inject<SecurityConfig>(SECURITY_CONFIG);
|
|
11
|
-
private tokenProvider = inject<LocalStorageTokenProvider>(SECURITY_CONTEXT_TOKEN_PROVIDER);
|
|
12
|
-
private authService = inject(AuthContextService);
|
|
13
|
-
private router = inject(Router);
|
|
14
|
-
|
|
15
|
-
login(token?: string): void {
|
|
16
|
-
if (token) {
|
|
17
|
-
this.tokenProvider.setToken(token);
|
|
18
|
-
this.authService.setUser(convertJWTToUser(token));
|
|
19
|
-
} else {
|
|
20
|
-
this.router.navigate([this.config.loginPage]);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { inject, Injectable } from '@angular/core';
|
|
2
|
-
import { SECURITY_CONTEXT_TOKEN_PROVIDER } from '../provider';
|
|
3
|
-
import { AuthContextService } from './auth-context.service';
|
|
4
|
-
import { LocalStorageTokenProvider } from './local-storage-token-provider';
|
|
5
|
-
|
|
6
|
-
@Injectable()
|
|
7
|
-
export class LogoutService {
|
|
8
|
-
private readonly tokenProvider = inject<LocalStorageTokenProvider>(SECURITY_CONTEXT_TOKEN_PROVIDER);
|
|
9
|
-
private readonly authService = inject(AuthContextService);
|
|
10
|
-
|
|
11
|
-
logout(): void {
|
|
12
|
-
this.tokenProvider.removeToken();
|
|
13
|
-
this.authService.setUser(undefined);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { Router } from '@angular/router';
|
|
2
|
-
import { firstValueFrom, from, of, switchMap } from 'rxjs';
|
|
3
|
-
import { convertJWTToUser, SecurityConfig } from '../models';
|
|
4
|
-
import { AuthContextService } from './auth-context.service';
|
|
5
|
-
import { TokenProvider } from './token-provider';
|
|
6
|
-
|
|
7
|
-
export function securityInitializerFactory(
|
|
8
|
-
tokenProvider: TokenProvider,
|
|
9
|
-
authService: AuthContextService,
|
|
10
|
-
config: SecurityConfig,
|
|
11
|
-
router: Router
|
|
12
|
-
): () => Promise<unknown> {
|
|
13
|
-
const initializationFn = tokenProvider.getToken().pipe(
|
|
14
|
-
switchMap((token) => {
|
|
15
|
-
const user = convertJWTToUser(token);
|
|
16
|
-
if (config.loginPage && !user) {
|
|
17
|
-
return from(router.navigate([config.loginPage]));
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
authService.setUser(user);
|
|
21
|
-
return of({});
|
|
22
|
-
})
|
|
23
|
-
);
|
|
24
|
-
|
|
25
|
-
return () => firstValueFrom(initializationFn)
|
|
26
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { inject, Injectable } from '@angular/core';
|
|
2
|
-
import { Observable, of } from 'rxjs';
|
|
3
|
-
import { SecurityConfig } from '../models';
|
|
4
|
-
import { SECURITY_CONFIG } from '../provider';
|
|
5
|
-
import { TokenProvider } from './token-provider';
|
|
6
|
-
|
|
7
|
-
@Injectable()
|
|
8
|
-
export class SessionStorageTokenProvider implements TokenProvider {
|
|
9
|
-
private config = inject<SecurityConfig>(SECURITY_CONFIG);
|
|
10
|
-
|
|
11
|
-
getToken(): Observable<string | undefined> {
|
|
12
|
-
const token = sessionStorage.getItem(this.config.tokenStorageKey);
|
|
13
|
-
return of(token ?? undefined);
|
|
14
|
-
}
|
|
15
|
-
}
|
package/src/lib/state/actions.ts
DELETED
package/src/lib/state/feature.ts
DELETED
package/src/lib/state/index.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { createReducer, on } from '@ngrx/store';
|
|
2
|
-
import { setUser } from './actions';
|
|
3
|
-
import { initialState } from './state';
|
|
4
|
-
|
|
5
|
-
export const authReducer = createReducer(
|
|
6
|
-
initialState,
|
|
7
|
-
on(setUser, (state, action) => ({
|
|
8
|
-
...state,
|
|
9
|
-
user: action.user
|
|
10
|
-
}))
|
|
11
|
-
);
|
package/src/lib/state/state.ts
DELETED
package/src/test-setup.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
// @ts-expect-error https://thymikee.github.io/jest-preset-angular/docs/getting-started/test-environment
|
|
2
|
-
globalThis.ngJest = {
|
|
3
|
-
testEnvironmentOptions: {
|
|
4
|
-
errorOnUnknownElements: true,
|
|
5
|
-
errorOnUnknownProperties: true,
|
|
6
|
-
},
|
|
7
|
-
};
|
|
8
|
-
import 'jest-preset-angular/setup-jest';
|
package/tsconfig.json
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "es2022",
|
|
4
|
-
"forceConsistentCasingInFileNames": true,
|
|
5
|
-
"strict": true,
|
|
6
|
-
"noImplicitOverride": true,
|
|
7
|
-
"noPropertyAccessFromIndexSignature": true,
|
|
8
|
-
"noImplicitReturns": true,
|
|
9
|
-
"noFallthroughCasesInSwitch": true
|
|
10
|
-
},
|
|
11
|
-
"files": [],
|
|
12
|
-
"include": [],
|
|
13
|
-
"references": [
|
|
14
|
-
{
|
|
15
|
-
"path": "./tsconfig.lib.json"
|
|
16
|
-
},
|
|
17
|
-
{
|
|
18
|
-
"path": "./tsconfig.spec.json"
|
|
19
|
-
}
|
|
20
|
-
],
|
|
21
|
-
"extends": "../../../../tsconfig.base.json",
|
|
22
|
-
"angularCompilerOptions": {
|
|
23
|
-
"enableI18nLegacyMessageIdFormat": false,
|
|
24
|
-
"strictInjectionParameters": true,
|
|
25
|
-
"strictInputAccessModifiers": true,
|
|
26
|
-
"strictTemplates": true
|
|
27
|
-
}
|
|
28
|
-
}
|
package/tsconfig.lib.json
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "./tsconfig.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"outDir": "../../../../dist/out-tsc",
|
|
5
|
-
"declaration": true,
|
|
6
|
-
"declarationMap": true,
|
|
7
|
-
"inlineSources": true,
|
|
8
|
-
"types": []
|
|
9
|
-
},
|
|
10
|
-
"exclude": [
|
|
11
|
-
"src/**/*.spec.ts",
|
|
12
|
-
"src/test-setup.ts",
|
|
13
|
-
"jest.config.ts",
|
|
14
|
-
"src/**/*.test.ts"
|
|
15
|
-
],
|
|
16
|
-
"include": ["src/**/*.ts"]
|
|
17
|
-
}
|
package/tsconfig.lib.prod.json
DELETED
package/tsconfig.spec.json
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "./tsconfig.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"outDir": "../../../../dist/out-tsc",
|
|
5
|
-
"module": "commonjs",
|
|
6
|
-
"target": "es2016",
|
|
7
|
-
"types": ["jest", "node"]
|
|
8
|
-
},
|
|
9
|
-
"files": ["src/test-setup.ts"],
|
|
10
|
-
"include": [
|
|
11
|
-
"jest.config.ts",
|
|
12
|
-
"src/**/*.test.ts",
|
|
13
|
-
"src/**/*.spec.ts",
|
|
14
|
-
"src/**/*.d.ts"
|
|
15
|
-
]
|
|
16
|
-
}
|