@@ -5,6 +5,7 @@ var Parse = require('parse/node').Parse;
55
66const  https  =  require ( 'https' ) ; 
77const  jwt  =  require ( 'jsonwebtoken' ) ; 
8+ const  httpsRequest  =  require ( './httpsRequest' ) ; 
89
910const  TOKEN_ISSUER  =  'accounts.google.com' ; 
1011const  HTTPS_TOKEN_ISSUER  =  'https://accounts.google.com' ; 
@@ -87,7 +88,7 @@ async function verifyIdToken({ id_token: token, id }, { clientId }) {
8788    ) ; 
8889  } 
8990
90-   if  ( jwtClaims . sub  !==  id )  { 
91+   if  ( typeof   id   !=   'undefined'   &&   jwtClaims . sub  !==  id )  { 
9192    throw  new  Parse . Error ( Parse . Error . OBJECT_NOT_FOUND ,  `auth data is invalid for this user.` ) ; 
9293  } 
9394
@@ -101,9 +102,39 @@ async function verifyIdToken({ id_token: token, id }, { clientId }) {
101102  return  jwtClaims ; 
102103} 
103104
105+ // Old way to validate an auth_token, only used for development purpose 
106+ function  validateAuthToken ( {  id,  access_token } )  { 
107+   return  googleRequest ( 'tokeninfo?access_token='  +  access_token ) . then ( response  =>  { 
108+     if  ( response  &&  ( response . sub  ==  id  ||  response . user_id  ==  id ) )  { 
109+       return ; 
110+     } 
111+     throw  new  Parse . Error ( Parse . Error . OBJECT_NOT_FOUND ,  'Google auth is invalid for this user.' ) ; 
112+   } ) ; 
113+ } 
114+ 
104115// Returns a promise that fulfills if this user id is valid. 
105- function  validateAuthData ( authData ,  options  =  { } )  { 
106-   return  verifyIdToken ( authData ,  options ) ; 
116+ function  validateAuthData ( {  id,  id_token,  access_token } ,  options )  { 
117+   if  ( ! id_token  &&  ! access_token )  { 
118+     return  Promise . reject ( new  Parse . Error ( 
119+       Parse . Error . OBJECT_NOT_FOUND , 
120+       `id_token or access_token is missing for this user.` 
121+     ) ) ; 
122+   } 
123+   // Returns a promise that fulfills if this user id is valid. 
124+   if  ( id_token )  { 
125+     return  verifyIdToken ( {  id,  id_token } ,  options ) ; 
126+   }  else  { 
127+     return  validateAuthToken ( {  id,  access_token } ) . then ( 
128+       ( )  =>  { 
129+         // Validation with auth token worked 
130+         return ; 
131+       } , 
132+       ( )  =>  { 
133+         // Try with the id_token param 
134+         return  verifyIdToken ( {  id,  id_token : access_token  } ,  options ) ; 
135+       } 
136+     ) ; 
137+   } 
107138} 
108139
109140// Returns a promise that fulfills if this app id is valid. 
@@ -169,3 +200,8 @@ function encodeLengthHex(n) {
169200  const  lengthOfLengthByte  =  128  +  nHex . length  /  2 ; 
170201  return  toHex ( lengthOfLengthByte )  +  nHex ; 
171202} 
203+ 
204+ // A promisey wrapper for api requests 
205+ function  googleRequest ( path )  { 
206+   return  httpsRequest . get ( 'https://www.googleapis.com/oauth2/v3/'  +  path ) ; 
207+ } 
0 commit comments