@antelopejs/auth-jwt
羚羊JS认证模块. 执行 @antelopejs/interface-auth 并用JSON Web Tokens来标记和验证其标志 jsonwebtoken。 。 。 .
部分 羚羊JS用于 羚羊JS 隐形物,用于您的 PostgreSQL 、 MongoDB 或 RethinkDB 数据库的开源后台.
接口
| 接口 | 文档 |
|---|---|
@antelopejs/interface-auth | 认证 |
界面页面记录 @Authentication 装饰器、自定义认证器、信使源和饼干 模块还取决于 @antelopejs/interface-api,哪个 @antelopejs/api 执行.
安装
ajs project modules add @antelopejs/auth-jwt
ajs project modules install
ajs project modules install 添加 @antelopejs/api 当项目中还没有模块执行 API 接口时.
配置
用一个共享的秘密签名 :
import { defineConfig } from "@antelopejs/interface-core/config";
export default defineConfig({
name: "my-project",
modules: {
"auth-jwt": {
source: {
type: "package",
package: "@antelopejs/auth-jwt",
version: "^2.0.0",
},
config: {
secret: "change-me",
signOptions: { expiresIn: "1h" },
verifyOptions: { ignoreExpiration: false },
},
},
},
envOverrides: {
JWT_SECRET: "modules.auth-jwt.config.secret",
},
});
当 JWT_SECRET 环境变量被设定, envOverrides 将其值写入 secret。 。 。 .
要用密钥对来签名, 请给出两个密钥文件的路径 。 模块构造时会读取两个文件.
config: {
signKey: "./keys/private.pem",
verifyKey: "./keys/public.pem",
signOptions: { algorithm: "RS256", expiresIn: "1h" },
verifyOptions: { algorithms: ["RS256"] },
},
| 密钥 | 说明 |
|---|---|
secret | 共同的秘密, 标志和验证的标志。 胜过 signKey 财务报告和审定财务报表 verifyKey。 。 。 . |
signKey | 私钥文件的路径 。 模块使用它与 verifyKey 何时 secret 未设置. |
verifyKey | 公钥文件的路径 . |
signOptions | 默认 jsonwebtoken 签名选项编辑 expiresIn 财务报告和审定财务报表 notBefore 那个 SignRaw 或者说 SignServerResponse 接收它们. |
verifyOptions | 默认 jsonwebtoken 校验选项选项 ValidateRaw 超过他们. |
模块拒绝启动 secret 或两者兼有 signKey 财务报告和审定财务报表 verifyKey投掷 Invalid JWT config。 。 。 . ajs project modules add 写入 secret: "your-secret-key"在部署前替换它.
快开始
在服务路线的模块中,添加两个接口.
pnpm add @antelopejs/interface-api @antelopejs/interface-auth
以饼干的形式发出一个令牌,然后在受保护的路线上读回.
import {
Context,
Controller,
Get,
Post,
type RequestContext,
} from "@antelopejs/interface-api";
import { Authentication, SignServerResponse } from "@antelopejs/interface-auth";
interface Session {
userId: string;
}
export class SessionController extends Controller("/session") {
@Post("/login")
async login(@Context() context: RequestContext) {
await SignServerResponse(
context.rawResponse,
{ userId: "42" },
{ expiresIn: "1h" },
{ httpOnly: true, path: "/" },
);
return { ok: true };
}
@Get("/me")
me(@Authentication() session: Session) {
return session;
}
}
GET /session/me 从 x-antelopejs-auth 标题,或来自 ANTELOPEJS_AUTH 当信头缺席时做曲奇. 它会回答的 401 当它不见了。 它会回答的 403 当符号无效或过期,或者其有效载荷既不是字符串,也不是JSON对象,如数组或数字.
文档
兼容性
| 软件包 | 支助 | 测试过 |
|---|---|---|
@antelopejs/interface-api | >=0.2.0 <1.0.0 | 0.2.0 |
@antelopejs/interface-auth | >=0.2.0 <0.3.0 | 0.2.0 |
@antelopejs/interface-core | >=0.4.0 <1.0.0 | 0.4.0 |
生成的表格列出接口软件包,并将此模块负载范围从 package.json。 。 。 .
捐款
pnpm install
pnpm dev # run the playground/ project in watch mode
pnpm lint
pnpm format:check
pnpm knip
pnpm test # build, then ajs module test
读取 捐款准则 在打开拉出请求之前.