Skip to content

Commit

Permalink
feat: add keycloak-auth library
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpetras committed Oct 18, 2023
1 parent 2e50909 commit eac1a4b
Show file tree
Hide file tree
Showing 22 changed files with 622 additions and 5 deletions.
4 changes: 2 additions & 2 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getJestProjects } from '@nx/jest';
import { getJestProjects } from '@nx/jest'

export default {
projects: getJestProjects(),
};
}
40 changes: 40 additions & 0 deletions libs/keycloak-auth/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"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"
}
]
}
},
{
"files": ["*.html"],
"extends": ["plugin:@nx/angular-template"],
"rules": {}
},
{
"files": ["*.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/dependency-checks": "error"
}
}
]
}
7 changes: 7 additions & 0 deletions libs/keycloak-auth/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# keycloak-auth

This library was generated with [Nx](https://nx.dev).

## Running unit tests

Run `nx test keycloak-auth` to execute the unit tests.
22 changes: 22 additions & 0 deletions libs/keycloak-auth/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-disable */
export default {
displayName: 'keycloak-auth',
preset: '../../jest.preset.js',
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
coverageDirectory: '../../coverage/libs/keycloak-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',
],
}
8 changes: 8 additions & 0 deletions libs/keycloak-auth/ng-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
"dest": "../../dist/libs/keycloak-auth",
"lib": {
"entryFile": "src/index.ts"
},
"assets": ["CHANGELOG.md", "./assets/**"]
}
12 changes: 12 additions & 0 deletions libs/keycloak-auth/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@onecx/keycloak-auth",
"version": "0.0.1",
"peerDependencies": {
"@angular/common": ">=15.2.7",
"@angular/core": ">=15.2.7",
"@onecx/portal-integration-angular": "~3",
"keycloak-angular": "^13.0.0",
"keycloak-js": "^18.0.0",
"rxjs": "~7.8.0"
}
}
57 changes: 57 additions & 0 deletions libs/keycloak-auth/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"name": "keycloak-auth",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/keycloak-auth/src",
"prefix": "ocx",
"tags": [],
"projectType": "library",
"targets": {
"build": {
"executor": "@nx/angular:package",
"outputs": ["{workspaceRoot}/dist/{projectRoot}"],
"options": {
"project": "libs/keycloak-auth/ng-package.json"
},
"configurations": {
"production": {
"tsConfig": "libs/keycloak-auth/tsconfig.lib.prod.json"
},
"development": {
"tsConfig": "libs/keycloak-auth/tsconfig.lib.json"
}
},
"defaultConfiguration": "production"
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "libs/keycloak-auth/jest.config.ts",
"passWithNoTests": true
},
"configurations": {
"ci": {
"ci": true,
"codeCoverage": true
}
}
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"libs/keycloak-auth/**/*.ts",
"libs/keycloak-auth/**/*.html",
"libs/keycloak-auth/package.json"
]
}
},
"release": {
"executor": "nx-release:build-update-publish",
"options": {
"libName": "portal-integration-angular"
}
}
}
}
1 change: 1 addition & 0 deletions libs/keycloak-auth/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/keycloak-auth.module'
24 changes: 24 additions & 0 deletions libs/keycloak-auth/src/lib/keycloak-auth.app-initializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// import { APP_INITIALIZER, Provider } from '@angular/core';
// import { KeycloakAuthService } from './keycloak-auth.service';

// function initializeKeycloak(authService: KeycloakAuthService) {
// return () =>
// authService.init({
// config: {
// url: 'http://localhost:8080/auth',
// realm: 'your-realm',
// clientId: 'your-client-id'
// },
// initOptions: {
// onLoad: 'check-sso',
// silentCheckSsoRedirectUri:
// window.location.origin + '/assets/silent-check-sso.html'
// }
// });
// }
// export const KeycloakAuthAppInitializer : Provider = {
// provide: APP_INITIALIZER,
// useFactory: initializeKeycloak,
// multi: true,
// deps: [KeycloakAuthService]
// }
42 changes: 42 additions & 0 deletions libs/keycloak-auth/src/lib/keycloak-auth.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { InjectionToken, NgModule, ModuleWithProviders } from '@angular/core'
import { CommonModule } from '@angular/common'
import { AUTH_SERVICE } from '@onecx/portal-integration-angular'
import { KeycloakAuthService } from './keycloak-auth.service'
import { KeycloakAngularModule } from 'keycloak-angular'
import { HTTP_INTERCEPTORS } from '@angular/common/http'
import { TokenInterceptor } from './token.interceptor'

export interface KeycloakAuthModuleConfig {
disablePermissionCheck: boolean
tokenInterceptorWhitelist?: string[]
}
/**
* Authentication module for keycloak. Requires @onecx/portal-integration-angular and keycloak-js to work.
*/
@NgModule({
imports: [CommonModule, KeycloakAngularModule],
providers: [
{
provide: AUTH_SERVICE,
useClass: KeycloakAuthService,
},
{ provide: HTTP_INTERCEPTORS, useClass: TokenInterceptor, multi: true },
],
})
export class KeycloakAuthModule {
static withConfig(config: KeycloakAuthModuleConfig): ModuleWithProviders<KeycloakAuthModule> {
return {
ngModule: KeycloakAuthModule,
providers: [
{
provide: AUTH_SERVICE,
useClass: KeycloakAuthService,
},
{ provide: HTTP_INTERCEPTORS, useClass: TokenInterceptor, multi: true },
{ provide: KEYCLOAK_AUTH_CONFIG, useValue: config },
],
}
}
}

export const KEYCLOAK_AUTH_CONFIG: InjectionToken<KeycloakAuthModuleConfig> = new InjectionToken('KEYCLOAK_AUTH_CONFIG')
Loading

0 comments on commit eac1a4b

Please sign in to comment.