@@ -15,6 +15,7 @@ var User = require('../models/user').User;
1515var verifyPassport = require ( '../libs/passportVerify' ) . verify ;
1616var cleanFilename = require ( '../libs/helpers' ) . cleanFilename ;
1717var addSession = require ( '../libs/modifySessions' ) . add ;
18+ var jwt = require ( 'jwt-simple' ) ;
1819
1920// Unused but removing it breaks passport
2021passport . serializeUser ( function ( aUser , aDone ) {
@@ -43,16 +44,22 @@ exports.auth = function (aReq, aRes, aNext) {
4344 var authedUser = aReq . session . user ;
4445 var strategy = aReq . body . auth || aReq . params . strategy ;
4546 var username = aReq . body . username || aReq . session . username ;
47+ var authOpts = { failureRedirect : '/register?stratfail' } ;
4648
4749 function auth ( ) {
48- var authenticate = passport . authenticate ( strategy , { failureRedirect : '/register?stratfail' } ) ;
50+ var authenticate = null ;
51+
52+ if ( strategy === 'google' ) {
53+ authOpts . scope = [ 'https://www.googleapis.com/auth/userinfo.profile' ] ;
54+ }
55+ authenticate = passport . authenticate ( strategy , authOpts ) ;
4956
5057 // Just in case some dumbass tries a bad /auth/* url
5158 if ( ! strategyInstances [ strategy ] ) {
5259 return aNext ( ) ;
5360 }
5461
55- authenticate ( aReq , aRes ) ;
62+ authenticate ( aReq , aRes , aNext ) ;
5663 }
5764
5865 // Allow a logged in user to add a new strategy
@@ -128,6 +135,14 @@ exports.callback = function (aReq, aRes, aNext) {
128135 strategyInstance . _verify = function ( aId , aDone ) {
129136 verifyPassport ( aId , strategy , username , aReq . session . user , aDone ) ;
130137 } ;
138+ } else if ( strategy === 'google' ) { // OpenID to OAuth2 migration
139+ strategyInstance . _verify =
140+ function ( aAccessToken , aRefreshToken , aParams , aProfile , aDone ) {
141+ var openIdId = jwt . decode ( aParams . id_token , null , true ) . openid_id ;
142+ var oAuthId = aProfile . id ;
143+
144+ verifyPassport ( [ openIdId , oAuthId ] , strategy , username , aReq . session . user , aDone ) ;
145+ } ;
131146 } else {
132147 strategyInstance . _verify =
133148 function ( aToken , aRefreshOrSecretToken , aProfile , aDone ) {
0 commit comments