{"id":19623621,"url":"https://github.com/flowcore-io/library-nestjs-oidc-protect-ts","last_synced_at":"2026-02-03T00:02:02.127Z","repository":{"id":65325751,"uuid":"583630601","full_name":"flowcore-io/library-nestjs-oidc-protect-ts","owner":"flowcore-io","description":"A NestJS OpenID Connect library that can validate a token and protect routes with Auth and Role Guards. This is build to use the @flowcore/microservice library for configuration.","archived":false,"fork":false,"pushed_at":"2024-12-09T10:42:05.000Z","size":287,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-30T13:49:15.694Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/flowcore-io.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-12-30T11:33:51.000Z","updated_at":"2024-12-09T10:41:51.000Z","dependencies_parsed_at":"2024-06-17T14:10:48.360Z","dependency_job_id":"145d0f05-3434-45a2-9195-89acd5085246","html_url":"https://github.com/flowcore-io/library-nestjs-oidc-protect-ts","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"purl":"pkg:github/flowcore-io/library-nestjs-oidc-protect-ts","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flowcore-io%2Flibrary-nestjs-oidc-protect-ts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flowcore-io%2Flibrary-nestjs-oidc-protect-ts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flowcore-io%2Flibrary-nestjs-oidc-protect-ts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flowcore-io%2Flibrary-nestjs-oidc-protect-ts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/flowcore-io","download_url":"https://codeload.github.com/flowcore-io/library-nestjs-oidc-protect-ts/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flowcore-io%2Flibrary-nestjs-oidc-protect-ts/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29024557,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-02T23:43:56.073Z","status":"ssl_error","status_checked_at":"2026-02-02T23:43:49.438Z","response_time":58,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T11:34:56.874Z","updated_at":"2026-02-03T00:02:02.108Z","avatar_url":"https://github.com/flowcore-io.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"![Build](https://github.com/flowcore-io/library-nestjs-oidc-protect-ts/actions/workflows/publish.yml/badge.svg)\n\n# NestJS Oidc-Protect\n\nA NestJS OpenID Connect library that can validate a token and protect routes with Auth and Role Guards. This is build to\nuse the [@flowcore/microservice](https://www.npmjs.com/package/@flowcore/microservice) library for configuration.\n\n## Installation\n\ninstall with npm:\n\n```bash\nnpm install @flowcore/nestjs-oidc-protect @flowcore/microservice\n```\n\nor yarn:\n\n```bash\nyarn add @flowcore/nestjs-oidc-protect @flowcore/microservice\n```\n\n\u003e If you are using GraphQL, you also need to install the `@nestjs/graphql` package.\n\n## Configuration\n\nImport the OidcProtectModule into your NestJS application and configure it with your Keycloak server's details:\n\n```typescript\nimport { OidcProtectModuleBuilder, OidcProtectConfigurationSchema } from '@flowcore/nestjs-oidc-protect';\nimport { ConfigModule, ConfigFactory } from '@flowcore/microservice';\nimport { AuthGuardBuilder } from \"./auth-guard.builder\";\n\nconst config = ConfigModule.forRoot(\n  new ConfigFactory()\n    // ...other configuration schemas\n    .withSchema(OidcProtectConfigurationSchema)\n  // ...other configuration schemas,\n);\n\n@Module({\n  imports: [\n    config,\n    // ...other modules\n    new OidcProtectModuleBuilder().withConfig(config).build(),\n  ],\n  providers: [\n    ...new AuthGuardBuilder()\n      .usingRoleGuard()\n      .usingResourceGuard()\n      .build(),\n  ],\n})\nexport class AppModule {\n}\n```\n\nThis can then be configured in a service with the following environment variables\n\n| Environment Variable | Description                                                                                    |   Type   | Default Value | Required |\n|----------------------|------------------------------------------------------------------------------------------------|:--------:|---------------|:--------:|\n| OIDC_WELLKNOWN_URL   | The wellknown configuration endpoint value for the authentication server                       |  `url`   |               |    X     |\n| OIDC_RESOURCE_ID     | The resource ID configured for this service, this is required for `resource_access` validation | `string` |               |          |\n\n## Usage\n\nThe `AuthGuard` is global and will protect all routes by default. You can use the `@Public()` decorator to exclude\nroutes from the AuthGuard.\n\n```typescript\nimport { Public } from '@flowcore/nestjs-oidc-protect';\nimport { Controller, Get } from '@nestjs/common';\n\n@Controller()\nexport class AppController {\n  @Get()\n  @Public()\n  getHello(): string {\n    return 'Hello World!';\n  }\n}\n```\n\nYou can also use the `@RealmRoles()` or `@ResourceRoles` decorators to protect routes with a RoleGuard.\nThe `@RealmRoles()` and `@ResourceRoles()` decorators accepts a list of\nroles. If the user has one of the roles, the route will be accessible.\n\n```typescript\nimport { Roles } from '@flowcore/nestjs-oidc-protect';\nimport { Controller, Get } from '@nestjs/common';\n\n@Controller()\nexport class AppController {\n  @Get()\n  @RealmRoles(['admin', 'write'])\n  getHello(): string {\n    return 'Hello World!';\n  }\n\n  @ResourceRoles(['read', 'write'])\n  getHello(): string {\n    return 'Hello World!';\n  }\n}\n```\n\nResource will be swapped out for the `resourceId` configured in the `OidcProtectModule`. If no resourceId is configured,\nthe `resource_access` claim will never go through and will fail the guard.\n\n\u003e The `resource` roles refer to the `resource_access` field in the token. The `realm` roles refer to the `realm_access`\n\u003e field in the token.\n\n\u003e It is not possible to use `@RealmRoles()` and `@ResourceRoles()` on the same route.\n\nFor public endpoints like `/health` or `/metrics` you can use the built-in public endpoints are used to keep these paths\nopen. If you want to override these defaults or disable them, you can do so by configuring the `OidcProtectModule` using\nthe `OidcProtectModuleBuilder`:\n\n```typescript\n@Module({\n  imports: [\n    config,\n    // ...other modules\n    new OidcProtectModuleBuilder()\n      .withConfig(config)\n      .noPublicEndpoints() // for disabling the built-in public endpoints\n      .overridePublicEndpoints(\n        [\n          '/health',\n          '/metrics',\n          '/healthz',\n        ],\n      )\n      .build(),\n  ],\n  // ... other provider, controllers etc.\n})\nexport class AppModule {\n}\n```\n\nto optimize the performance you can specify a cache using the `withCache()` method on the `OidcProtectModuleBuilder`:\n\n```typescript\nimport { CACHE_MANAGER, CacheModule } from \"@nestjs/cache-manager\";\n\nconst cacheModule = CacheModule.register();\n\n@Module({\n  imports: [\n    config,\n    // ...other modules\n    cacheModule,\n    new OidcProtectModuleBuilder()\n      .withConfig(config)\n      .withCache(cacheModule, CACHE_MANAGER)\n      .build(),\n  ],\n  // ... other provider, controllers etc.\n})\nexport class AppModule {\n}\n```\n\n\u003e This supports the same cache managers as the `@nestjs/cache-manager` module.\n\nTo get access to the user information in the request, you can use the `@AuthenticatedUser()` decorator:\n\n```typescript\nimport { AuthenticatedUser } from '@flowcore/nestjs-oidc-protect';\nimport { Roles } from '@flowcore/nestjs-oidc-protect';\nimport { Controller, Get } from '@nestjs/common';\n\n@Controller()\nexport class AppController {\n  @Get()\n  @RealmRoles(['admin', 'write'])\n  getHello(@AuthenticatedUser() user: any /* change to match your token */): string {\n    return 'Hello World!';\n  }\n}\n```\n\n\u003e An options object can be passed to the `@AuthenticatedUser()` decorator to specify the property name of the user and if it should be optional.\n\u003e The default options are `{ required: true, storedIn: \"authenticatedUser\" }`.\n \n\u003e This also works with @Resolvers() in GraphQL.\n\nWe hope you find this library useful in your NestJS projects!\n\n## Development\n\n```bash\nyarn install\n```\n\nor with npm:\n\n```bash\nnpm install\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflowcore-io%2Flibrary-nestjs-oidc-protect-ts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflowcore-io%2Flibrary-nestjs-oidc-protect-ts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflowcore-io%2Flibrary-nestjs-oidc-protect-ts/lists"}