Skip to content

Commit c5897bc

Browse files
authored
Add AppSecret to Facebook Auth (parse-community#5695)
Closes: parse-community#5448
1 parent 140ff0f commit c5897bc

File tree

2 files changed

+73
-4
lines changed

2 files changed

+73
-4
lines changed

spec/AuthenticationAdapters.spec.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ describe('AuthenticationProviders', function() {
416416
const options = {
417417
facebook: {
418418
appIds: ['a', 'b'],
419+
appSecret: 'secret',
419420
},
420421
};
421422
const {
@@ -428,6 +429,56 @@ describe('AuthenticationProviders', function() {
428429
expect(providerOptions).toEqual(options.facebook);
429430
});
430431

432+
it('should handle Facebook appSecret for validating appIds', async () => {
433+
const httpsRequest = require('../lib/Adapters/Auth/httpsRequest');
434+
spyOn(httpsRequest, 'get').and.callFake(() => {
435+
return Promise.resolve({ id: 'a' });
436+
});
437+
const options = {
438+
facebook: {
439+
appIds: ['a', 'b'],
440+
appSecret: 'secret_sauce',
441+
},
442+
};
443+
const authData = {
444+
access_token: 'badtoken',
445+
};
446+
const {
447+
adapter,
448+
appIds,
449+
providerOptions,
450+
} = authenticationLoader.loadAuthAdapter('facebook', options);
451+
await adapter.validateAppId(appIds, authData, providerOptions);
452+
expect(
453+
httpsRequest.get.calls.first().args[0].includes('appsecret_proof')
454+
).toBe(true);
455+
});
456+
457+
it('should handle Facebook appSecret for validating auth data', async () => {
458+
const httpsRequest = require('../lib/Adapters/Auth/httpsRequest');
459+
spyOn(httpsRequest, 'get').and.callFake(() => {
460+
return Promise.resolve();
461+
});
462+
const options = {
463+
facebook: {
464+
appIds: ['a', 'b'],
465+
appSecret: 'secret_sauce',
466+
},
467+
};
468+
const authData = {
469+
id: 'test',
470+
access_token: 'test',
471+
};
472+
const { adapter, providerOptions } = authenticationLoader.loadAuthAdapter(
473+
'facebook',
474+
options
475+
);
476+
await adapter.validateAuthData(authData, providerOptions);
477+
expect(
478+
httpsRequest.get.calls.first().args[0].includes('appsecret_proof')
479+
).toBe(true);
480+
});
481+
431482
it('properly loads a custom adapter with options', () => {
432483
const options = {
433484
custom: {

src/Adapters/Auth/facebook.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
// Helper functions for accessing the Facebook Graph API.
22
const httpsRequest = require('./httpsRequest');
33
var Parse = require('parse/node').Parse;
4+
const crypto = require('crypto');
5+
6+
function getAppSecretPath(authData, options = {}) {
7+
const appSecret = options.appSecret;
8+
if (!appSecret) {
9+
return '';
10+
}
11+
const appsecret_proof = crypto
12+
.createHmac('sha256', appSecret)
13+
.update(authData.access_token)
14+
.digest('hex');
15+
16+
return `&appsecret_proof=${appsecret_proof}`;
17+
}
418

519
// Returns a promise that fulfills iff this user id is valid.
6-
function validateAuthData(authData) {
20+
function validateAuthData(authData, options) {
721
return graphRequest(
8-
'me?fields=id&access_token=' + authData.access_token
22+
'me?fields=id&access_token=' +
23+
authData.access_token +
24+
getAppSecretPath(authData, options)
925
).then(data => {
1026
if (
1127
(data && data.id == authData.id) ||
@@ -21,7 +37,7 @@ function validateAuthData(authData) {
2137
}
2238

2339
// Returns a promise that fulfills iff this app id is valid.
24-
function validateAppId(appIds, authData) {
40+
function validateAppId(appIds, authData, options) {
2541
var access_token = authData.access_token;
2642
if (process.env.TESTING && access_token === 'test') {
2743
return Promise.resolve();
@@ -32,7 +48,9 @@ function validateAppId(appIds, authData) {
3248
'Facebook auth is not configured.'
3349
);
3450
}
35-
return graphRequest('app?access_token=' + access_token).then(data => {
51+
return graphRequest(
52+
'app?access_token=' + access_token + getAppSecretPath(authData, options)
53+
).then(data => {
3654
if (data && appIds.indexOf(data.id) != -1) {
3755
return;
3856
}

0 commit comments

Comments
 (0)