Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle Callback/Redirects from dpd-passport via Google strategy?

I am building a project using Deployd for assistance with my API, and dpd-passport for authentication.

I seem to have everything authenticating, with session keys behind handed out and users authentication through Google, but I am having trouble with my redirectURLs, as well as translating the callback page I am returned with.

I have dug into the dpd-passport/index.js file, and I believe this is the relevant information:

var sendResponse = function(ctx, err, config) {
var sessionData = ctx.session.data;
var returnUrl = (ctx.req.cookies && ctx.req.cookies.get('_passportReturnUrl')) || null;

if(returnUrl) {
    var redirectURL = url.parse(returnUrl, true);

    // only append if not disabled
    if(!config.disableReturnParams) {
        // delete search so that query is used
        delete redirectURL.search;

        // make sure query is inited
        redirectURL.query = redirectURL.query || {};
        if(err) {
            redirectURL.query.success = false;
            redirectURL.query.error = err;
        } else {
            // append user + session id to the redirect url
            redirectURL.query.success = true;

            if(!config.disableSessionId) {
                redirectURL.query.sid = sessionData.id;
                redirectURL.query.uid = sessionData.uid;
            }
        }
    }

    var redirectURLString = '';
    try {
        redirectURLString = url.format(redirectURL);
    } catch(ex) {
        console.warn('An error happened while formatting the redirectURL', ex);
    }

    // redirect the user
    ctx.res.setHeader("Location", redirectURLString);
    ctx.res.statusCode = 302;

    ctx.done(null, 'This page has moved to ' + redirectURLString);
    } else {
        if(err) {
            ctx.res.statusCode = 401;
            console.error(err);
            return ctx.done('bad credentials');
        } else {
            ctx.done(err, { path: sessionData.path, id: sessionData.id, uid: sessionData.uid });
        }
    }
};

After successfully authenticating, I am given a returnUrl of:

http://localhost:3000/auth/google/callback?code=4/l4o-H2F4QKJ5tdKbVbGfWygTGRvhHgr9zrHWImFFKdM#

with an body of:

{"path":"/users","id":"d03c0faccfe41134c193266afef979c5af33adf935aeff45844b0f9473dee4ab1fbd1114240e13ea9a542785da3845cfec984e3a5b8cb188d6c595b6fc39a726","uid":"747f97a9bcfa9811"}

which seems to me like my results are hitting the final else statement in the topmost code block.

If this is true, then my returnUrl is NULL.

Tracing back the returnUrl code in the dpd-passport file, it looks like it should be grabbing this from cookies in the follow snippet:

if(ctx.query.redirectURL && this.config.allowedRedirectURLs) {
    try {
        this.regEx = this.regEx || new RegExp(this.config.allowedRedirectURLs, 'i');

        if(ctx.query.redirectURL.match(this.regEx)) {
            // save this info into the users session, so that we can access it later (even if the user was redirected to facebook)
            if (ctx.res.cookies) ctx.res.cookies.set('_passportReturnUrl', ctx.query.redirectURL);
        } else {
            debug(ctx.query.redirectURL, 'did not match', this.config.allowedRedirectURLs);
        }
    } catch(ex) {
        debug('Error parsing RedirectURL Regex!', ex);
    }
}

To add to this, I have my allowedRedirectUrls in the config as:

^http://localhost:3000/.*$

I am at a loss and am hoping there is something obvious that I am missing.

I have seen the passport routes and authentication strategies similar to the following, but have been unsuccessful in implementing this into dpd-passport:

app.get('/auth/google/callback', 
  passport.authenticate('google', { failureRedirect: '/login' }),
  function(req, res) {
    // Successful authentication, redirect home.
    res.redirect('/');
  });

To add to this all, I am using ui-router/AngularJS.

like image 389
Nick Sporich Avatar asked Oct 31 '22 01:10

Nick Sporich


1 Answers

You have to supply the redirectURL to dpd-passport through the link that starts the oauth procedure:

http://localhost:2403/auth/google?redirectURL=http://localhost
like image 165
Pendar Avatar answered Nov 14 '22 09:11

Pendar