-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
182 lines (154 loc) · 4.61 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
178
179
180
181
182
var b64url = require('b64url');
var crypto = require('crypto');
var qs = require('querystring');
var request = require('request');
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 faceplate(req, res, next) {
if (req.body.signed_request) {
self.parse_signed_request(req.body.signed_request, function(err, 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(err, 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')) {
cb(new Error("unknown algorithm. expected HMAC-SHA256"));
return;
}
// 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) {
cb(new Error("bad signature"));
return;
}
// not logged in or not authorized
if (!data.user_id) {
cb(null,data);
return;
}
if (data.access_token || data.oauth_token) {
cb(null,data);
return;
}
if (!data.code) {
cb(new Error("no oauth token and no code to get one"));
return;
}
var params = {
client_id: self.app_id,
client_secret: self.secret,
redirect_uri: '',
code: data.code
};
var req = request('https://graph.facebook.com/oauth/access_token',
{ qs:params }, function(err, resp, body){
if(err) return cb(err);
var result = qs.parse(body);
cb(null, result);
});
};
};
var FaceplateSession = function(plate, signed_request) {
var self = this;
this.plate = plate;
if (signed_request) {
this.token = signed_request.access_token || signed_request.oauth_token;
this.signed_request = signed_request;
}
this.app = function(cb) {
self.get('/' + self.plate.app_id, function(err, app) {
cb(err,app);
});
};
this.me = function(cb) {
if (self.token) {
self.get('/me', function(err, me) {
cb(err,me);
});
} else {
cb(null,null);
}
};
this.get = function(path, params, cb) {
if (cb === undefined) {
cb = params;
params = {};
}
if (self.token)
params.access_token = self.token;
try {
var req = request('https://graph.facebook.com' + path,
{ qs: params }, function(err, resp, body){
if(err) return cb(err);
cb(null, JSON.parse(body));
});
} 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 = function(res){
var result = JSON.parse(res);
cb(null, result.data ? result.data : result);
};
}
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(null,data);
};
}
var req = request('https://api.facebook.com/method/'+method,
{ qs: params }, function(err, resp, body){
if(err) return cb(err);
onComplete(body);
});
};
this.post = function (params, cb) {
var req = request(
'https://graph.facebook.com/me/feed',
{qs: {access_token: self.token}, form: params, method:'POST'},
function(err, resp, body){
if(err) return callback(err);
var result = JSON.parse(body);
cb(null, result.data ? result.data : result);
});
};
};
module.exports.middleware = function(options) {
return new Faceplate(options).middleware();
};