]> git.openstreetmap.org Git - osqa.git/blob - forum_modules/facebookauth/authentication.py
b80c47767ff08846e8b49e960e6c675d30bf5871
[osqa.git] / forum_modules / facebookauth / authentication.py
1 # -*- coding: utf-8 -*-
2
3 import cgi
4 import logging
5
6 from urllib import urlopen,  urlencode
7 from forum.authentication.base import AuthenticationConsumer, ConsumerTemplateContext, InvalidAuthentication
8
9 from django.conf import settings as django_settings
10 from django.utils.encoding import smart_unicode
11 from django.utils.translation import ugettext as _
12
13 import settings
14
15 from json import load as load_json
16
17
18 class FacebookAuthConsumer(AuthenticationConsumer):
19
20     def prepare_authentication_request(self, request, redirect_to):
21         args = dict(
22             client_id=settings.FB_API_KEY,
23             redirect_uri="%s%s" % (django_settings.APP_URL, redirect_to),
24             scope="email"
25         )
26
27         facebook_api_authentication_url = "https://graph.facebook.com/oauth/authorize?" + urlencode(args)
28
29         return facebook_api_authentication_url
30
31     def process_authentication_request(self, request):
32         try:
33             args = dict(client_id=settings.FB_API_KEY, redirect_uri="%s%s" % (django_settings.APP_URL, request.path))
34
35             args["client_secret"] = settings.FB_APP_SECRET  #facebook APP Secret
36
37             args["code"] = request.GET.get("code", None)
38             response = cgi.parse_qs(urlopen("https://graph.facebook.com/oauth/access_token?" + urlencode(args)).read())
39             access_token = response["access_token"][-1]
40
41
42             user_data = self.get_user_data(access_token)
43             assoc_key = user_data["id"]
44
45             # Store the access token in cookie
46             request.session["access_token"] = access_token
47             request.session["assoc_key"] = assoc_key
48
49             # Return the association key
50             return assoc_key
51         except Exception, e:
52             logging.error("Problem during facebook authentication: %s" % e)
53             raise InvalidAuthentication(_("Something wrond happened during Facebook authentication, administrators will be notified"))
54
55     def get_user_data(self, access_token):
56         profile = load_json(urlopen("https://graph.facebook.com/me?" + urlencode(dict(access_token=access_token))))
57
58         name = profile["name"]
59
60         # Check whether the length if the email is greater than 75, if it is -- just replace the email
61         # with a blank string variable, otherwise we're going to have trouble with the Django model.
62         email = smart_unicode(profile['email'])
63         if len(email) > 75:
64             email = ''
65
66         # If the name is longer than 30 characters - leave it blank
67         if len(name) > 30:
68             name = ''
69
70         # Return the user data.
71         return {
72             'id' : profile['id'],
73             'username': name,
74             'email': email,
75         }
76
77 class FacebookAuthContext(ConsumerTemplateContext):
78     mode = 'BIGICON'
79     type = 'CUSTOM'
80     weight = 100
81     human_name = 'Facebook'
82     code_template = 'modules/facebookauth/button.html'
83     extra_css = []
84
85     API_KEY = settings.FB_API_KEY