@exlinc/keycloak-passport

中文文档
1.0.2发布于 2018/07/23Apache-2.0CommonJS近 30 天下载 1.6万

Flexible Keycloak Passport strategy (with multi-realm support) by EXL Inc.

原始使用文档
文档目录7 节
  1. Why? Hasn't this already been done?
  2. Usage
  3. Install
  4. Import
  5. Initialize
  6. Routes
  7. Contributing/feedback

Keycloak Passport Strategy - oAuth2/OIDC

This library offers a production-ready and maintained Keycloak Passport connector that offers the following key features:

  • Use multiple realms in the same application (multi-tenancy)

  • Use with oAuth2/Open ID Connect 'clients' in keycloak

  • Fetch users' data from keycloak automatically via the JSON API

Why? Hasn't this already been done?

To a certain extent, yes. There are about 3 to 4 repos that brand themselves as 'Keycloak Passport', yet not a single one of them is actively maintained and most of them are either completely empty, don't allow using multiple realms, only implement part of the protocol, and/or don't fetch the user's data from Keycloak. There also exists a dedicated NodeJS connector by the Keycloak project itself, however, it is unusable if you are seeking to have Keycloak as 'yet another' passport strategy in your app. This project fills that gap.

Usage

Install

npm install @exlinc/keycloak-passport

Import

import KeycloakStrategy from "@exlinc/keycloak-passport";

Initialize

// Register the strategy with passport
passport.use(
  "keycloak",
  new KeycloakStrategy(
    {
      host: process.env.KEYCLOAK_HOST,
      realm: process.env.KEYCLOAK_REALM,
      clientID: process.env.KEYCLOAK_CLIENT_ID,
      clientSecret: process.env.KEYCLOAK_CLIENT_SECRET,
      callbackURL: `/api${AUTH_KEYCLOAK_CALLBACK}`
    },
    (accessToken, refreshToken, profile, done) => {
      // This is called after a successful authentication has been completed
      // Here's a sample of what you can then do, i.e., write the user to your DB
      User.findOrCreate({ email: profile.email }, (err, user) => {
        assert.ifError(err);
        user.keycloakId = profile.keycloakId;
        user.imageUrl = profile.avatar;
        user.name = profile.name;
        user.save((err, savedUser) => done(err, savedUser));
      });
    }
  )
);

Routes

router.get(
  routes.AUTH_KEYCLOAK,
  passport.authenticate("keycloak", DEFAULT_PASSPORT_OPTIONS)
);
router.get(
  routes.AUTH_KEYCLOAK_CALLBACK,
  passport.authenticate("keycloak", DEFAULT_PASSPORT_OPTIONS),
  AuthController.keyCloakSuccess
);

Contributing/feedback

All forms of contribution are welcome via Issues and Pull-requests to this repo

版本与发布记录

@exlinc/keycloak-passport 共发布 3 个版本,首次发布于 2018/07/23;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.2latest2018/07/238 年前
1.0.12018/07/238 年前
1.0.02018/07/238 年前

关于 @exlinc/keycloak-passport 的常见问题

@exlinc/keycloak-passport 是什么?

Flexible Keycloak Passport strategy (with multi-realm support) by EXL Inc.

如何安装 @exlinc/keycloak-passport?

在项目目录运行 npm install @exlinc/keycloak-passport,或使用 pnpm add / yarn add / bun add。

@exlinc/keycloak-passport 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/exlinc__keycloak-passport 是否存在。

@exlinc/keycloak-passport 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@exlinc/keycloak-passport 可以商用吗?

@exlinc/keycloak-passport 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@exlinc/keycloak-passport 的下载量有多少?

根据 npm 官方下载统计,@exlinc/keycloak-passport 在 2026/10/14 – 2026/11/12 这 30 天内被下载 16,040 次,日均约 535 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@exlinc/keycloak-passport 还在维护吗?

最新版本 1.0.2 发布于 2018/07/23(8 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/07/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。