Skip to content

Commit d8ba9e8

Browse files
authored
Adds validation for id_token and access_token (#2878)
* ADds validation for id_token and access_token * nit
1 parent 60d5066 commit d8ba9e8

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

src/authDataManager/google.js

+31-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,22 @@
22
var https = require('https');
33
var Parse = require('parse/node').Parse;
44

5-
// Returns a promise that fulfills iff this user id is valid.
6-
function validateAuthData(authData) {
7-
return request("tokeninfo?id_token="+authData.access_token)
5+
function validateIdToken(id, token) {
6+
return request("tokeninfo?id_token="+token)
7+
.then((response) => {
8+
if (response && response.sub == id) {
9+
return;
10+
}
11+
throw new Parse.Error(
12+
Parse.Error.OBJECT_NOT_FOUND,
13+
'Google auth is invalid for this user.');
14+
});
15+
}
16+
17+
function validateAuthToken(id, token) {
18+
return request("tokeninfo?access_token="+token)
819
.then((response) => {
9-
if (response && response.sub == authData.id) {
20+
if (response && response.user_id == id) {
1021
return;
1122
}
1223
throw new Parse.Error(
@@ -15,7 +26,22 @@ function validateAuthData(authData) {
1526
});
1627
}
1728

18-
// Returns a promise that fulfills iff this app id is valid.
29+
// Returns a promise that fulfills if this user id is valid.
30+
function validateAuthData(authData) {
31+
if (authData.id_token) {
32+
return validateIdToken(authData.id, authData.id_token);
33+
} else {
34+
return validateAuthToken(authData.id, authData.access_token).then(() => {
35+
// Validation with auth token worked
36+
return;
37+
}, () => {
38+
// Try with the id_token param
39+
return validateIdToken(authData.id, authData.access_token);
40+
});
41+
}
42+
}
43+
44+
// Returns a promise that fulfills if this app id is valid.
1945
function validateAppId() {
2046
return Promise.resolve();
2147
}

0 commit comments

Comments
 (0)