oauthjs / node-oauth2-server

Complete, compliant and well tested module for implementing an OAuth2 Server/Provider with express in node.js
https://npmjs.org/package/oauth2-server
MIT License
4.01k stars 931 forks source link

Uncaught error: Invalid argument: `request` must be an instance of Request - Using Hapi #443

Open michaelpwilson opened 6 years ago

michaelpwilson commented 6 years ago

When trying to authenticate a get request with hapi, I cannot supply request and reply like so:

server.route({
    method: 'GET',
    path:'/users', 
    handler: (request, reply) => {
        oauth.authenticate(request, reply)
         .then((token) => {
            console.log("successful", token);
         })
         .catch((err) => {
            // The request failed authentication.
            //console.log("error", err);
         });
    }
});

I understand this is because Request and Response are not being used by Hapi.js, but is there anyway to get this to work?

Error log:

invalid_argument: Uncaught error: Invalid argument: `request` must be an instance of Request
    at new InvalidArgumentError (/Users/michaelwilson/Code/checkmyfile-scraper/node_modules/oauth2-server/lib/errors/invalid-argument-error.js:21:14)
    at AuthenticateHandler.handle (/Users/michaelwilson/Code/checkmyfile-scraper/node_modules/oauth2-server/lib/handlers/authenticate-handler.js:59:11)
    at OAuth2Server.authenticate (/Users/michaelwilson/Code/checkmyfile-scraper/node_modules/oauth2-server/lib/server.js:43:6)
    at handler (/Users/michaelwilson/Code/checkmyfile-scraper/index.js:128:19)
    at Object.internals.handler (/Users/michaelwilson/Code/checkmyfile-scraper/node_modules/hapi/lib/handler.js:101:51)
    at request._protect.run (/Users/michaelwilson/Code/checkmyfile-scraper/node_modules/hapi/lib/handler.js:32:23)
    at internals.Protect.run (/Users/michaelwilson/Code/checkmyfile-scraper/node_modules/hapi/lib/protect.js:60:12)
    at exports.execute (/Users/michaelwilson/Code/checkmyfile-scraper/node_modules/hapi/lib/handler.js:26:22)
    at each (/Users/michaelwilson/Code/checkmyfile-scraper/node_modules/hapi/lib/request.js:401:16)
    at iterate (/Users/michaelwilson/Code/checkmyfile-scraper/node_modules/items/lib/index.js:36:13)
    at done (/Users/michaelwilson/Code/checkmyfile-scraper/node_modules/items/lib/index.js:28:25)
    at internals.Auth._authenticate (/Users/michaelwilson/Code/checkmyfile-scraper/node_modules/hapi/lib/auth.js:222:16)
    at internals.Auth.authenticate (/Users/michaelwilson/Code/checkmyfile-scraper/node_modules/hapi/lib/auth.js:197:17)
    at each (/Users/michaelwilson/Code/checkmyfile-scraper/node_modules/hapi/lib/request.js:401:16)
    at iterate (/Users/michaelwilson/Code/checkmyfile-scraper/node_modules/items/lib/index.js:36:13)
    at done (/Users/michaelwilson/Code/checkmyfile-scraper/node_modules/items/lib/index.js:28:25)
    at Hoek.once (/Users/michaelwilson/Code/checkmyfile-scraper/node_modules/hapi/lib/protect.js:50:16)
    at wrapped (/Users/michaelwilson/Code/checkmyfile-scraper/node_modules/hoek/lib/index.js:875:20)
    at done (/Users/michaelwilson/Code/checkmyfile-scraper/node_modules/items/lib/index.js:31:25)
    at finalize (/Users/michaelwilson/Code/checkmyfile-scraper/node_modules/hapi/lib/request.js:446:24)
    at Function.wrapped [as _next] (/Users/michaelwilson/Code/checkmyfile-scraper/node_modules/hoek/lib/index.js:875:20)
    at Function.internals.continue (/Users/michaelwilson/Code/checkmyfile-scraper/node_modules/hapi/lib/reply.js:139:10)
michaelpwilson commented 6 years ago

I was able to get around this by creating functions which took in the request/response, and made them into Request and Response objects:

function convertRequest(request) {
    return new Request({
        method: request.method,
        query: request.query,
        headers: request.headers
    });
}

function convertResponse(response) {
    return new Response({
        headers: response.headers
    });
}

Would anyone be able to suggest if this is a good idea or not?

Thanks!