Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Support for SAPO Connect (OAuth) #150

Closed
wants to merge 1 commit into from

1 participant

@cpinto

See http://id.sapo.pt/connect (turn on Google Translate)

@cpinto

Still missing a few bits, sorry.

@cpinto cpinto closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 8, 2012
  1. @cpinto
This page is out of date. Refresh to see the latest.
View
2  socialregistration/clients/oauth.py
@@ -16,7 +16,7 @@ class OAuthError(Exception):
class OAuth(Client):
"""
- Base class for OAuth1 services such as Twitter, LinkedIn and Tumblr.
+ Base class for OAuth1 services such as Twitter, LinkedIn, SAPO and Tumblr.
"""
# The API key provided by the service
View
0  socialregistration/contrib/sapo/__init__.py
No changes.
View
4 socialregistration/contrib/sapo/admin.py
@@ -0,0 +1,4 @@
+from django.contrib import admin
+from socialregistration.contrib.sapo.models import SapoProfile
+
+admin.site.register(SapoProfile)
View
14 socialregistration/contrib/sapo/auth.py
@@ -0,0 +1,14 @@
+from django.contrib.auth.backends import ModelBackend
+from django.contrib.sites.models import Site
+from socialregistration.contrib.sapo.models import SapoProfile
+
+
+class SapoAuth(ModelBackend):
+ def authenticate(self, sapo=None):
+ try:
+ return SapoProfile.objects.get(
+ sapo=sapo,
+ site=Site.objects.get_current()
+ ).user
+ except SapoProfile.DoesNotExist:
+ return None
View
37 socialregistration/contrib/sapo/client.py
@@ -0,0 +1,37 @@
+from django.conf import settings
+from django.contrib.sites.models import Site
+from django.core.urlresolvers import reverse
+from socialregistration.clients.oauth import OAuth
+from socialregistration.settings import SESSION_KEY
+import json
+import urlparse
+
+class Sapo(OAuth):
+ api_key = getattr(settings, 'SAPO_CONSUMER_KEY', '')
+ secret_key = getattr(settings, 'SAPO_CONSUMER_SECRET_KEY', '')
+
+ request_token_url = 'https://id.sapo.pt/oauth/request_token'
+ access_token_url = 'https://id.sapo.pt/oauth/access_token'
+ auth_url = 'https://id.sapo.pt/oauth/authorize'
+ authentication_url = 'https://id.sapo.pt/oauth/authenticate'
+
+ def get_callback_url(self):
+ if self.is_https():
+ return urlparse.urljoin(
+ 'https://%s' % Site.objects.get_current().domain,
+ reverse('socialregistration:sapo:callback'))
+ return urlparse.urljoin(
+ 'http://%s' % Site.objects.get_current().domain,
+ reverse('socialregistration:sapo:callback'))
+
+ def get_user_info(self):
+ if self._user_info is None:
+ self._user_info = json.loads(
+ self.request('https://services.sapo.pt/SSO/GetPublicProfile'))['response']['user']
+ return self._user_info
+
+ @staticmethod
+ def get_session_key():
+ return '%ssapo' % SESSION_KEY
+
+
View
50 socialregistration/contrib/sapo/models.py
@@ -0,0 +1,50 @@
+from django.contrib.auth import authenticate
+from django.contrib.auth.models import User
+from django.contrib.sites.models import Site
+from django.db import models
+from socialregistration.signals import connect
+
+class SapoProfile(models.Model):
+ user = models.ForeignKey(User, unique=True)
+ site = models.ForeignKey(Site, default=Site.objects.get_current)
+ sapo = models.CharField(max_length=100)
+
+ def __unicode__(self):
+ try:
+ return u'%s: %s' % (self.user, self.sapo_name)
+ except User.DoesNotExist:
+ return u'None'
+
+ def authenticate(self):
+ return authenticate(sapo=self.sapo)
+
+class SapoRequestToken(models.Model):
+ profile = models.OneToOneField(SapoProfile, related_name='request_token')
+ oauth_token = models.CharField(max_length=80)
+ oauth_token_secret = models.CharField(max_length=80)
+
+class SapoAccessToken(models.Model):
+ profile = models.OneToOneField(SapoProfile, related_name='access_token')
+ oauth_token = models.CharField(max_length=80)
+ oauth_token_secret = models.CharField(max_length=80)
+
+def save_sapo_token(sender, user, profile, client, **kwargs):
+ try:
+ SapoRequestToken.objects.get(profile=profile).delete()
+ except SapoRequestToken.DoesNotExist:
+ pass
+ try:
+ SapoAccessToken.objects.get(profile=profile).delete()
+ except SapoAccessToken.DoesNotExist:
+ pass
+
+ SapoRequestToken.objects.create(profile=profile,
+ oauth_token=client.get_request_token().key,
+ oauth_token_secret=client.get_request_token().secret)
+
+ SapoAccessToken.objects.create(profile=profile,
+ oauth_token=client.get_access_token().key,
+ oauth_token_secret=client.get_access_token().secret)
+
+connect.connect(save_sapo_token, sender=SapoProfile,
+ dispatch_uid='socialregistration_sapo_token')
View
3  socialregistration/contrib/sapo/templates/socialregistration/sapo/sapo.html
@@ -0,0 +1,3 @@
+{% if error %}
+ <p>{{ error }}</p>
+{% endif %}
View
7 socialregistration/contrib/sapo/templates/socialregistration/sapo/sapo_button.html
@@ -0,0 +1,7 @@
+<form class="connect-button" name="login" method="post" action="{% url socialregistration:sapo:redirect %}">
+{% csrf_token %}
+{% if next %}
+ <input type="hidden" name="next" value="{{ next }}" />
+{% endif %}
+<input type="image" onclick="this.form.submit();" src="{% if button %}{{ button }}{% else %}http://platform.tumblr.com/v1/share_2.png{% endif %}" />
+</form>
View
0  socialregistration/contrib/sapo/templatetags/__init__.py
No changes.
View
6 socialregistration/contrib/sapo/templatetags/sapo.py
@@ -0,0 +1,6 @@
+from django import template
+from socialregistration.templatetags import button
+
+register = template.Library()
+
+register.tag('sapo_button', button('socialregistration/sapo/sapo_button.html'))
View
47 socialregistration/contrib/sapo/tests.py
@@ -0,0 +1,47 @@
+from django.conf import settings
+from django.core.urlresolvers import reverse
+from django.test import TestCase
+from socialregistration.contrib.sapo.models import SapoProfile
+from socialregistration.tests import TemplateTagTest, OAuthTest
+import json
+import urllib
+
+
+class TestTemplateTag(TemplateTagTest, TestCase):
+ def get_tag(self):
+ return 'sapo', 'sapo_button'
+
+class TestSapo(OAuthTest, TestCase):
+ profile = SapoProfile
+
+ def get_redirect_url(self):
+ return reverse('socialregistration:sapo:redirect')
+
+ def get_callback_url(self):
+ return reverse('socialregistration:sapo:callback')
+
+ def get_setup_callback_url(self):
+ return reverse('socialregistration:sapo:setup')
+
+ def get_redirect_mock_response(self, *args, **kwargs):
+ return {'status': '200'}, urllib.urlencode({
+ 'oauth_token': '123',
+ 'oauth_token_secret': '456'})
+
+ def get_callback_mock_response(self, *args, **kwargs):
+ return {'status': '200'}, urllib.urlencode({
+ 'oauth_token': '456',
+ 'oauth_token_secret': '789'})
+
+ def get_setup_callback_mock_response(self, *args, **kwargs):
+ return {'status': '200'}, json.dumps({
+ 'response': {
+ 'user': {
+ 'name': '123'}}})
+
+ def create_profile(self, user):
+ SapoProfile.objects.create(user=user, sapo='123')
+
+class TestAuthenticationBackend(TestCase):
+ def test_authentication_backend_should_be_configured_in_settings(self):
+ self.assertTrue('socialregistration.contrib.sapo.auth.SapoAuth' in settings.AUTHENTICATION_BACKENDS)
View
11 socialregistration/contrib/sapo/urls.py
@@ -0,0 +1,11 @@
+from django.conf import settings
+from django.conf.urls.defaults import *
+from socialregistration.contrib.sapo.views import SapoRedirect, \
+ SapoCallback, SapoSetup
+
+
+urlpatterns = patterns('',
+ url('^redirect/$', SapoRedirect.as_view(), name='redirect'),
+ url('^callback/$', SapoCallback.as_view(), name='callback'),
+ url('^setup/$', SapoSetup.as_view(), name='setup'),
+)
View
24 socialregistration/contrib/sapo/views.py
@@ -0,0 +1,24 @@
+from django.core.urlresolvers import reverse
+from socialregistration.contrib.sapo.client import Sapo
+from socialregistration.contrib.sapo.models import SapoProfile
+from socialregistration.views import OAuthRedirect, OAuthCallback, SetupCallback
+
+class SapoRedirect(OAuthRedirect):
+ client = Sapo
+ template_name = 'socialregistration/sapo/sapo.html'
+
+class SapoCallback(OAuthCallback):
+ client = Sapo
+ template_name = 'socialregistration/sapo/sapo.html'
+
+ def get_redirect(self):
+ return reverse('socialregistration:sapo:setup')
+
+class SapoSetup(SetupCallback):
+ client = Sapo
+ profile = SapoProfile
+ template_name = 'socialregistration/sapo/sapo.html'
+
+ def get_lookup_kwargs(self, request, client):
+ return {'sapo': client.get_user_info()['name']}
+
View
5 socialregistration/urls.py
@@ -39,6 +39,11 @@
url(r'^tumblr/', include('socialregistration.contrib.tumblr.urls',
namespace='tumblr')))
+if 'socialregistration.contrib.sapo' in settings.INSTALLED_APPS:
+ urlpatterns = urlpatterns + patterns('',
+ url(r'^sapo/', include('socialregistration.contrib.sapo.urls',
+ namespace='sapo')))
+
urlpatterns = urlpatterns + patterns('',
url(r'^setup/$', Setup.as_view(), name='setup'),
url(r'^logout/$', Logout.as_view(), name='logout'),
Something went wrong with that request. Please try again.