Skip to content

Commit 9c28ef4

Browse files
committed
Merge remote-tracking branch 'remotes/hulab/features/googleAuth'
2 parents 9b34b02 + b7430eb commit 9c28ef4

File tree

2 files changed

+41
-19
lines changed

2 files changed

+41
-19
lines changed

spec/AuthenticationAdapters.spec.js

+2-16
Original file line numberDiff line numberDiff line change
@@ -597,12 +597,12 @@ describe('google auth adapter', () => {
597597
const google = require('../lib/Adapters/Auth/google');
598598
const jwt = require('jsonwebtoken');
599599

600-
it('should throw error with missing id_token', async () => {
600+
it('should throw error with missing id_token or access_token', async () => {
601601
try {
602602
await google.validateAuthData({}, {});
603603
fail();
604604
} catch (e) {
605-
expect(e.message).toBe('id token is invalid for this user.');
605+
expect(e.message).toBe('id_token or access_token is missing for this user.');
606606
}
607607
});
608608

@@ -615,20 +615,6 @@ describe('google auth adapter', () => {
615615
}
616616
});
617617

618-
// it('should throw error if public key used to encode token is not available', async () => {
619-
// const fakeDecodedToken = { header: { kid: '789', alg: 'RS256' } };
620-
// try {
621-
// spyOn(jwt, 'decode').and.callFake(() => fakeDecodedToken);
622-
623-
// await google.validateAuthData({ id: 'the_user_id', id_token: 'the_token' }, {});
624-
// fail();
625-
// } catch (e) {
626-
// expect(e.message).toBe(
627-
// `Unable to find matching key for Key ID: ${fakeDecodedToken.header.kid}`
628-
// );
629-
// }
630-
// });
631-
632618
it('(using client id as string) should verify id_token', async () => {
633619
const fakeClaim = {
634620
iss: 'https://accounts.google.com',

src/Adapters/Auth/google.js

+39-3
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';
@@ -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

Comments
 (0)