{"id":19737987,"url":"https://github.com/devsu/keycloak-nodejs-multirealm","last_synced_at":"2025-04-30T05:30:56.367Z","repository":{"id":29242192,"uuid":"118790297","full_name":"devsu/keycloak-nodejs-multirealm","owner":"devsu","description":"Keycloak NodeJS Multi Realm Adapter","archived":false,"fork":false,"pushed_at":"2023-01-06T01:59:21.000Z","size":617,"stargazers_count":30,"open_issues_count":11,"forks_count":11,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-08-09T02:36:55.248Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/devsu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-24T16:20:50.000Z","updated_at":"2024-06-26T00:48:27.000Z","dependencies_parsed_at":"2023-01-14T14:28:46.708Z","dependency_job_id":null,"html_url":"https://github.com/devsu/keycloak-nodejs-multirealm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devsu%2Fkeycloak-nodejs-multirealm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devsu%2Fkeycloak-nodejs-multirealm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devsu%2Fkeycloak-nodejs-multirealm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devsu%2Fkeycloak-nodejs-multirealm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devsu","download_url":"https://codeload.github.com/devsu/keycloak-nodejs-multirealm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224197880,"owners_count":17271999,"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":[],"created_at":"2024-11-12T01:13:00.616Z","updated_at":"2024-11-12T01:13:01.091Z","avatar_url":"https://github.com/devsu.png","language":"JavaScript","readme":"# Keycloak Node.js Adapter With Support for multiple realms\n\nExpress Middleware that allows authentication / authorization using [Keycloak](http://keycloak.org/). It's similar to the [official adapter](https://github.com/keycloak/keycloak-nodejs-connect), but it allows the application to work with **any** keycloak realm.\n\nUses the official [Keycloak Node.js Adapter](https://github.com/keycloak/keycloak-nodejs-connect) under the hood.\n\n## Installation\n\n`npm install keycloak-connect keycloak-connect-multirealm`\n\nor\n\n`yarn add keycloak-connect keycloak-connect-multirealm`\n\nStarting from version 1.1.0, `keycloak-connect` is required as a peer dependency. That way you can update `keycloak-connect` module independently from this module.\n\n## Usage\n\nThe usage is very similar to the official module:\n\n```javascript\n\nconst express = require('express');\nconst KeycloakMultirealm = require('keycloak-connect-multirealm');\n\nconst app = express();\n\nconst config = {};\n\nconst keycloakConfig = {\n  'auth-server-url': 'http://localhost:8080/auth',\n  'bearer-only': true,\n  'ssl-required': 'external',\n  'resource': 'my-node-app',\n};\n\n// Instantiate the class just as the official module. If no keycloakConfig\n// is provided, it will read the configuration from keycloak.json file.\n\nconst keycloak = new KeycloakMultirealm(config, keycloakConfig);\n\n// add the middleware\n\napp.use(keycloak.middleware());\n\n// protect any endpoint\n\napp.get('/files', keycloak.protect(), filesEndpointHandler);\n\n```\n\nAs you can see, you don't need to set the `realm` in your keycloak configuration. Any of the realms will be accepted.\n\nYou can see the official [documentation](http://www.keycloak.org/docs/latest/securing_apps/index.html#_nodejs_adapter) for more examples and options.\n\n### Implementing getRealmNameFromRequest\n\nFor requests without token to work (anonymous requests), you must implement the `getRealmNameFromRequest` method. This is required for admin and logout endpoints to work.\n\nThe implementation will depend on your specific use case:\n\n```\nkeycloak.getRealmNameFromRequest = (req) =\u003e {\n  // for example, you could get the realmName from the path\n  return req.originalUrl.split('/')[0];\n};\n\nkeycloak.getRealmNameFromRequest = (req) =\u003e {\n  // or from the host\n  return req.get('host').split('.')[0];\n};\n\nkeycloak.getRealmNameFromRequest = (req) =\u003e {\n  // or from a query string\n  return req.query.realm;\n};\n```\n\nObviously, for admin endpoints to work, you might need to change the admin URL in the client settings in Keycloak.\n\n## How it works\n\nIf the request contains a valid token, it tries to get the realm name from the token.\nIf the request doesn't contain a valid token, it tries to get the realm name from the `getRealmNameFromRequest` method. (Which by default is empty, and should be implemented if needed)\n\nThen, based on the realm name, uses under the hood the official keycloak-connect module.\n\nWhen found, this middleware adds the realm name to the request: `req.kauth.realm`.\n\n## Status\n\nTested on bearer-only applications. If `getRealmNameFromRequest` is properly implemented, it *should* work for public clients as well, but I haven't tested it.\n\n## License and Credits\n\nCopyright 2018, by the [NodeJS Team](https://devsu.com) at Devsu\n\nApache 2.0 License\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevsu%2Fkeycloak-nodejs-multirealm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevsu%2Fkeycloak-nodejs-multirealm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevsu%2Fkeycloak-nodejs-multirealm/lists"}