@ csrf- armor/ 核心
CSRF Armor背后的框架-不可知引擎. 它没有依赖性,只使用Web API,因此在Node.js和边缘跑道运行.
使用Next.js,快递还是Nuxt? 安装适配器; 包含此软件包 :
@csrf-armor/nextjs • 妇女
@csrf-armor/express • 妇女
@csrf-armor/nuxt
直接使用核心支持另一个框架,或为低等符号帮助者.
安装
npm install @csrf-armor/core
使用量
执行 CsrfAdapter 您的框架,然后创建一个保护者:
import { createCsrfProtection } from '@csrf-armor/core';
const csrf = createCsrfProtection(new MyFrameworkAdapter(), {
secret: process.env.CSRF_SECRET,
});
const result = await csrf.protect(request, response);
if (!result.success) {
// Respond with 403. result.reason says why.
}
// Otherwise continue with result.response, which carries the token cookie and header.
这个 适配器指南 覆盖接口并包含一个完整的快感适配器.
调用助手
import { generateNonce, generateSignedToken, parseSignedToken } from '@csrf-armor/core';
const token = await generateSignedToken(secret, 3600); // "{expiry}.{nonce}.{signature}"
const { exp } = await parseSignedToken(token, secret); // throws TokenExpiredError / TokenInvalidError
const nonce = generateNonce(32); // 64 hex characters
这只处理密码工作 它们不绑定饼干或会话的代币;使用 createCsrfProtection 请求保护.
文档
许可证
麻省理工学院 萨缪尔斯