@qldc/auth

中文文档
0.1.4发布于 2026/11/02MITTypeScriptES Module

Verify QLDC workforce and Entra External ID access tokens

原始使用文档
文档目录6 节
  1. Install
  2. Usage
  3. Debug mode
  4. Advanced types
  5. Publish (maintainers)
  6. Scripts

@qldc/auth

Verify QLDC workforce and Entra External ID access tokens in a Node API.

The package checks signature, an explicit issuer, audience, expiry, and oid. An External ID token is public and is rejected when its email is on the caller's work-email domain list. A workforce token is staff. There is no default issuer: an app passes the issuers it trusts, and a blank audience, issuer, or work-email list fails at startup.

Each SPA keeps its own MSAL setup. km-auth-extensions keeps its own check of Microsoft's extension-service token.

Install

npm install @qldc/auth

Local sibling checkout:

{
  "dependencies": {
    "@qldc/auth": "file:../km-auth"
  }
}

Usage

import {
  QLDC_WORK_EMAIL_DOMAINS,
  createExternalIdVerifier,
  createWorkforceVerifier,
  externalIdIssuer,
  workforceIssuers,
} from "@qldc/auth";

const tenantId = process.env.ENTRA_TENANT_ID!;
const audience = process.env.ENTRA_AUDIENCE!;

const verifyWorkforce = createWorkforceVerifier({
  audience,
  ...workforceIssuers(tenantId),
  debug: process.env.NODE_ENV === "development", // optional: enable error logging
});

const verifyExternalId = createExternalIdVerifier({
  audience: process.env.EXTERNAL_ID_AUDIENCE!,
  issuer: externalIdIssuer(process.env.EXTERNAL_ID_TENANT_ID!),
  workEmailDomains: QLDC_WORK_EMAIL_DOMAINS,
  debug: process.env.NODE_ENV === "development", // optional: enable error logging
});

const staff = await verifyWorkforce(token); // AuthPrincipal | null
const pub = await verifyExternalId(token);

A workforce-only app creates createWorkforceVerifier and does not create the External ID verifier. workforceIssuers includes the v1 issuer (https://sts.windows.net/{tenant}/). Omit v1 when the app does not accept those tokens.

externalIdIssuer uses https://{tenant}.ciamlogin.com/{tenant}/v2.0. The sign-in host signin.qldc.govt.nz is not the token issuer.

Audience accepts the GUID and api://{guid} form of the same app registration. Other api:// values must match the token exactly.

QLDC_WORK_EMAIL_DOMAINS is qldc.govt.nz, qlcht.org.nz, and alpinewaters.co.nz. The match includes subdomains. The verifier uses that list only when the caller passes it.

A verified principal:

FieldMeaning
capabilitystaff or public
oidEntra object id. Key public user data by this.
emailemail, upn, preferred_username, or unique_name
roles, scpWorkforce app roles and scopes, when present

A token that fails the check returns null.

Debug mode

Enable debug: true in development to log verification failures to the console:

const verifyWorkforce = createWorkforceVerifier({
  audience,
  ...workforceIssuers(tenantId),
  debug: true,
});

Debug logs include:

  • Invalid JWT format errors
  • Token signature failures
  • Issuer/audience mismatches
  • Expiration issues
  • Work email rejections (External ID only)

Advanced types

For observability and monitoring, the package exports additional types:

import type {
  VerificationResult,
  VerificationFailureReason,
  TokenVerifierWithResult,
} from "@qldc/auth";

These types support building custom verifiers with structured error responses rather than just null.

Publish (maintainers)

CI uses npm Trusted Publishing (OIDC) — no long-lived NPM_TOKEN.

  1. On npmjs.com → package → Settings → Trusted Publisher: wire the GitHub org/repo and publish.yml
  2. Bump version and push a tag; Actions publishes:
npm version patch   # or minor / major
git push --follow-tags

Scripts

npm test
npm run build
npm run typecheck

版本与发布记录

@qldc/auth 共发布 4 个版本,首次发布于 2026/10/29;过去 12 个月发布了 4 个版本。

版本发布日期距今
0.1.4latest2026/11/021 天前
0.1.22026/10/295 天前
0.1.02026/10/295 天前
0.0.0-stage2026/10/295 天前

关于 @qldc/auth 的常见问题

@qldc/auth 是什么?

验证QLDC员工和Entra External ID访问令牌

如何安装 @qldc/auth?

在项目目录运行 npm install @qldc/auth,或使用 pnpm add / yarn add / bun add。

@qldc/auth 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@qldc/auth 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@qldc/auth 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@qldc/auth 可以商用吗?

@qldc/auth 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@qldc/auth 还在维护吗?

最新版本 0.1.4 发布于 2026/11/02(1 天前);至今共发布 4 个版本,其中过去 12 个月发布了 4 个;首次发布于 2026/10/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。