Skip to content

Add back a google verification for old access_token #6992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions spec/AuthenticationAdapters.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,12 +523,12 @@ describe('google auth adapter', () => {
const google = require('../lib/Adapters/Auth/google');
const jwt = require('jsonwebtoken');

it('should throw error with missing id_token', async () => {
it('should throw error with missing id_token or access_token', async () => {
try {
await google.validateAuthData({}, {});
fail();
} catch (e) {
expect(e.message).toBe('id token is invalid for this user.');
expect(e.message).toBe('id_token or access_token is missing for this user.');
}
});

Expand All @@ -541,20 +541,6 @@ describe('google auth adapter', () => {
}
});

// it('should throw error if public key used to encode token is not available', async () => {
// const fakeDecodedToken = { header: { kid: '789', alg: 'RS256' } };
// try {
// spyOn(jwt, 'decode').and.callFake(() => fakeDecodedToken);

// await google.validateAuthData({ id: 'the_user_id', id_token: 'the_token' }, {});
// fail();
// } catch (e) {
// expect(e.message).toBe(
// `Unable to find matching key for Key ID: ${fakeDecodedToken.header.kid}`
// );
// }
// });

it('(using client id as string) should verify id_token', async () => {
const fakeClaim = {
iss: 'https://accounts.google.com',
Expand Down
42 changes: 39 additions & 3 deletions src/Adapters/Auth/google.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

const https = require('https');
const jwt = require('jsonwebtoken');
const httpsRequest = require('./httpsRequest');

const TOKEN_ISSUER = 'accounts.google.com';
const HTTPS_TOKEN_ISSUER = 'https://accounts.google.com';
Expand Down Expand Up @@ -87,7 +88,7 @@
);
}

if (jwtClaims.sub !== id) {
if (typeof id != 'undefined' && jwtClaims.sub !== id) {
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, `auth data is invalid for this user.`);
}

Expand All @@ -101,9 +102,39 @@
return jwtClaims;
}

// Old way to validate an auth_token, only used for development purpose
function validateAuthToken({ id, access_token }) {
return googleRequest('tokeninfo?access_token=' + access_token).then(response => {
if (response && (response.sub == id || response.user_id == id)) {
return;

Check warning on line 109 in src/Adapters/Auth/google.js

View check run for this annotation

Codecov / codecov/patch

src/Adapters/Auth/google.js#L107-L109

Added lines #L107 - L109 were not covered by tests
}
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Google auth is invalid for this user.');

Check warning on line 111 in src/Adapters/Auth/google.js

View check run for this annotation

Codecov / codecov/patch

src/Adapters/Auth/google.js#L111

Added line #L111 was not covered by tests
});
}

// Returns a promise that fulfills if this user id is valid.
function validateAuthData(authData, options = {}) {
return verifyIdToken(authData, options);
function validateAuthData({ id, id_token, access_token }, options) {
if (!id_token && !access_token) {
return Promise.reject(new Parse.Error(
Parse.Error.OBJECT_NOT_FOUND,
`id_token or access_token is missing for this user.`
));
}
// Returns a promise that fulfills if this user id is valid.
if (id_token) {
return verifyIdToken({ id, id_token }, options);
} else {
return validateAuthToken({ id, access_token }).then(

Check warning on line 127 in src/Adapters/Auth/google.js

View check run for this annotation

Codecov / codecov/patch

src/Adapters/Auth/google.js#L127

Added line #L127 was not covered by tests
() => {
// Validation with auth token worked
return;

Check warning on line 130 in src/Adapters/Auth/google.js

View check run for this annotation

Codecov / codecov/patch

src/Adapters/Auth/google.js#L130

Added line #L130 was not covered by tests
},
() => {
// Try with the id_token param
return verifyIdToken({ id, id_token: access_token }, options);

Check warning on line 134 in src/Adapters/Auth/google.js

View check run for this annotation

Codecov / codecov/patch

src/Adapters/Auth/google.js#L134

Added line #L134 was not covered by tests
}
);
}
}

// Returns a promise that fulfills if this app id is valid.
Expand Down Expand Up @@ -169,3 +200,8 @@
const lengthOfLengthByte = 128 + nHex.length / 2;
return toHex(lengthOfLengthByte) + nHex;
}

// A promisey wrapper for api requests
function googleRequest(path) {
return httpsRequest.get('https://www.googleapis.com/oauth2/v3/' + path);

Check warning on line 206 in src/Adapters/Auth/google.js

View check run for this annotation

Codecov / codecov/patch

src/Adapters/Auth/google.js#L206

Added line #L206 was not covered by tests
}