Skip to content

Commit 940c8ab

Browse files
committed
Add back a google verification for old access_token
1 parent c983202 commit 940c8ab

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

src/Adapters/Auth/google.js

+38-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var Parse = require('parse/node').Parse;
55

66
const https = require('https');
77
const jwt = require('jsonwebtoken');
8+
const httpsRequest = require('./httpsRequest');
89

910
const TOKEN_ISSUER = 'accounts.google.com';
1011
const HTTPS_TOKEN_ISSUER = 'https://accounts.google.com';
@@ -25,7 +26,7 @@ function getGoogleKeyByKeyId(keyId) {
2526
data += chunk.toString('utf8');
2627
});
2728
res.on('end', () => {
28-
const { keys } = JSON.parse(data);
29+
const {keys} = JSON.parse(data);
2930
const pems = keys.reduce(
3031
(pems, { n: modulus, e: exposant, kid }) =>
3132
Object.assign(pems, {
@@ -54,7 +55,7 @@ function getGoogleKeyByKeyId(keyId) {
5455
}
5556

5657
function getHeaderFromToken(token) {
57-
const decodedToken = jwt.decode(token, { complete: true });
58+
const decodedToken = jwt.decode(token, {complete: true});
5859

5960
if (!decodedToken) {
6061
throw new Parse.Error(
@@ -66,7 +67,7 @@ function getHeaderFromToken(token) {
6667
return decodedToken.header;
6768
}
6869

69-
async function verifyIdToken({ id_token: token, id }, { clientId }) {
70+
async function verifyIdToken({id_token: token, id}, {clientId}) {
7071
if (!token) {
7172
throw new Parse.Error(
7273
Parse.Error.OBJECT_NOT_FOUND,
@@ -112,9 +113,34 @@ async function verifyIdToken({ id_token: token, id }, { clientId }) {
112113
return jwtClaims;
113114
}
114115

116+
// Old way to validate an auth_token, only used for development purpose
117+
function validateAuthToken({id,access_token}) {
118+
return googleRequest('tokeninfo?access_token=' + access_token).then(response => {
119+
if (response && (response.sub == id || response.user_id == id)) {
120+
return;
121+
}
122+
throw new Parse.Error(
123+
Parse.Error.OBJECT_NOT_FOUND, 'Google auth is invalid for this user.');
124+
});
125+
}
126+
115127
// Returns a promise that fulfills if this user id is valid.
116-
function validateAuthData(authData, options = {}) {
117-
return verifyIdToken(authData, options);
128+
function validateAuthData({id, id_token, access_token}, options) {
129+
// Returns a promise that fulfills if this user id is valid.
130+
if (id_token) {
131+
return verifyIdToken({id, id_token}, options);
132+
} else {
133+
return validateAuthToken({id, access_token}).then(
134+
() => {
135+
// Validation with auth token worked
136+
return;
137+
},
138+
() => {
139+
// Try with the id_token param
140+
return verifyIdToken({id, id_token: access_token}, options);
141+
}
142+
);
143+
}
118144
}
119145

120146
// Returns a promise that fulfills if this app id is valid.
@@ -124,9 +150,10 @@ function validateAppId() {
124150

125151
module.exports = {
126152
validateAppId: validateAppId,
127-
validateAuthData: validateAuthData,
153+
validateAuthData: validateAuthData
128154
};
129155

156+
130157
// Helpers functions to convert the RSA certs to PEM (from jwks-rsa)
131158
function rsaPublicKeyToPEM(modulusB64, exponentB64) {
132159
const modulus = new Buffer(modulusB64, 'base64');
@@ -182,3 +209,8 @@ function encodeLengthHex(n) {
182209
const lengthOfLengthByte = 128 + nHex.length / 2;
183210
return toHex(lengthOfLengthByte) + nHex;
184211
}
212+
213+
// A promisey wrapper for api requests
214+
function googleRequest(path) {
215+
return httpsRequest.get('https://www.googleapis.com/oauth2/v3/' + path);
216+
}

0 commit comments

Comments
 (0)