]> git.openstreetmap.org Git - osqa.git/blobdiff - forum_modules/facebookauth/authentication.py
Merge pull request #26 from udacity/remove_dj_version
[osqa.git] / forum_modules / facebookauth / authentication.py
index 55cf16d88b4d986de0169b96b0be8d4a861fa2a4..b80c47767ff08846e8b49e960e6c675d30bf5871 100644 (file)
@@ -12,14 +12,8 @@ from django.utils.translation import ugettext as _
 
 import settings
 
-try:
-    from json import load as load_json
-except Exception:
-    from django.utils.simplejson import JSONDecoder
+from json import load as load_json
 
-    def load_json(json):
-        decoder = JSONDecoder()
-        return decoder.decode(json.read())
 
 class FacebookAuthConsumer(AuthenticationConsumer):
 
@@ -33,7 +27,7 @@ class FacebookAuthConsumer(AuthenticationConsumer):
         facebook_api_authentication_url = "https://graph.facebook.com/oauth/authorize?" + urlencode(args)
 
         return facebook_api_authentication_url
-    
+
     def process_authentication_request(self, request):
         try:
             args = dict(client_id=settings.FB_API_KEY, redirect_uri="%s%s" % (django_settings.APP_URL, request.path))
@@ -44,16 +38,22 @@ class FacebookAuthConsumer(AuthenticationConsumer):
             response = cgi.parse_qs(urlopen("https://graph.facebook.com/oauth/access_token?" + urlencode(args)).read())
             access_token = response["access_token"][-1]
 
+
+            user_data = self.get_user_data(access_token)
+            assoc_key = user_data["id"]
+
             # Store the access token in cookie
-            request.session["assoc_key"] = access_token
+            request.session["access_token"] = access_token
+            request.session["assoc_key"] = assoc_key
 
-            return access_token
+            # Return the association key
+            return assoc_key
         except Exception, e:
             logging.error("Problem during facebook authentication: %s" % e)
             raise InvalidAuthentication(_("Something wrond happened during Facebook authentication, administrators will be notified"))
 
-    def get_user_data(self, assoc_key):
-        profile = load_json(urlopen("https://graph.facebook.com/me?" + urlencode(dict(access_token=assoc_key))))
+    def get_user_data(self, access_token):
+        profile = load_json(urlopen("https://graph.facebook.com/me?" + urlencode(dict(access_token=access_token))))
 
         name = profile["name"]
 
@@ -69,6 +69,7 @@ class FacebookAuthConsumer(AuthenticationConsumer):
 
         # Return the user data.
         return {
+            'id' : profile['id'],
             'username': name,
             'email': email,
         }