forked from heroku/faceplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
177 lines (148 loc) · 4.33 KB
/
index.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var b64url = require('b64url');
var crypto = require('crypto');
var qs = require('querystring');
var restler = require('restler');
var util = require('util');
var Faceplate = function(options) {
var self = this;
this.options = options || {};
this.app_id = this.options.app_id;
this.secret = this.options.secret;
this.middleware = function() {
return function(req, res, next) {
if (req.body.signed_request) {
self.parse_signed_request(req.body.signed_request, function(decoded_signed_request) {
req.facebook = new FaceplateSession(self, decoded_signed_request);
next();
});
} else if (req.cookies["fbsr_" + self.app_id]) {
self.parse_signed_request(req.cookies["fbsr_" + self.app_id], function(decoded_signed_request) {
req.facebook = new FaceplateSession(self, decoded_signed_request);
next();
});
} else {
req.facebook = new FaceplateSession(self);
next();
}
}
}
this.parse_signed_request = function(signed_request, cb) {
var encoded_data = signed_request.split('.', 2);
var sig = encoded_data[0];
var json = b64url.decode(encoded_data[1]);
var data = JSON.parse(json);
// check algorithm
if (!data.algorithm || (data.algorithm.toUpperCase() != 'HMAC-SHA256')) {
throw("unknown algorithm. expected HMAC-SHA256");
}
// check signature
var secret = self.secret;
var expected_sig = crypto.createHmac('sha256', secret).update(encoded_data[1]).digest('base64').replace(/\+/g,'-').replace(/\//g,'_').replace('=','');
if (sig !== expected_sig)
throw("bad signature");
// not logged in or not authorized
if (!data.user_id) {
cb(data);
return;
}
if (data.access_token) {
cb(data);
return;
}
if (!data.code)
throw("no oauth token and no code to get one");
var params = {
client_id: self.app_id,
client_secret: self.secret,
redirect_uri: '',
code: data.code
};
var request = restler.get('https://graph.facebook.com/oauth/access_token', { query:params });
request.on('fail', function(data) {
var result = JSON.parse(data);
console.log('invalid code: ' + result.error.message);
cb();
});
request.on('success', function(data) {
cb(qs.parse(data));
});
}
}
var FaceplateSession = function(plate, signed_request) {
var self = this;
this.plate = plate;
if (signed_request) {
this.token = signed_request.access_token;
this.signed_request = signed_request;
}
this.app = function(cb) {
self.get('/' + self.plate.app_id, function(err, app) {
cb(app);
});
}
this.me = function(cb) {
if (self.token) {
self.get('/me', function(err, me) {
cb(me);
});
} else {
cb();
}
}
this.get = function(path, params, cb) {
if (cb === undefined) {
cb = params;
params = {};
}
if (self.token)
params.access_token = self.token;
try {
restler.get('https://graph.facebook.com' + path, { query: params }).on('complete', function(data) {
var result = JSON.parse(data);
cb(null, result);
});
} catch (err) {
cb(err);
}
}
this.fql = function(query, cb) {
var params = { access_token: self.token, format:'json' };
var method;
var onComplete;
if (typeof query == 'string') {
method = 'fql.query';
params.query = query;
onComplete = cb;
}
else {
method = 'fql.multiquery';
params.queries = JSON.stringify(query);
onComplete = function(res) {
if (res.error_code)
return cb(res);
var data = {};
res.forEach(function(q) {
data[q.name] = q.fql_result_set;
});
cb(data);
};
}
restler.get('https://api.facebook.com/method/'+method, { query: params }).on('complete', onComplete);
}
this.post = function (params, cb) {
restler.post(
'https://graph.facebook.com/me/feed',
{
query:{
access_token:self.token
},
data: params
}).on('complete', function (data) {
var result = JSON.parse(data);
cb(result.data ? result.data : result);
});
}
}
module.exports.middleware = function(options) {
return new Faceplate(options).middleware();
}