@authup/specs

中文文档
1.0.0-beta.73发布于 2026/11/02Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 4431

Constants, types and error classes for OAuth 2.0, OpenID Connect, JWT and JWK, as Authup uses them.

原始使用文档
文档目录9 节
  1. Where it fits
  2. Installation
  3. Usage
  4. Token payloads and grants
  5. Scopes
  6. Errors
  7. API
  8. Notes
  9. License

Authup logo

@authup/specs

Constants, types and error classes for OAuth 2.0, OpenID Connect, JWT and JWK, as Authup uses them.
The grant names, token payload shape, error codes and scope helpers both sides of an Authup login share.

npm version CI License: Apache-2.0

Installation · Usage · API · Documentation


Where it fits

@authup/specs is a foundation package: @authup/core-kit, @authup/core-http-kit, the server and the server adapters all take their protocol vocabulary from it. You install it directly when you read Authup tokens or protocol errors yourself, for example to type a decoded access token as OAuth2TokenPayload or to compare an error against OAuth2ErrorCode.INVALID_GRANT. It is not a general OAuth2 library: it carries what Authup issues and answers, including Authup's own claims such as realm_name and sub_kind.

Installation

npm install @authup/specs

ESM only. Node.js >=26.0.0, or a browser bundler. There are no peer dependencies.

Usage

Token payloads and grants

import type { OAuth2TokenGrantResponse, OAuth2TokenPayload } from '@authup/specs';
import { OAuth2SubKind, OAuth2TokenGrant, OAuth2TokenKind } from '@authup/specs';

function isUserAccessToken(payload: OAuth2TokenPayload) : boolean {
    return payload.kind === OAuth2TokenKind.ACCESS &&
        payload.sub_kind === OAuth2SubKind.USER;
}

OAuth2TokenGrant.DEVICE_CODE; // 'urn:ietf:params:oauth:grant-type:device_code'

OAuth2TokenPayload is the JWT claim set plus Authup's claims: kind, sub_kind, realm_id, realm_name, client_id, scope, session_id / sid, amr, acr, cnf, realm_access and global_access. OAuth2TokenGrantResponse is the /token answer.

Scopes

import { hasOAuth2Scopes, mergeOAuth2Scopes } from '@authup/specs';

hasOAuth2Scopes('openid Email', ['email']); // true
mergeOAuth2Scopes('openid email', ['email', 'roles']); // 'openid email roles'
  • hasOAuth2Scopes(granted, required) lower-cases both sides, accepts a space- or comma-separated string or an array, and returns false when nothing is granted.
  • deserializeOAuth2Scope and unwrapOAuth2Scope split and lower-case; splitOAuth2Scope and mergeOAuth2Scopes split and dedupe but keep the case.

Errors

The error classes extend AuthupError from @authup/errors and carry the OAuth2 error code under data.error:

import { OAuth2ErrorCode, OAuth2GrantError, isJWTError, isOAuth2Error } from '@authup/specs';

const error = OAuth2GrantError.invalid('refresh token is unknown');
error.data?.error === OAuth2ErrorCode.INVALID_GRANT; // true

function describe(e: unknown) : string {
    if (isJWTError(e)) return 'token rejected';
    if (isOAuth2Error(e)) return e.message;
    return 'unknown';
}

The is* guards match an instance as well as an error rehydrated from JSON, since they read the serialized marker chain rather than the prototype. isJWTErrorCode and isJWKErrorCode test a bare code string.

API

ExportKindPurpose
OAuth2TokenGrant, OAuth2TokenKind, OAuth2SubKindenumGrant types, token kinds (access_token, refresh_token, id_token, mfa_token, ...), subject kinds
OAuth2AuthorizationResponseType, OAuth2AuthorizationCodeChallengeMethod, OAuth2AuthorizationPrompt, OAuth2UIColorModeenum/authorize parameters
OAuth2AuthenticationMethodReference, OAuth2AuthenticationContextClassenumamr (pwd, otp, ext) and acr (urn:authup:pwd, urn:authup:mfa) values
OAuth2ErrorCodeenumRFC 6749, OIDC and RFC 8628 error codes
OAuth2TokenGrantResponse, OAuth2TokenPayload, OAuth2TokenIntrospectionResponse, OAuth2TokenPermission, OAuth2JsonWebKeytypeToken endpoint answers and token contents
OAuth2Error and OAuth2RequestError, OAuth2ClientError, OAuth2ClientUnauthorizedError, OAuth2GrantError, OAuth2GrantTypeError, OAuth2ScopeError, OAuth2ResponseTypeError, OAuth2AccessDeniedError, OAuth2LoginRequiredError, OAuth2MfaRequiredError, OAuth2DeviceAuthorizationError, OAuth2ServerErrorclassProtocol errors with static factories (OAuth2GrantError.invalid(), OAuth2DeviceAuthorizationError.slowDown(), ...)
isOAuth2Error and an isOAuth2*Error guard per class (none for OAuth2LoginRequiredError)functionError guards
serializeOAuth2Scope, deserializeOAuth2Scope, unwrapOAuth2Scope, splitOAuth2Scope, mergeOAuth2Scopes, hasOAuth2ScopesfunctionScope string handling
JWTAlgorithmenumHS*, RS*, ES*, PS* (256, 384, 512)
JWTHeader, JWTClaimsinterfaceJWT header and registered claims
JWTError, isJWTError, isJWTErrorCodeclass, functionToken errors (expired_token, inactive_token, invalid_token)
getJWTClaimByPatternfunctionRead claims by a path pattern, optionally matched against a value or regex
JWKType, JWKUseenumKey types (oct, rsa, ec) and uses (sig, enc)
JWKError, isJWKError, isJWKErrorCodeclass, functionKey errors
OpenIDProviderMetadata, OpenIDTokenPayload, OpenIDClaimstypeThe discovery document, the id_token payload and the OIDC Core standard claims
isOpenIDProviderMetadatafunctionGuard for a discovery document

Notes

  • JWKType is Authup's key type, not the JWK kty. Its values are lower case (rsa, ec, oct); RFC 7517 spells kty as RSA and EC.
  • An absent OpenID claim is an omitted key. OpenIDClaims types every claim as optional, never null, and Authup leaves a claim out rather than sending null.

License

Made with 💚

Published under the Apache 2.0 License.

版本与发布记录

@authup/specs 共发布 48 个版本,首次发布于 2025/01/21;过去 12 个月发布了 44 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.0.0-beta.73latest2026/11/025 天前
1.0.0-beta.722026/10/2711 天前
1.0.0-beta.712026/10/2018 天前
1.0.0-beta.702026/10/1919 天前
1.0.0-beta.692026/10/1424 天前
1.0.0-beta.682026/09/251 个月前

关于 @authup/specs 的常见问题

@authup/specs 是什么?

Constants, types and error classes for OAuth 2.0, OpenID Connect, JWT and JWK, as Authup uses them.

如何安装 @authup/specs?

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

@authup/specs 支持 TypeScript 吗?

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

@authup/specs 支持 ESM 还是 CommonJS?

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

@authup/specs 需要什么版本的 Node.js?

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

@authup/specs 可以商用吗?

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

@authup/specs 的下载量有多少?

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

@authup/specs 还在维护吗?

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