{"id":14978648,"url":"https://github.com/anonrig/nestjs-keycloak-admin","last_synced_at":"2026-01-12T02:44:44.616Z","repository":{"id":37527247,"uuid":"272422619","full_name":"anonrig/nestjs-keycloak-admin","owner":"anonrig","description":"Keycloak client and admin provider for Nest.js applications with built-in User Managed Access (UMA) and ACL support.","archived":false,"fork":false,"pushed_at":"2023-06-29T02:54:36.000Z","size":2297,"stargazers_count":186,"open_issues_count":8,"forks_count":26,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-29T02:07:53.134Z","etag":null,"topics":["javascript","keycloak","nest","nestjs","nodejs","typescript"],"latest_commit_sha":null,"homepage":"https://npmjs.com/package/nestjs-keycloak-admin","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/anonrig.png","metadata":{"files":{"readme":"readme.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-06-15T11:38:42.000Z","updated_at":"2025-02-12T09:22:13.000Z","dependencies_parsed_at":"2024-01-12T20:03:47.006Z","dependency_job_id":null,"html_url":"https://github.com/anonrig/nestjs-keycloak-admin","commit_stats":null,"previous_names":["relevantfruit/nestjs-keycloak-admin"],"tags_count":70,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anonrig%2Fnestjs-keycloak-admin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anonrig%2Fnestjs-keycloak-admin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anonrig%2Fnestjs-keycloak-admin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anonrig%2Fnestjs-keycloak-admin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anonrig","download_url":"https://codeload.github.com/anonrig/nestjs-keycloak-admin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247280272,"owners_count":20912967,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["javascript","keycloak","nest","nestjs","nodejs","typescript"],"created_at":"2024-09-24T13:58:07.458Z","updated_at":"2026-01-12T02:44:44.578Z","avatar_url":"https://github.com/anonrig.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Keycloak for Nest.js\n\n## Installation\n\nInstall using `npm i --save nestjs-keycloak-admin` or `pnpm add nestjs-keycloak-admin`\n\n## ESM restriction\n\n- Due to `@keycloak/keycloak-admin-client` package, `nestjs-keycloak-admin` can't support CommonJS at the moment. \nThe team behind `keycloak-admin-client` made the decision to have a breaking change and support CommonJS.\nPlease refer to [this Github issue](https://github.com/keycloak/keycloak-nodejs-admin-client/issues/523) for more information about their decision-making process.\n- You need to switch to ESM to run this package without any issues. Please refer to [this Github gist](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c)\nfor more information.\n\n## Initialize KeycloakModule\n\nThen on your app.module.ts\n\n```typescript\nimport { Module } from '@nestjs/common';\nimport { AppController } from './app.controller';\nimport KeycloakModule, { AuthGuard, ResourceGuard, RoleGuard } from 'nestjs-keycloak-admin'\nimport { APP_GUARD } from '@nestjs/core';\n\n@Module({\n  imports: [\n    KeycloakModule.register({\n      baseUrl: '',\n      realmName: '',\n      clientSecret: '',\n      clientId: ''\n    })\n  ],\n  controllers: [AppController],\n  providers: [\n    { provide: APP_GUARD, useClass: AuthGuard },\n    { provide: APP_GUARD, useClass: ResourceGuard },\n    { provide: APP_GUARD, useClass: RoleGuard },\n  ],\n})\nexport class AppModule {}\n```\n\n### Resource Management using User Managed Access (UMA)\n\nBy default nestjs-keycloak-admin supports User Managed Access for managing your resources.\n\n```typescript\nimport { Controller, Get, Request, ExecutionContext, Post } from '@nestjs/common'\nimport {\n  DefineResource,\n  Public,\n  KeycloakService,\n  FetchResources,\n  Resource,\n  DefineScope,\n  DefineResourceEnforcer,\n  UMAResource,\n  Scope,\n} from 'nestjs-keycloak-admin'\n\n@Controller('/organization')\n@DefineResource('organization')\nexport class AppController {\n  constructor(private readonly keycloak: KeycloakService) {}\n\n  @Get('/hello')\n  @Public()\n  sayHello(): string {\n    return 'life is short.'\n  }\n\n  @Get('/')\n  @FetchResources()\n  findAll(@Request() req: any): Resource[] {\n    return req.resources as Resource[]\n  }\n\n  @Get('/:slug')\n  @DefineScope('read')\n  @EnforceResource({\n    def: ({ params }) =\u003e params.slug,\n    param: 'slug',\n  })\n  findBySlug(@Request() req: any): Resource {\n    return req.resource as Resource\n  }\n\n  @Post('/')\n  @DefineScope('create')\n  async create(@Request() req: any): Promise\u003cResource\u003e {\n    let resource = new Resource({\n      name: 'resource',\n      displayName: 'My Resource',\n    } as UMAResource)\n      .setOwner(req.user._id)\n      .setScopes([new Scope('organization:read'), new Scope('organization:write')])\n      .setType('urn:resource-server:type:organization')\n      .setUris(['/organization/123'])\n      .setAttributes({\n        valid: true,\n        types: ['customer', 'any'],\n      })\n\n    resource = await this.keycloak.resourceManager.create(resource)\n\n    // create organization on your resource server and add link to resource.id, to access it later.\n\n    return resource\n  }\n}\n```\n\n## Decorators\n\n```typescript\n@Get('/hello')\n@Roles({roles: ['realm:admin'], mode: RoleMatchingMode.ANY})\nsayHello(@User() user: KeycloakUser, @AccessToken() accessToken): string {\n  return `life is short. -${user.email}/${accessToken}`\n}\n```\n\nHere is the decorators you can use in your controllers.\n\n| Decorator        | Description                                                                                               |\n|------------------|-----------------------------------------------------------------------------------------------------------|\n| @User            | Retrieves the current Keycloak logged-in user. (must be per method, unless controller is request scoped.) |\n| @AccessToken     | Retrieves the current access token. (must be per method, unless controller is request scoped.)            |\n| @DefineResource  | Define the keycloak application resource name.                                                            |\n| @DefineScope     | Define the keycloak resource scope (ex: 'create', 'read', 'update', 'delete')                             |\n| @EnforceResource |                                                                                                           |\n| @FetchResources  |                                                                                                           |\n| @Public          | Allow any user to use the route.                                                                          |\n| @Roles           | Keycloak realm/application roles. Prefix any realm-level roles with \"realm:\" (i.e realm:admin)            |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanonrig%2Fnestjs-keycloak-admin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanonrig%2Fnestjs-keycloak-admin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanonrig%2Fnestjs-keycloak-admin/lists"}