Skip to content
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

Add AppSecret to Facebook Auth #5695

Merged
merged 1 commit into from
Jun 20, 2019
Merged
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
51 changes: 51 additions & 0 deletions spec/AuthenticationAdapters.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ describe('AuthenticationProviders', function() {
const options = {
facebook: {
appIds: ['a', 'b'],
appSecret: 'secret',
},
};
const {
Expand All @@ -428,6 +429,56 @@ describe('AuthenticationProviders', function() {
expect(providerOptions).toEqual(options.facebook);
});

it('should handle Facebook appSecret for validating appIds', async () => {
const httpsRequest = require('../lib/Adapters/Auth/httpsRequest');
spyOn(httpsRequest, 'get').and.callFake(() => {
return Promise.resolve({ id: 'a' });
});
const options = {
facebook: {
appIds: ['a', 'b'],
appSecret: 'secret_sauce',
},
};
const authData = {
access_token: 'badtoken',
};
const {
adapter,
appIds,
providerOptions,
} = authenticationLoader.loadAuthAdapter('facebook', options);
await adapter.validateAppId(appIds, authData, providerOptions);
expect(
httpsRequest.get.calls.first().args[0].includes('appsecret_proof')
).toBe(true);
});

it('should handle Facebook appSecret for validating auth data', async () => {
const httpsRequest = require('../lib/Adapters/Auth/httpsRequest');
spyOn(httpsRequest, 'get').and.callFake(() => {
return Promise.resolve();
});
const options = {
facebook: {
appIds: ['a', 'b'],
appSecret: 'secret_sauce',
},
};
const authData = {
id: 'test',
access_token: 'test',
};
const { adapter, providerOptions } = authenticationLoader.loadAuthAdapter(
'facebook',
options
);
await adapter.validateAuthData(authData, providerOptions);
expect(
httpsRequest.get.calls.first().args[0].includes('appsecret_proof')
).toBe(true);
});

it('properly loads a custom adapter with options', () => {
const options = {
custom: {
Expand Down
26 changes: 22 additions & 4 deletions src/Adapters/Auth/facebook.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
// Helper functions for accessing the Facebook Graph API.
const httpsRequest = require('./httpsRequest');
var Parse = require('parse/node').Parse;
const crypto = require('crypto');

function getAppSecretPath(authData, options = {}) {
const appSecret = options.appSecret;
if (!appSecret) {
return '';
}
const appsecret_proof = crypto
.createHmac('sha256', appSecret)
.update(authData.access_token)
.digest('hex');

return `&appsecret_proof=${appsecret_proof}`;
}

// Returns a promise that fulfills iff this user id is valid.
function validateAuthData(authData) {
function validateAuthData(authData, options) {
return graphRequest(
'me?fields=id&access_token=' + authData.access_token
'me?fields=id&access_token=' +
authData.access_token +
getAppSecretPath(authData, options)
).then(data => {
if (
(data && data.id == authData.id) ||
Expand All @@ -21,7 +37,7 @@ function validateAuthData(authData) {
}

// Returns a promise that fulfills iff this app id is valid.
function validateAppId(appIds, authData) {
function validateAppId(appIds, authData, options) {
var access_token = authData.access_token;
if (process.env.TESTING && access_token === 'test') {
return Promise.resolve();
Expand All @@ -32,7 +48,9 @@ function validateAppId(appIds, authData) {
'Facebook auth is not configured.'
);
}
return graphRequest('app?access_token=' + access_token).then(data => {
return graphRequest(
'app?access_token=' + access_token + getAppSecretPath(authData, options)
).then(data => {
if (data && appIds.indexOf(data.id) != -1) {
return;
}
Expand Down