hapi-auth-bearer-token仅适用于查询字符串中的access_token,而不能作为标头

时间:2019-11-06 20:36:50

标签: node.js authentication hapi

如何将hapi-auth-bearer-token用作使用access_token作为HTTP标头,而不是在查询字符串中传递它?该文档非常清楚,它应该可以工作,但是您不能从下面的屏幕截图中看到。

const Hapi = require('hapi');
const AuthBearer = require('hapi-auth-bearer-token');

const server = Hapi.server({ port: 8080 });

const start = async () => {

    await server.register(AuthBearer)

    server.auth.strategy('simple', 'bearer-access-token', {
        allowQueryToken: true,              // optional, false by default
        validate: async (request, token, h) => {

            // here is where you validate your token
            // comparing with token from your database for example
            const isValid = token === '1234';

            const credentials = { token };
            const artifacts = { test: 'info' };

            return { isValid, credentials, artifacts };
        }
    });

    server.auth.default('simple');

    server.route({
        method: 'GET',
        path: '/',
        handler: async function (request, h) {

            return { info: 'success!' };
        }
    });

    await server.start();

    return server;
}

start()
    .then((server) => console.log(`Server listening on ${server.info.uri}`))
    .catch(err => {

        console.error(err);
        process.exit(1);
    })

查询字符串access_token起作用:

Query string works:

标题access_token不:

enter image description here

1 个答案:

答案 0 :(得分:0)

hapi-auth-bearer-token需要一个承载令牌,这意味着该值需要为Bearer 1234而不仅仅是1234。在我的情况下,我必须不带Bearer来使用它,因此我深入研究了插件源代码并推出了自己的实现。