Skip to content

Commit aff2c57

Browse files
authored
Support Google Play Games Service (parse-community#6147)
* Support Google Play Games Service * clean up * improve coverage * more tests
1 parent cc7ca74 commit aff2c57

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

spec/AuthenticationAdapters.spec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const defaultColumns = require('../lib/Controllers/SchemaController')
55
const authenticationLoader = require('../lib/Adapters/Auth');
66
const path = require('path');
77
const responses = {
8+
gpgames: { playerId: 'userId' },
89
instagram: { data: { id: 'userId' } },
910
janrainengage: { stat: 'ok', profile: { identifier: 'userId' } },
1011
janraincapture: { stat: 'ok', result: 'userId' },
@@ -22,6 +23,7 @@ describe('AuthenticationProviders', function() {
2223
[
2324
'apple',
2425
'gcenter',
26+
'gpgames',
2527
'facebook',
2628
'facebookaccountkit',
2729
'github',
@@ -648,6 +650,37 @@ describe('google auth adapter', () => {
648650
});
649651
});
650652

653+
describe('google play games service auth', () => {
654+
const gpgames = require('../lib/Adapters/Auth/gpgames');
655+
const httpsRequest = require('../lib/Adapters/Auth/httpsRequest');
656+
657+
it('validateAuthData should pass validation', async () => {
658+
spyOn(httpsRequest, 'get').and.callFake(() => {
659+
return Promise.resolve({ playerId: 'userId' });
660+
});
661+
await gpgames.validateAuthData({
662+
id: 'userId',
663+
access_token: 'access_token',
664+
});
665+
});
666+
667+
it('validateAuthData should throw error', async () => {
668+
spyOn(httpsRequest, 'get').and.callFake(() => {
669+
return Promise.resolve({ playerId: 'invalid' });
670+
});
671+
try {
672+
await gpgames.validateAuthData({
673+
id: 'userId',
674+
access_token: 'access_token',
675+
});
676+
} catch (e) {
677+
expect(e.message).toBe(
678+
'Google Play Games Services - authData is invalid for this user.'
679+
);
680+
}
681+
});
682+
});
683+
651684
describe('oauth2 auth adapter', () => {
652685
const oauth2 = require('../lib/Adapters/Auth/oauth2');
653686
const httpsRequest = require('../lib/Adapters/Auth/httpsRequest');

src/Adapters/Auth/gcenter.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,10 @@ function verifySignature(publicKey, authData) {
107107
// Returns a promise that fulfills if this user id is valid.
108108
async function validateAuthData(authData) {
109109
if (!authData.id) {
110-
return Promise.reject('Apple Game Center - authData id missing');
110+
throw new Parse.Error(
111+
Parse.Error.OBJECT_NOT_FOUND,
112+
'Apple Game Center - authData id missing'
113+
);
111114
}
112115
authData.playerId = authData.id;
113116
const publicKey = await getAppleCertificate(authData.publicKeyUrl);

src/Adapters/Auth/gpgames.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* Google Play Game Services
2+
https://developers.google.com/games/services/web/api/players/get
3+
4+
const authData = {
5+
id: 'playerId',
6+
access_token: 'token',
7+
};
8+
*/
9+
const { Parse } = require('parse/node');
10+
const httpsRequest = require('./httpsRequest');
11+
12+
// Returns a promise that fulfills if this user id is valid.
13+
async function validateAuthData(authData) {
14+
const response = await httpsRequest.get(
15+
`https://www.googleapis.com/games/v1/players/${authData.id}?access_token=${authData.access_token}`
16+
);
17+
if (!(response && response.playerId === authData.id)) {
18+
throw new Parse.Error(
19+
Parse.Error.OBJECT_NOT_FOUND,
20+
'Google Play Games Services - authData is invalid for this user.'
21+
);
22+
}
23+
}
24+
25+
// Returns a promise that fulfills if this app id is valid.
26+
function validateAppId() {
27+
return Promise.resolve();
28+
}
29+
30+
module.exports = {
31+
validateAppId,
32+
validateAuthData,
33+
};

src/Adapters/Auth/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import loadAdapter from '../AdapterLoader';
22

33
const apple = require('./apple');
44
const gcenter = require('./gcenter');
5+
const gpgames = require('./gpgames');
56
const facebook = require('./facebook');
67
const facebookaccountkit = require('./facebookaccountkit');
78
const instagram = require('./instagram');
@@ -35,6 +36,7 @@ const anonymous = {
3536
const providers = {
3637
apple,
3738
gcenter,
39+
gpgames,
3840
facebook,
3941
facebookaccountkit,
4042
instagram,

0 commit comments

Comments
 (0)