@verisoft/security-core 18.4.0 → 18.6.0

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.
Files changed (84) hide show
  1. package/esm2022/index.mjs +2 -0
  2. package/esm2022/lib/directives/has-permission.directive.mjs +55 -0
  3. package/esm2022/lib/directives/has-role.directive.mjs +55 -0
  4. package/esm2022/lib/directives/index.mjs +3 -0
  5. package/esm2022/lib/guards/auth.guard.mjs +40 -0
  6. package/esm2022/lib/guards/index.mjs +2 -0
  7. package/esm2022/lib/index.mjs +7 -0
  8. package/esm2022/lib/models/authenticated-user.model.mjs +2 -0
  9. package/esm2022/lib/models/config.model.mjs +2 -0
  10. package/esm2022/lib/models/functions.mjs +70 -0
  11. package/esm2022/lib/models/index.mjs +4 -0
  12. package/esm2022/lib/provider.mjs +47 -0
  13. package/esm2022/lib/services/auth-context.service.mjs +36 -0
  14. package/esm2022/lib/services/index.mjs +8 -0
  15. package/esm2022/lib/services/local-storage-token-provider.mjs +23 -0
  16. package/esm2022/lib/services/login.service.mjs +27 -0
  17. package/esm2022/lib/services/logout.service.mjs +18 -0
  18. package/esm2022/lib/services/security-initializer.mjs +14 -0
  19. package/esm2022/lib/services/session-token-provider.mjs +17 -0
  20. package/esm2022/lib/services/token-provider.mjs +2 -0
  21. package/esm2022/lib/state/actions.mjs +3 -0
  22. package/esm2022/lib/state/feature.mjs +8 -0
  23. package/esm2022/lib/state/index.mjs +5 -0
  24. package/esm2022/lib/state/reducers.mjs +8 -0
  25. package/esm2022/lib/state/selectors.mjs +5 -0
  26. package/esm2022/lib/state/state.mjs +4 -0
  27. package/esm2022/verisoft-security-core.mjs +5 -0
  28. package/fesm2022/verisoft-security-core.mjs +382 -0
  29. package/fesm2022/verisoft-security-core.mjs.map +1 -0
  30. package/index.d.ts +1 -0
  31. package/lib/directives/has-permission.directive.d.ts +17 -0
  32. package/lib/directives/has-role.directive.d.ts +17 -0
  33. package/{src/lib/directives/index.ts → lib/directives/index.d.ts} +2 -2
  34. package/lib/guards/auth.guard.d.ts +13 -0
  35. package/{src/lib/guards/index.ts → lib/guards/index.d.ts} +1 -1
  36. package/{src/lib/index.ts → lib/index.d.ts} +6 -6
  37. package/{src/lib/models/authenticated-user.model.ts → lib/models/authenticated-user.model.d.ts} +8 -8
  38. package/{src/lib/models/config.model.ts → lib/models/config.model.d.ts} +8 -9
  39. package/lib/models/functions.d.ts +5 -0
  40. package/{src/lib/models/index.ts → lib/models/index.d.ts} +3 -3
  41. package/lib/provider.d.ts +14 -0
  42. package/lib/services/auth-context.service.d.ts +15 -0
  43. package/{src/lib/services/index.ts → lib/services/index.d.ts} +7 -7
  44. package/lib/services/local-storage-token-provider.d.ts +11 -0
  45. package/lib/services/login.service.d.ts +10 -0
  46. package/lib/services/logout.service.d.ts +8 -0
  47. package/lib/services/security-initializer.d.ts +5 -0
  48. package/lib/services/session-token-provider.d.ts +9 -0
  49. package/{src/lib/services/token-provider.ts → lib/services/token-provider.d.ts} +4 -5
  50. package/lib/state/actions.d.ts +6 -0
  51. package/lib/state/feature.d.ts +7 -0
  52. package/{src/lib/state/index.ts → lib/state/index.d.ts} +4 -4
  53. package/lib/state/reducers.d.ts +1 -0
  54. package/lib/state/selectors.d.ts +2 -0
  55. package/lib/state/state.d.ts +5 -0
  56. package/package.json +27 -11
  57. package/.eslintrc.json +0 -48
  58. package/jest.config.ts +0 -21
  59. package/ng-package.json +0 -7
  60. package/project.json +0 -36
  61. package/src/index.ts +0 -1
  62. package/src/lib/directives/has-permission.directive.ts +0 -54
  63. package/src/lib/directives/has-role.directive.ts +0 -54
  64. package/src/lib/guards/auth.guard.ts +0 -55
  65. package/src/lib/models/functions.spec.ts +0 -159
  66. package/src/lib/models/functions.ts +0 -103
  67. package/src/lib/provider.ts +0 -58
  68. package/src/lib/services/auth-context.service.ts +0 -38
  69. package/src/lib/services/local-storage-token-provider.ts +0 -23
  70. package/src/lib/services/local-token-provider.ts +0 -15
  71. package/src/lib/services/login.service.ts +0 -23
  72. package/src/lib/services/logout.service.ts +0 -15
  73. package/src/lib/services/security-initializer.ts +0 -26
  74. package/src/lib/services/session-token-provider.ts +0 -15
  75. package/src/lib/state/actions.ts +0 -7
  76. package/src/lib/state/feature.ts +0 -10
  77. package/src/lib/state/reducers.ts +0 -11
  78. package/src/lib/state/selectors.ts +0 -9
  79. package/src/lib/state/state.ts +0 -9
  80. package/src/test-setup.ts +0 -8
  81. package/tsconfig.json +0 -30
  82. package/tsconfig.lib.json +0 -17
  83. package/tsconfig.lib.prod.json +0 -10
  84. package/tsconfig.spec.json +0 -16
@@ -1,58 +0,0 @@
1
- import {
2
- APP_INITIALIZER,
3
- InjectionToken,
4
- ModuleWithProviders,
5
- NgModule,
6
- Provider,
7
- } from '@angular/core';
8
- import { Router } from '@angular/router';
9
- import { StoreModule } from '@ngrx/store';
10
- import { AuthGuard } from './guards';
11
- import { SecurityConfig } from './models';
12
- import {
13
- AuthContextService,
14
- LocalStorageTokenProvider,
15
- securityInitializerFactory,
16
- } from './services';
17
- import { SecurityFeature } from './state/feature';
18
-
19
- export function provideSecurity(
20
- config: Partial<SecurityConfig> | undefined = undefined
21
- ): Provider[] {
22
- const securityConfig: SecurityConfig = {
23
- tokenStorageKey: 'APP_TOKEN',
24
- notAuthorizedPage: '/not-authorized',
25
- ...(config ?? {}),
26
- };
27
- return [
28
- AuthGuard,
29
- SECURITY_INITIALIZER_PROVIDER,
30
- { provide: SECURITY_CONTEXT_TOKEN_PROVIDER, useClass: LocalStorageTokenProvider },
31
- { provide: SECURITY_CONFIG, useValue: securityConfig },
32
- ];
33
- }
34
-
35
- @NgModule({
36
- imports: [StoreModule.forFeature(SecurityFeature)],
37
- })
38
- export class SecurityModule {
39
- static forRoot(
40
- config?: Partial<SecurityConfig>
41
- ): ModuleWithProviders<SecurityModule> {
42
- return {
43
- ngModule: SecurityModule,
44
- providers: [...provideSecurity(config)],
45
- };
46
- }
47
- }
48
-
49
- export const SECURITY_CONTEXT_TOKEN_PROVIDER = new InjectionToken('SECURITY_CONTEXT_TOKEN_PROVIDER');
50
-
51
- export const SECURITY_CONFIG = new InjectionToken('SECURITY_CONFIG');
52
-
53
- export const SECURITY_INITIALIZER_PROVIDER: Provider = {
54
- provide: APP_INITIALIZER,
55
- useFactory: securityInitializerFactory,
56
- deps: [SECURITY_CONTEXT_TOKEN_PROVIDER, AuthContextService, SECURITY_CONFIG, Router],
57
- multi: true,
58
- };
@@ -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,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
- }
@@ -1,7 +0,0 @@
1
- import { createAction, props } from '@ngrx/store';
2
- import { AuthenticatedUser } from '../models';
3
-
4
- export const setUser = createAction(
5
- '[Auth] Set User',
6
- props<{ user: AuthenticatedUser | undefined }>()
7
- );
@@ -1,10 +0,0 @@
1
- import { createFeature } from "@ngrx/store"
2
- import { authReducer } from "./reducers"
3
-
4
- export const SECURITY_FEATURE_NAME = 'SECURITY';
5
-
6
- export const SecurityFeature = createFeature({
7
- name: SECURITY_FEATURE_NAME,
8
- reducer: authReducer
9
- });
10
-
@@ -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
- );
@@ -1,9 +0,0 @@
1
- import { createSelector } from '@ngrx/store';
2
- import { SecurityFeature } from './feature';
3
-
4
- export const selectIsAuthenticated = createSelector(
5
- SecurityFeature.selectUser,
6
- (user) => !!user
7
- );
8
-
9
- export const selectUser = SecurityFeature.selectUser;
@@ -1,9 +0,0 @@
1
- import { AuthenticatedUser } from '../models';
2
-
3
- export interface AuthState {
4
- user: AuthenticatedUser | undefined;
5
- }
6
-
7
- export const initialState: AuthState = {
8
- user: undefined,
9
- };
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,30 +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
- "compilationMode": "partial",
28
- "enableIvy": true
29
- }
30
- }
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
- }
@@ -1,10 +0,0 @@
1
- {
2
- "extends": "./tsconfig.lib.json",
3
- "compilerOptions": {
4
- "declarationMap": false
5
- },
6
- "angularCompilerOptions": {
7
- "compilationMode": "partial",
8
- "enableIvy": true
9
- }
10
- }
@@ -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
- }