@ fastify/ basic-auth (英语)
很简单 基本认证 快速化插件 .
安装
npm i @fastify/basic-auth
兼容性
| 插件版本 | 快速化版本 |
|---|---|
>=6.x | ^5.x |
^4.x | ^4.x |
>=1.x <4.x | ^3.x |
^0.x | ^2.x |
^0.x | ^1.x |
请注意,如果一个快取版本没有支持,那么该插件的相应版本也一样 上表列出. 见 快速化 LTS 政策 更多细节.
使用量
此插件以一个 basicAuth 函数,可在路由处理器之前在任意钩子内使用,或与 @fastify/auth。 。 。 .
const fastify = require('fastify')()
const authenticate = {realm: 'Westeros'}
fastify.register(require('@fastify/basic-auth'), { validate, authenticate })
// `this` inside validate is `fastify`
function validate (username, password, req, reply, done) {
if (username === 'Tyrion' && password === 'wine') {
done()
} else {
done(new Error('Winter is coming'))
}
}
fastify.after(() => {
fastify.addHook('onRequest', fastify.basicAuth)
fastify.get('/', (req, reply) => {
reply.send({ hello: 'world' })
})
})
承诺和 自动/等待 也支持他们!
const fastify = require('fastify')()
const authenticate = {realm: 'Westeros'}
fastify.register(require('@fastify/basic-auth'), { validate, authenticate })
async function validate (username, password, req, reply) {
if (username !== 'Tyrion' || password !== 'wine') {
return new Error('Winter is coming')
}
}
使用方式 onRequest数字 :
const fastify = require('fastify')()
const authenticate = {realm: 'Westeros'}
fastify.register(require('@fastify/basic-auth'), { validate, authenticate })
async function validate (username, password, req, reply) {
if (username !== 'Tyrion' || password !== 'wine') {
return new Error('Winter is coming')
}
}
fastify.after(() => {
fastify.route({
method: 'GET',
url: '/',
onRequest: fastify.basicAuth,
handler: async (req, reply) => {
return { hello: 'world' }
}
})
})
使用方式 @fastify/auth数字 :
const fastify = require('fastify')()
const authenticate = {realm: 'Westeros'}
fastify.register(require('@fastify/auth'))
fastify.register(require('@fastify/basic-auth'), { validate, authenticate })
async function validate (username, password, req, reply) {
if (username !== 'Tyrion' || password !== 'wine') {
return new Error('Winter is coming')
}
}
fastify.after(() => {
// use preHandler to authenticate all the routes
fastify.addHook('preHandler', fastify.auth([fastify.basicAuth]))
fastify.route({
method: 'GET',
url: '/',
// use onRequest to authenticate just this one
onRequest: fastify.auth([fastify.basicAuth]),
handler: async (req, reply) => {
return { hello: 'world' }
}
})
})
自定义错误处理器
在认证失败时, @fastify/basic-auth 调用快捷键
通用错误
处理器 错误
组合 err.statusCode 改为 401。 。 。 .
适当 401 错误 :
fastify.setErrorHandler(function (err, req, reply) {
if (err.statusCode === 401) {
// This was unauthorized! Display the correct page/message
reply.code(401).send({ was: 'unauthorized' })
return
}
reply.send(err)
})
选项
utf8 (可选,默认:真实)
非美国ASCII字符的用户名或密码可能引起问题
除非双方就编码方案达成协议。 设置 utf8 改为
true 发送“ charset” 参数以选择“ UTF-8”, 增加
客户可能使用该编码.
strictCredentials (可选,默认:真实)
设置 strictCredentials 允许在
授权标题的开头、中间和结尾.
这是一种倒置选项,用于确保行为与 @fastify/basic-auth
<=-5.x.
validate (必须)
这个 validate 每次请求时都会调用函数
并被传递 username, (中文(简体) ), password, (中文(简体) ), req,以及 reply
参数,按此顺序。 可选的第五个参数, done,可以是
用于在无论据的情况下请求有效
或无效的请求,如果 Error 对象。 或者,
页:1 validate 函数可以返回承诺, 解析是否有效
请求和拒绝无效。 这也可以使用
a 一个 async/await 函数,并显示无效请求.
例子见上文代码.
authenticate < Boolean > 对象 > (可选,默认:虚假)
这个 authenticate 选项 WWW-Authenticate 标题 并可以设置 realm 数值.
这可以触发客户端认证接口,例如浏览器认证对话框.
设置 authenticate 改为 true 添加页眉 WWW-Authenticate: Basic时 false,不添加标题(默认).
何时 proxyMode 实值 true 它会使用 Proxy-Authenticate 取而代之的是标题.
fastify.register(require('@fastify/basic-auth'), {
validate,
authenticate: true // WWW-Authenticate: Basic
})
fastify.register(require('@fastify/basic-auth'), {
validate,
proxyMode: true,
authenticate: true // Proxy-Authenticate: Basic
})
fastify.register(require('@fastify/basic-auth'), {
validate,
authenticate: false // no authenticate header, same as omitting authenticate option
})
这个 authenticate 选项 realm 键作为对象提供时.
如果 realm 键,将附加在标题值上:
fastify.register(require('@fastify/basic-auth'), {
validate,
authenticate: {realm: 'example'} // WWW-Authenticate: Basic realm="example"
})
这个 realm 键也可以是函数 :
fastify.register(require('@fastify/basic-auth'), {
validate,
authenticate: {
realm(req) {
return 'example' // WWW-Authenticate: Basic realm="example"
}
}
})
这个 authenticate 对象可以包括可选 header 键来自定义信头名称,替换默认 WWW-Authenticate数字 :
fastify.register(require('@fastify/basic-auth'), {
validate,
authenticate: {
header: 'Proxy-Authenticate' // Proxy-Authenticate: Basic
}
})
proxyMode 布尔( 可选, 默认: 假)
设置 proxyMode 改为 true 将执行插件 HTTP 代理认证,而不是资源认证。 换言之,插件将:
- 读取
Proxy-Authorization标题而不是Authorization - 使用情况
407响应状态代码而不是401以指示缺少或无效的证书 - 使用该
Proxy-Authenticate标题而不是WWW-Authenticate若为authenticate选项已设定
header 字符串( 可选)
这个 header 选项指定要从认证中获取证书的页眉名称。 未指定则默认为 Authorization 或者说 Proxy-Authorization (根据 proxyMode 选项)
fastify.register(require('@fastify/basic-auth'), {
validate,
header: 'x-forwarded-authorization'
})
许可证
许可证根据 麻省理工学院。 。 。 .