1

I implemented oauth2orize in hapijs. But when I am calling the api, nothing happen. The function goes inside code.js file of oauth2orize module and hangs in between. Please suggest me how to implement oauth2orize in hapjs. hapi-oauth2orize is also not working as immigration & hapi-oauth2orize plugin throws option error.

const Hapi = require('hapi');
const server = new Hapi.Server();
const oauth2orize = require('oauth2orize');
var oauth = oauth2orize.createServer();

server.connection({ 
    host: 'localhost', 
    port: 8000 
});

    server.register([{
        register: require('hapi-mongodb'),
        options: dbOpts
    }], function (err) {
        if (err) {
            console.error(err);
            throw err;
        }
        server.start();

        server.route([
                    {
                      method: 'GET',
                      path: '/oauth/authorizegrant',
                      config: {
                        auth: false,
                        handler: function(request, reply) {
                            var clientId = request.query.client_id,
                                redirectUrl = request.query.redirect_uri,
                                resType = request.query.response_type,
                                state = request.query.state;
                            oauth.grant(oauth2orize.grant.code(function(clientId,redirectUrl,resType,state,callback) {
                              // Create a new authorization code
                                  console.log('client', client);
                                  var db = request.server.plugins['hapi-mongodb'].db;
                                  var code = new Code({
                                    value: uid(16),
                                    clientId: client._id,
                                    redirectUri: redirectUri,
                                    userId: user._id
                                  });

                              // Save the auth code and check for errors
                            db.collection('codes').insert(code, function(err) {
                                if (err) { console.log('err*********', err); return callback(err); }

                                callback(null, code.value);
                              });
                            }));

                        }
                      }
                    },
                ]);
    });
Garima
  • 1,566
  • 2
  • 11
  • 14

1 Answers1

0

You need to change parameters passed to oauth.grant function, the callback should be removed and replaced by hapi's reply function. A simple snippet would be

if (err) {
    return reply(err);
}
return reply(code.value);

I would file an issue in the plugin repo as this is the best way to interface between hapi and oauth2orize.

simon-p-r
  • 3,623
  • 2
  • 20
  • 35
  • thanks for reply. I am new to hapijs and oauth2orize. I did the code as per you but its not working. Also , hapi-oauth2orize is outdated and its throwing a plugin error. oauth.grant(oauth2orize.grant.code(function(clientId, redirectUrl, resType, state, reply){ } – Garima Jun 22 '16 at 05:23
  • Remove callback and use reply where you were using callback inside function body, also file an issue on github for module. It maybe throwing due to a programmatic error. Do you need be to write all code? – simon-p-r Jun 22 '16 at 08:44
  • I removed callback, but nothing works. Yaah, I already raised it. – Garima Jun 22 '16 at 09:54
  • Yes seen you have opened 3 different issues, the code in them is wrong that is why it is not working. – simon-p-r Jun 22 '16 at 11:49
  • But I implemented the same code as given in oauth2orize framework. – Garima Jun 22 '16 at 12:58
  • That code is to be used in outside of hapi handler context hence why it is not working – simon-p-r Jun 22 '16 at 13:22
  • Then from where it get called? – Garima Jun 23 '16 at 03:56
  • As I shown in my answer where you make db call you must replace callbacks with hapi's reply function and remove callback from oauth2orize function signature. It will then work. – simon-p-r Jun 23 '16 at 07:22