]> git.openstreetmap.org Git - osqa.git/blob - forum_modules/facebookauth/authentication.py
deleting the test file
[osqa.git] / forum_modules / facebookauth / authentication.py
1 import hashlib\r
2 from time import time\r
3 from datetime import datetime\r
4 from urllib import urlopen,  urlencode\r
5 from forum.authentication.base import AuthenticationConsumer, ConsumerTemplateContext, InvalidAuthentication\r
6 from django.utils.translation import ugettext as _\r
7 \r
8 import settings\r
9 \r
10 try:\r
11     from json import load as load_json\r
12 except:\r
13     from django.utils.simplejson import JSONDecoder\r
14 \r
15     def load_json(json):\r
16         decoder = JSONDecoder()\r
17         return decoder.decode(json.read())\r
18 \r
19 class FacebookAuthConsumer(AuthenticationConsumer):\r
20     \r
21     def process_authentication_request(self, request):\r
22         API_KEY = settings.FB_API_KEY\r
23 \r
24         if API_KEY in request.COOKIES:\r
25             if self.check_cookies_signature(request.COOKIES):\r
26                 if self.check_session_expiry(request.COOKIES):\r
27                     return request.COOKIES[API_KEY + '_user']\r
28                 else:\r
29                     raise InvalidAuthentication(_('Sorry, your Facebook session has expired, please try again'))\r
30             else:\r
31                 raise InvalidAuthentication(_('The authentication with Facebook connect failed due to an invalid signature'))\r
32         else:\r
33             raise InvalidAuthentication(_('The authentication with Facebook connect failed, cannot find authentication tokens'))\r
34 \r
35     def generate_signature(self, values):\r
36         keys = []\r
37 \r
38         for key in sorted(values.keys()):\r
39             keys.append(key)\r
40 \r
41         signature = ''.join(['%s=%s' % (key,  values[key]) for key in keys]) + settings.FB_APP_SECRET\r
42         return hashlib.md5(signature).hexdigest()\r
43 \r
44     def check_session_expiry(self, cookies):\r
45         return datetime.fromtimestamp(float(cookies[settings.FB_API_KEY+'_expires'])) > datetime.now()\r
46 \r
47     def check_cookies_signature(self, cookies):\r
48         API_KEY = settings.FB_API_KEY\r
49 \r
50         values = {}\r
51 \r
52         for key in cookies.keys():\r
53             if (key.startswith(API_KEY + '_')):\r
54                 values[key.replace(API_KEY + '_',  '')] = cookies[key]\r
55 \r
56         return self.generate_signature(values) == cookies[API_KEY]\r
57 \r
58     def get_user_data(self, key):\r
59         request_data = {\r
60             'method': 'Users.getInfo',\r
61             'api_key': settings.FB_API_KEY,\r
62             'call_id': time(),\r
63             'v': '1.0',\r
64             'uids': key,\r
65             'fields': 'name,first_name,last_name,email',\r
66             'format': 'json',\r
67         }\r
68 \r
69         request_data['sig'] = self.generate_signature(request_data)\r
70         fb_response = load_json(urlopen(settings.REST_SERVER, urlencode(request_data)))[0]\r
71 \r
72         return {\r
73             'username': fb_response['first_name'] + ' ' + fb_response['last_name'],\r
74             'email': fb_response['email']\r
75         }\r
76 \r
77 class FacebookAuthContext(ConsumerTemplateContext):\r
78     mode = 'BIGICON'\r
79     type = 'CUSTOM'\r
80     weight = 100\r
81     human_name = 'Facebook'\r
82     code_template = 'modules/facebookauth/button.html'\r
83     extra_css = ["http://www.facebook.com/css/connect/connect_button.css"]\r
84 \r
85     API_KEY = settings.FB_API_KEY