-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add angular auth lib and add config key to angular-integration-…
…interface (#222)
- Loading branch information
Showing
19 changed files
with
522 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{ | ||
"extends": ["../../.eslintrc.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts"], | ||
"extends": ["plugin:@nx/angular", "plugin:@angular-eslint/template/process-inline-templates"], | ||
"rules": { | ||
"@angular-eslint/directive-selector": [ | ||
"error", | ||
{ | ||
"type": "attribute", | ||
"prefix": "ocx", | ||
"style": "camelCase" | ||
} | ||
], | ||
"@angular-eslint/component-selector": [ | ||
"error", | ||
{ | ||
"type": "element", | ||
"prefix": "ocx", | ||
"style": "kebab-case" | ||
} | ||
], | ||
"no-restricted-syntax": [ | ||
"off", | ||
{ | ||
"selector": "CallExpression[callee.object.name=\"console\"][callee.property.name=/^(debug|info|time|timeEnd|trace)$/]" | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"files": ["*.html"], | ||
"extends": ["plugin:@nx/angular-template"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.json"], | ||
"parser": "jsonc-eslint-parser", | ||
"rules": { | ||
"@nx/dependency-checks": "error" | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# angular-auth | ||
|
||
This library was generated with [Nx](https://nx.dev). | ||
|
||
## Running unit tests | ||
|
||
Run `nx test angular-auth` to execute the unit tests. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* eslint-disable */ | ||
export default { | ||
displayName: 'angular-auth', | ||
preset: '../../jest.preset.js', | ||
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'], | ||
coverageDirectory: '../../coverage/libs/angular-auth', | ||
transform: { | ||
'^.+\\.(ts|mjs|js|html)$': [ | ||
'jest-preset-angular', | ||
{ | ||
tsconfig: '<rootDir>/tsconfig.spec.json', | ||
stringifyContentPathRegex: '\\.(html|svg)$', | ||
}, | ||
], | ||
}, | ||
transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'], | ||
snapshotSerializers: [ | ||
'jest-preset-angular/build/serializers/no-ng-attributes', | ||
'jest-preset-angular/build/serializers/ng-snapshot', | ||
'jest-preset-angular/build/serializers/html-comment', | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"$schema": "../../node_modules/ng-packagr/ng-package.schema.json", | ||
"dest": "../../dist/libs/angular-auth", | ||
"lib": { | ||
"entryFile": "src/index.ts" | ||
}, | ||
"assets": ["CHANGELOG.md", "./assets/**"] | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "@onecx/angular-auth", | ||
"version": "4.13.2", | ||
"peerDependencies": { | ||
"@angular/common": ">=15.2.7", | ||
"@angular/core": ">=15.2.7", | ||
"@onecx/angular-integration-interface": "^4", | ||
"@onecx/integration-interface": "^4", | ||
"keycloak-angular": "^13.0.0", | ||
"keycloak-js": "^18.0.0", | ||
"rxjs": "~7.8.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
{ | ||
"name": "angular-auth", | ||
"$schema": "../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "libs/angular-auth/src", | ||
"prefix": "ocx", | ||
"tags": [], | ||
"projectType": "library", | ||
"targets": { | ||
"build": { | ||
"executor": "@nx/angular:package", | ||
"outputs": [ | ||
"{workspaceRoot}/dist/{projectRoot}" | ||
], | ||
"options": { | ||
"project": "libs/angular-auth/ng-package.json" | ||
}, | ||
"configurations": { | ||
"production": { | ||
"tsConfig": "libs/angular-auth/tsconfig.lib.prod.json" | ||
}, | ||
"development": { | ||
"tsConfig": "libs/angular-auth/tsconfig.lib.json" | ||
} | ||
}, | ||
"defaultConfiguration": "production" | ||
}, | ||
"test": { | ||
"executor": "@nx/jest:jest", | ||
"outputs": [ | ||
"{workspaceRoot}/coverage/{projectRoot}" | ||
], | ||
"options": { | ||
"jestConfig": "libs/angular-auth/jest.config.ts", | ||
"passWithNoTests": true | ||
}, | ||
"configurations": { | ||
"ci": { | ||
"ci": true, | ||
"codeCoverage": true | ||
} | ||
} | ||
}, | ||
"lint": { | ||
"executor": "@nx/linter:eslint", | ||
"outputs": [ | ||
"{options.outputFile}" | ||
], | ||
"options": { | ||
"lintFilePatterns": [ | ||
"libs/angular-auth/**/*.ts", | ||
"libs/angular-auth/**/*.html", | ||
"libs/angular-auth/package.json" | ||
] | ||
} | ||
}, | ||
"release": { | ||
"executor": "nx-release:build-update-publish", | ||
"options": { | ||
"libName": "portal-integration-angular" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './lib/angular-auth.module' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { APP_INITIALIZER, NgModule } from '@angular/core' | ||
import { CommonModule } from '@angular/common' | ||
import { HTTP_INTERCEPTORS } from '@angular/common/http' | ||
import { ConfigurationService } from '@onecx/angular-integration-interface' | ||
import { TokenInterceptor } from './token.interceptor' | ||
import { AuthService } from './auth.service' | ||
import { AuthServiceWrapper } from './auth-service-wrapper' | ||
import { KeycloakAuthService } from './auth_services/keycloak-auth.service' | ||
import { KeycloakService } from 'keycloak-angular' | ||
|
||
function appInitializer(configService: ConfigurationService, authService: AuthService) { | ||
return async () => { | ||
await configService.isInitialized | ||
await authService.init() | ||
} | ||
} | ||
|
||
@NgModule({ | ||
imports: [CommonModule], | ||
providers: [ | ||
AuthServiceWrapper, | ||
{ provide: HTTP_INTERCEPTORS, useClass: TokenInterceptor, multi: true }, | ||
{ | ||
provide: APP_INITIALIZER, | ||
useFactory: appInitializer, | ||
deps: [ConfigurationService, AuthServiceWrapper], | ||
multi: true, | ||
}, | ||
KeycloakAuthService, | ||
KeycloakService, | ||
], | ||
}) | ||
export class AngularAuthModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { filter } from 'rxjs/internal/operators/filter' | ||
import { AuthService } from './auth.service' | ||
import { EventsTopic } from '@onecx/integration-interface' | ||
import { AppStateService, CONFIG_KEY, ConfigurationService } from '@onecx/angular-integration-interface' | ||
import { Injectable, Injector } from '@angular/core' | ||
import { KeycloakAuthService } from './auth_services/keycloak-auth.service' | ||
@Injectable() | ||
export class AuthServiceWrapper { | ||
private eventsTopic$ = new EventsTopic() | ||
private authService: AuthService | undefined | ||
|
||
constructor( | ||
private configService: ConfigurationService, | ||
private appStateService: AppStateService, | ||
private injector: Injector | ||
) { | ||
this.eventsTopic$ | ||
.pipe(filter((e) => e.type === 'authentication#logoutButtonClicked')) | ||
.subscribe(() => this.authService?.logout()) | ||
} | ||
async init(): Promise<boolean | undefined> { | ||
await this.configService.isInitialized | ||
|
||
this.initializeAuthService() | ||
const initResult = this.getInitResult() | ||
return initResult | ||
} | ||
async getInitResult(): Promise<boolean | undefined> { | ||
const initResult = await this.authService?.init() | ||
|
||
if (initResult) { | ||
await this.appStateService.isAuthenticated$.publish() | ||
} | ||
return initResult | ||
} | ||
getHeaderValues(): Record<string, string> { | ||
return this.authService?.getHeaderValues() ?? {} | ||
} | ||
|
||
initializeAuthService(): void { | ||
const serviceTypeConfig = this.configService.getProperty(CONFIG_KEY.AUTH_SERVICE) ?? 'keycloak' | ||
switch (serviceTypeConfig) { | ||
case 'keycloak': | ||
this.authService = this.injector.get(KeycloakAuthService) | ||
break | ||
// TODO: Extend the other cases in the future | ||
default: | ||
throw new Error('Configured AuthService not found') | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export interface AuthService { | ||
init(): Promise<boolean> | ||
|
||
getHeaderValues(): Record<string, string> | ||
|
||
logout(): void | ||
} |
Oops, something went wrong.