gpt4 book ai didi

node.js - @hapi/hawk 的 Hawk 身份验证错误

转载 作者:太空宇宙 更新时间:2023-11-04 01:27:21 24 4
gpt4 key购买 nike

我正在尝试使用 Hawk 方案和 Hapi 验证基本示例,但 hawk 插件失败,因为它尝试访问不存在的 payload 属性:

错误:

Server started listening on http://localhost:3000
Debug: internal, implementation, error
TypeError: Cannot read property 'payload' of undefined
at Object.authenticate (D:\TEST\node\sample3\node_modules\@hapi\hawk\lib\plugin.js:45:45)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:189:7)

相关Hawk插件code错误产生的位置:

...
if (request.route.settings.auth.payload) {
request.events.once('peek', (chunk) => {
...

服务器代码:

    const Hapi = require('@hapi/hapi');
const Hawk = require('@hapi/hawk');

const credentials = {
John: {
key: 'secret',
algorithm: 'sha256'
}
};

const getCredentialsFunc = function (id) {
return credentials[id];
};

const start = async () => {
const server = Hapi.server({ port: 3000, host: 'localhost' });

await server.register(Hawk);

server.auth.strategy('default', 'hawk', { getCredentialsFunc });
server.auth.default('default');

server.route({
method: 'GET',
path: '/',
handler: function (request, h) {
return 'Welcome';
}
});

await server.start();

console.log('Server started listening on %s', server.info.uri);
};

start();

客户端代码:

    const Request = require('request');
const Hawk = require('@hapi/hawk');

const credentials = {
id: 'John',
key: 'secret',
algorithm: 'sha256'
};

const requestOptions = {
uri: 'http://localhost:3000/',
method: 'GET',
headers: {}
};

const { header } = Hawk.client.header(requestOptions.uri, requestOptions.method, { credentials: credentials, ext: 'some-app-data' });
requestOptions.headers.Authorization = header;

Request(requestOptions, function (error, response, body) {
const isValid = Hawk.client.authenticate(response, credentials, header.artifacts, { payload: body });

console.log(`${response.statusCode}: ${body}` + (isValid ? ' (valid)' : ' (invalid)'));
});

最佳答案

我针对这个问题创建了一个 PR :) https://github.com/hapijs/hawk/pull/259

关于node.js - @hapi/hawk 的 Hawk 身份验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57055801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com