-
Notifications
You must be signed in to change notification settings - Fork 0
/
facebook_lib.py
335 lines (253 loc) · 11.8 KB
/
facebook_lib.py
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import facebook
import urllib
import logging
from django.conf import settings
from django.db import IntegrityError
from helpers import ip_address
from models import FacebookUser, FacebookUserIP
# Find a JSON parser
try:
import json
_parse_json = lambda s: json.loads(s)
except ImportError:
try:
import simplejson
_parse_json = lambda s: simplejson.loads(s)
except ImportError:
# For Google AppEngine
from django.utils import simplejson
_parse_json = lambda s: simplejson.loads(s)
from sentry.client.handlers import SentryHandler
logger = logging.getLogger(__name__)
is_logging_setup = False
if not is_logging_setup:
is_logging_setup = True
logger.setLevel(settings.LOGGING_LEVEL)
logger.addHandler(SentryHandler())
class FacebookHelper(object):
def __init__(self, request,
allow_cookie_auth = False,
facebook_app_secret = None,
facebook_app_id = None,
cookie = None):
"""
The xxxHandler classes in the AppEngine example are something like
class-based views in Django. But it's difficult to do class-based views
in a thread-safe way, so it's cleaner and simpler just to instantiate
this helper class from within regular views.
"""
self.request = request
self.allow_cookie_auth = allow_cookie_auth
if facebook_app_secret:
self.facebook_app_secret = str(facebook_app_secret)
else:
self.facebook_app_secret = settings.FACEBOOK_APP_SECRET
if facebook_app_id:
self.facebook_app_id = str(facebook_app_id)
else:
self.facebook_app_id = settings.FACEBOOK_APP_ID
self.cookie = cookie
"""Provides access to the active Facebook user in self.current_user
The property is lazy-loaded on first access, using the cookie saved
by the Facebook JavaScript SDK to determine the user ID of the active
user. See http://developers.facebook.com/docs/authentication/ for
more information.
"""
@property
def current_user(self):
"""Returns the active user, or None if the user has not logged in."""
if not hasattr(self, "_current_user"):
self._current_user = None
if self.cookie:
cookie = self.cookie
else:
cookie = self.parse_auth()
if cookie:
# print ('we have cookie', cookie["uid"])
# Store a local instance of the user data so we don't need
# a round-trip to Facebook on every request
try:
user = FacebookUser.objects.get(uid=cookie["uid"])
except FacebookUser.DoesNotExist:
try:
graph = facebook.GraphAPI(cookie["access_token"])
attempt_counter = 0
ATTEMPT_LIMIT = 10
while attempt_counter < ATTEMPT_LIMIT:
try:
profile = graph.get_object("me")
break
except (IOError) as e:
attempt_counter += 1
if attempt_counter == ATTEMPT_LIMIT:
raise
logger.info('mplib.current_user', extra = {
'data': { 'exception': e }
})
except facebook.GraphAPIError:
user = None
else:
user = FacebookUser(pk=str(profile["id"]),
first_name=profile.get("first_name"),
last_name=profile.get("last_name"),
locale=profile.get("locale"),
gender=profile.get("gender", ''),
time_zone=profile.get('timezone', ''),
email=profile.get('email', ''),
access_token=cookie["access_token"])
try:
user.save()
except IntegrityError as e:
logger.info('IntegrityError saving user', extra = {
'data': { 'exception': e, 'profile': profile }
})
# this user has already been saved somehow, let's skip
# over this problem and grab him from the db
user = FacebookUser.objects.get(pk=str(profile["id"]))
ip = FacebookUserIP()
ip.fb_user = user
ip.ip_address = ip_address(self.request)
ip.save()
else:
if user.access_token != cookie["access_token"]:
user.access_token = cookie["access_token"]
ip = FacebookUserIP()
ip.fb_user = user
ip.ip_address = ip_address(self.request)
ip.save()
user.save()
self._current_user = user
return self._current_user
@property
def graph(self):
"""Returns a Graph API client for the current user."""
if not hasattr(self, "_graph"):
if self.current_user:
self._graph = facebook.GraphAPI(self.current_user.access_token)
else:
self._graph = facebook.GraphAPI()
return self._graph
@property
def classic(self):
"""Returns a Graph API client for the current user."""
if not hasattr(self, "_classic"):
if self.current_user:
self._classic = ClassicAPI(self.current_user.access_token)
else:
self._classic = ClassicAPI()
return self._classic
def parse_auth(self):
"""
looks for a request param or cookie in order to decode a facebook object
the returned value can then be used to do something like:
graph = facebook.GraphAPI(cookie["access_token"])
profile = graph.get_object("me")
"""
signed_request = self.request.REQUEST.get('signed_request', None)
if signed_request:
signed_request = decode_signed_request(signed_request, self.facebook_app_secret)
if signed_request and 'uid' in signed_request:
return signed_request
else:
if self.allow_cookie_auth:
return facebook.get_user_from_cookie(
self.request.COOKIES, self.facebook_app_id, self.facebook_app_secret)
return None
def is_fan(self, page_uid):
return self.classic.request('method/pages.isfan', args = { 'page_id': page_uid, 'format': 'json' })
def is_admin(self, page_uid):
return self.classic.request('method/pages.isadmin', args = { 'page_id': page_uid, 'format': 'json' })
def fql(self, query):
return self.classic.request('method/fql.query', args = { 'query': query, 'format': 'json' })
def has_app_added(self):
print ('checking if app is added')
return self.classic.request('method/users.isAppUser', args = { 'format': 'json' })
def has_app_permission(self, permission):
print ('checking if app permission is added')
return self.classic.request('method/users.hasAppPermission', args = { 'format': 'json', 'ext_perm': permission })
def decode_signed_request(signed_request, secret):
"""
http://developers.facebook.com/docs/authentication/canvas
"""
import hmac, base64, hashlib
if not signed_request:
return None
# watch out for split returning a non-correct number of elements
try:
sig, payload = map(str, signed_request.split('.', 1))
except ValueError:
return None
def pad(string):
if len(string) % 4:
return string + '=' * (4 - len(string) % 4)
return string
try:
sig = base64.urlsafe_b64decode(pad(sig))
data = base64.urlsafe_b64decode(pad(payload))
data = _parse_json(data)
except:
return None
if data['algorithm'] != 'HMAC-SHA256':
return None
digest = hmac.new(secret, payload, hashlib.sha256).digest()
if str(digest) != sig:
return None
classic = {
'access_token': data.get('oauth_token', None),
'expires': data.get('expires', None) or None,
'uid': data.get('user_id', None),
'session_key': None,
}
return dict(data, **classic)
class ClassicAPI(object):
def __init__(self, access_token=None):
self.access_token = access_token
def request(self, path, args=None, post_args=None):
"""Fetches the given path in the Graph API.
We translate args to a valid query string. If post_args is given,
we send a POST request to the given path with the given arguments.
"""
if not args: args = {}
if self.access_token:
if post_args is not None:
post_args["access_token"] = self.access_token
else:
args["access_token"] = self.access_token
post_data = None if post_args is None else urllib.urlencode(post_args)
# we had some issues where the facebook server would hang up
# on us while we are trying to authenticate the request, here
# we try and attempt to loop a couple of times in case it's an
# intermittent issue.
# IOError: ('http protocol error', 0, 'got a bad status line', None)
ATTEMPT_LIMIT = 3
attempt = 0
while attempt < ATTEMPT_LIMIT:
try:
file = urllib.urlopen("https://api.facebook.com/" + path + "?" +
urllib.urlencode(args), post_data)
break
except (IOError) as e:
attempt += 1
logger.debug('ClassicAPI.request IOException', extra = {
'data': { 'exception': e, 'attempt': attempt }
})
if attempt == ATTEMPT_LIMIT:
raise
try:
response = file.read()
try:
response = _parse_json(response)
try:
if response.get("error_code"):
print (response)
raise facebook.GraphAPIError(response["error_code"],
response["error_msg"])
except AttributeError:
# not json, possibly a single value, live with it...
pass
except ValueError:
# not json, possibly a single value, live with it...
pass
finally:
file.close()
return response