forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacebook_server.js
108 lines (92 loc) · 3.3 KB
/
facebook_server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
Facebook = {};
var crypto = Npm.require('crypto');
Facebook.handleAuthFromAccessToken = function handleAuthFromAccessToken(accessToken, expiresAt) {
// include all fields from facebook
// http://developers.facebook.com/docs/reference/login/public-profile-and-friend-list/
var whitelisted = ['id', 'email', 'name', 'first_name',
'last_name', 'link', 'gender', 'locale', 'age_range'];
var identity = getIdentity(accessToken, whitelisted);
var serviceData = {
accessToken: accessToken,
expiresAt: expiresAt
};
var fields = _.pick(identity, whitelisted);
_.extend(serviceData, fields);
return {
serviceData: serviceData,
options: {profile: {name: identity.name}}
};
};
OAuth.registerService('facebook', 2, null, function(query) {
var response = getTokenResponse(query);
var accessToken = response.accessToken;
var expiresIn = response.expiresIn;
return Facebook.handleAuthFromAccessToken(accessToken, (+new Date) + (1000 * expiresIn));
});
// checks whether a string parses as JSON
var isJSON = function (str) {
try {
JSON.parse(str);
return true;
} catch (e) {
return false;
}
};
// returns an object containing:
// - accessToken
// - expiresIn: lifetime of token in seconds
var getTokenResponse = function (query) {
var config = ServiceConfiguration.configurations.findOne({service: 'facebook'});
if (!config)
throw new ServiceConfiguration.ConfigError();
var responseContent;
try {
// Request an access token
responseContent = HTTP.get(
"https://graph.facebook.com/v2.8/oauth/access_token", {
params: {
client_id: config.appId,
redirect_uri: OAuth._redirectUri('facebook', config),
client_secret: OAuth.openSecret(config.secret),
code: query.code
}
}).data;
} catch (err) {
throw _.extend(new Error("Failed to complete OAuth handshake with Facebook. " + err.message),
{response: err.response});
}
var fbAccessToken = responseContent.access_token;
var fbExpires = responseContent.expires_in;
if (!fbAccessToken) {
throw new Error("Failed to complete OAuth handshake with facebook " +
"-- can't find access token in HTTP response. " + responseContent);
}
return {
accessToken: fbAccessToken,
expiresIn: fbExpires
};
};
var getIdentity = function (accessToken, fields) {
var config = ServiceConfiguration.configurations.findOne({service: 'facebook'});
if (!config)
throw new ServiceConfiguration.ConfigError();
// Generate app secret proof that is a sha256 hash of the app access token, with the app secret as the key
// https://developers.facebook.com/docs/graph-api/securing-requests#appsecret_proof
var hmac = crypto.createHmac('sha256', OAuth.openSecret(config.secret));
hmac.update(accessToken);
try {
return HTTP.get("https://graph.facebook.com/v2.8/me", {
params: {
access_token: accessToken,
appsecret_proof: hmac.digest('hex'),
fields: fields.join(",")
}
}).data;
} catch (err) {
throw _.extend(new Error("Failed to fetch identity from Facebook. " + err.message),
{response: err.response});
}
};
Facebook.retrieveCredential = function(credentialToken, credentialSecret) {
return OAuth.retrieveCredential(credentialToken, credentialSecret);
};