2015-06-17 7 views
5

Sto costruendo un progetto utilizzando Deployd per assistenza con la mia API e dpd-passport per l'autenticazione.Come gestire il callback/reindirizzamenti da dpd-passport tramite la strategia di Google?

Mi sembra di avere tutto autenticante, con le chiavi di sessione consegnate e l'autenticazione degli utenti tramite Google, ma ho problemi con i miei redirectURL s, oltre a tradurre la pagina di richiamata con la quale sono restituito.

Ho scavato nel file dpd-passport/index.js, e credo che questo è l'informazioni rilevanti:

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 }); 
     } 
    } 
}; 

Dopo l'autenticazione con successo, mi viene data una returnUrl di:

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

con una corpo di:

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

che mi sembra come se i miei risultati stessero colpendo l'ultima dichiarazione else nel blocco di codice più in alto.

Se ciò è vero, il mio returnUrl è NULL.

Ripercorrere il codice returnUrl nel file DPD-passaporto, sembra che si dovrebbe afferrare questo dal cookie nel frammento seguente:

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); 
    } 
} 

Per aggiungere a questo, ho il mio allowedRedirectUrls nella configurazione come:

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

sono in perdita e spero ci sia qualcosa di ovvio che mi manca.

Ho visto le vie del passaporto e le strategie di autenticazione simile a quanto segue, ma non hanno avuto successo nell'attuazione del presente in DPD-passaporto:

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

Per aggiungere a tutto questo, io sto usando ui-router/AngularJS.

risposta

2

È necessario fornire il redirectUrl con DPD passaporto attraverso il link che avvia la procedura di OAuth:

http://localhost:2403/auth/google?redirectURL=http://localhost