diff options
author | hrcerqueira <hrcerqueira@gmail.com> | 2010-01-22 01:39:13 +0000 |
---|---|---|
committer | hrcerqueira <hrcerqueira@gmail.com> | 2010-01-22 01:39:13 +0000 |
commit | d305ad98090558eb872c97f69addd2163f8ba5bd (patch) | |
tree | 08472efc2490125d56c6dd3274f4e06fd2db147f /fbconnect | |
parent | c18bda49f13afb4e2eaaf9ccb35098ee174e3afc (diff) | |
download | askbot-d305ad98090558eb872c97f69addd2163f8ba5bd.tar.gz askbot-d305ad98090558eb872c97f69addd2163f8ba5bd.tar.bz2 askbot-d305ad98090558eb872c97f69addd2163f8ba5bd.zip |
Handle the case were user logs in with facebook for the first time.
Diffstat (limited to 'fbconnect')
-rwxr-xr-x | fbconnect/__init__.py | 0 | ||||
-rwxr-xr-x | fbconnect/fb.py | 70 | ||||
-rwxr-xr-x | fbconnect/forms.py | 8 | ||||
-rwxr-xr-x | fbconnect/models.py | 6 | ||||
-rwxr-xr-x | fbconnect/tests.py | 23 | ||||
-rwxr-xr-x | fbconnect/urls.py | 11 | ||||
-rwxr-xr-x | fbconnect/views.py | 63 |
7 files changed, 181 insertions, 0 deletions
diff --git a/fbconnect/__init__.py b/fbconnect/__init__.py new file mode 100755 index 00000000..e69de29b --- /dev/null +++ b/fbconnect/__init__.py diff --git a/fbconnect/fb.py b/fbconnect/fb.py new file mode 100755 index 00000000..7aeda131 --- /dev/null +++ b/fbconnect/fb.py @@ -0,0 +1,70 @@ +from django.conf import settings +from time import time +from datetime import datetime +from urllib import urlopen, urlencode +from json import load as load_json +import md5 +import logging + +REST_SERVER = 'http://api.facebook.com/restserver.php' + +def generate_sig(values): + keys = [] + + for key in sorted(values.keys()): + keys.append(key) + + signature = ''.join(['%s=%s' % (key, values[key]) for key in keys]) + settings.FB_SECRET + return md5.new(signature).hexdigest() + +def check_cookies_signature(cookies): + API_KEY = settings.FB_API_KEY + + values = {} + + for key in cookies.keys(): + if (key.startswith(API_KEY + '_')): + values[key.replace(API_KEY + '_', '')] = cookies[key] + + return generate_sig(values) == cookies[API_KEY] + +def get_user_data(cookies): + request_data = { + 'method': 'Users.getInfo', + 'api_key': settings.FB_API_KEY, + 'call_id': time(), + 'v': '1.0', + 'uids': cookies[settings.FB_API_KEY + '_user'], + 'fields': 'name,first_name,last_name', + 'format': 'json', + } + + request_data['sig'] = generate_sig(request_data) + fb_response = load_json(urlopen(REST_SERVER, urlencode(request_data))) + return fb_response[0] + + +def delete_cookies(): + API_KEY = settings.FB_API_KEY + + response.delete_cookie(API_KEY + '_user') + response.delete_cookie(API_KEY + '_session_key') + response.delete_cookie(API_KEY + '_expires') + response.delete_cookie(API_KEY + '_ss') + response.delete_cookie(API_KEY) + response.delete_cookie('fbsetting_' + API_KEY) + +def check_session_expiry(cookies): + return datetime.fromtimestamp(float(cookies[settings.FB_API_KEY+'_expires'])) > datetime.now() + +STATES = { + 'FIRSTTIMER': 1, + 'SESSIONEXPIRED': 2, +} + +def get_user_state(request): + if settings.FB_API_KEY in request.COOKIES: + if check_cookies_signature(request.COOKIES): + if check_session_expiry(request.COOKIES): + return STATES['FIRSTTIMER'] + diff --git a/fbconnect/forms.py b/fbconnect/forms.py new file mode 100755 index 00000000..94f86816 --- /dev/null +++ b/fbconnect/forms.py @@ -0,0 +1,8 @@ +from django_authopenid.forms import NextUrlField, UserNameField, UserEmailField + +from django import forms + +class FBConnectRegisterForm(forms.Form): + next = NextUrlField() + username = UserNameField() + email = UserEmailField() diff --git a/fbconnect/models.py b/fbconnect/models.py new file mode 100755 index 00000000..33a723e8 --- /dev/null +++ b/fbconnect/models.py @@ -0,0 +1,6 @@ +from django.db import models +from django.contrib.auth.models import User + +class FBAssociation(models.Model): + user = models.ForeignKey(User) + fbuid = models.TextField(max_length=12) diff --git a/fbconnect/tests.py b/fbconnect/tests.py new file mode 100755 index 00000000..a6f218a9 --- /dev/null +++ b/fbconnect/tests.py @@ -0,0 +1,23 @@ +"""
+This file demonstrates two different styles of tests (one doctest and one
+unittest). These will both pass when you run "manage.py test".
+
+Replace these with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+
+class SimpleTest(TestCase):
+ def test_basic_addition(self):
+ """
+ Tests that 1 + 1 always equals 2.
+ """
+ self.failUnlessEqual(1 + 1, 2)
+
+__test__ = {"doctest": """
+Another way to test that 1 + 1 is equal to 2.
+
+>>> 1 + 1 == 2
+True
+"""}
+
diff --git a/fbconnect/urls.py b/fbconnect/urls.py new file mode 100755 index 00000000..e4048151 --- /dev/null +++ b/fbconnect/urls.py @@ -0,0 +1,11 @@ +from django.conf.urls.defaults import * +from django.utils.translation import ugettext as _ +from django.views.generic.simple import direct_to_template +from views import signin, register + +urlpatterns = patterns('', + url(r'^xd_receiver$', direct_to_template, {'template': 'fbconnect/xd_receiver.html'}, name='xd_receiver'), + url(r'^%s' % _('signin/'), signin, name="fb_signin"), + url(r'^%s' % _('register/'), register, name="fb_user_register"), + +) diff --git a/fbconnect/views.py b/fbconnect/views.py new file mode 100755 index 00000000..2acb1dc0 --- /dev/null +++ b/fbconnect/views.py @@ -0,0 +1,63 @@ +from django.shortcuts import render_to_response as render
+from django.template import RequestContext +from django.http import HttpResponseRedirect +from django.utils.safestring import mark_safe +from django.core.urlresolvers import reverse +from django.contrib.auth.models import User +from django.contrib.auth import login +from models import FBAssociation +from forum.forms import EditUserEmailFeedsForm +from django.conf import settings + +import fb +import forms + +import logging + +def signin(request): + user_state = fb.get_user_state(request) + + if user_state == fb.STATES['FIRSTTIMER']: + return HttpResponseRedirect(reverse('fb_user_register')) + + return HttpResponseRedirect('/') + +def register(request): + if fb.get_user_state(request) == fb.STATES['FIRSTTIMER']: + user_data = fb.get_user_data(request.COOKIES) + + if 'bnewaccount' in request.POST.keys(): + form1 = forms.FBConnectRegisterForm(request.POST) + email_feeds_form = EditUserEmailFeedsForm(request.POST) + + if (form1.is_valid() and email_feeds_form.is_valid()): + tmp_pwd = User.objects.make_random_password()
+ user_ = User.objects.create_user(form1.cleaned_data['username'],
+ form1.cleaned_data['email'], tmp_pwd)
+
+ user_.set_unusable_password() + + uassoc = FBAssociation(user=user_, fbuid=user_data['uid']) + uassoc.save() + + user_.backend = "django.contrib.auth.backends.ModelBackend"
+ login(request, user_)
+ email_feeds_form.save(user_) + + return HttpResponseRedirect('/') + else: + form1 = forms.FBConnectRegisterForm(initial={
+ 'next': '/',
+ 'username': user_data['name'],
+ 'email': '',
+ }) + + email_feeds_form = EditUserEmailFeedsForm() + + return render('authopenid/complete.html', {
+ 'form1': form1,
+ 'email_feeds_form': email_feeds_form,
+ 'provider':mark_safe('facebook'),
+ 'login_type':'facebook',
+ 'gravatar_faq_url':reverse('faq') + '#gravatar',
+ }, context_instance=RequestContext(request)) |