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