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

标签 node.js hapi.js

我正在尝试使用 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/

相关文章:

node.js - 在 globals.js 中注释 async 不会影响其在服务中的使用

javascript - 如何在 socket.io 中锁定全局数组

javascript - 哈皮斯 : How to send POST objects array in POSTMAN

javascript - hapi.js 与 auth 相关

javascript - 我想在 POST 命令中发送一些内容,而不需要用户在 Javascript 中输入该值

node.js - 验证错误: child "password" fails because ["password" is required]

javascript - Webpack - 将应用程序部署到不同的环境

json - Node.js:如何交换 JSON 对象

javascript - 使用 debug-brk 启动 Sails 不会运行 Grunt?

javascript - 匹配两个查询以检查数据类型