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