@@ -5,6 +5,7 @@ var Parse = require('parse/node').Parse;
5
5
6
6
const https = require ( 'https' ) ;
7
7
const jwt = require ( 'jsonwebtoken' ) ;
8
+ const httpsRequest = require ( './httpsRequest' ) ;
8
9
9
10
const TOKEN_ISSUER = 'accounts.google.com' ;
10
11
const HTTPS_TOKEN_ISSUER = 'https://accounts.google.com' ;
@@ -87,7 +88,7 @@ async function verifyIdToken({ id_token: token, id }, { clientId }) {
87
88
) ;
88
89
}
89
90
90
- if ( jwtClaims . sub !== id ) {
91
+ if ( typeof id != 'undefined' && jwtClaims . sub !== id ) {
91
92
throw new Parse . Error ( Parse . Error . OBJECT_NOT_FOUND , `auth data is invalid for this user.` ) ;
92
93
}
93
94
@@ -101,9 +102,39 @@ async function verifyIdToken({ id_token: token, id }, { clientId }) {
101
102
return jwtClaims ;
102
103
}
103
104
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
+
104
115
// 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
+ }
107
138
}
108
139
109
140
// Returns a promise that fulfills if this app id is valid.
@@ -169,3 +200,8 @@ function encodeLengthHex(n) {
169
200
const lengthOfLengthByte = 128 + nHex . length / 2 ;
170
201
return toHex ( lengthOfLengthByte ) + nHex ;
171
202
}
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