Permalink
Browse files

Added tests and fixed a quazillion of bugs

  • Loading branch information...
Jonas Obrist
Jonas Obrist committed Aug 10, 2011
1 parent ace70b9 commit 28c1941199cb23226420d24bb690c6aa272a246f
View
@@ -4,3 +4,4 @@
/dist/
*.egg-info
/*env*/
+/htmlcov/
View
@@ -1,11 +1,14 @@
# -*- coding: utf-8 -*-
#!/usr/bin/env python
from django.conf.urls.defaults import patterns, url, include
+from django.http import HttpResponse
import sys
urlpatterns = patterns('',
url('^server/', include('simple_sso.sso_server.urls')),
url('^client/', include('simple_sso.sso_client.urls')),
+ url(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'admin/login.html'}),
+ url('^$', lambda request: HttpResponse('home'), name='root')
)
INSTALLED_APPS = [
@@ -24,17 +27,13 @@
}
}
-def teardown(state):
- from django.conf import settings
- # Restore the old settings.
- for key, value in state.items():
- setattr(settings, key, value)
+ROOT_URLCONF = 'runtests'
def run_tests():
from django.conf import settings
settings.configure(
INSTALLED_APPS = INSTALLED_APPS,
- ROOT_URLCONF = 'runtests',
+ ROOT_URLCONF = ROOT_URLCONF,
DATABASES = DATABASES,
TEST_RUNNER = 'django.test.simple.DjangoTestSuiteRunner',
SIMPLE_SSO_SERVER = '/server/',
View
@@ -25,7 +25,7 @@
url='http://github.com/ojii/django-simple-sso',
license='BSD',
packages=['simple_sso', 'simple_sso.sso_client', 'simple_sso.sso_server'],
- install_requires=['Django>=1.3', 'django-load'],
+ install_requires=['Django>=1.3', 'django-load', 'requests'],
include_package_data=True,
zip_safe=False
)
View
@@ -11,7 +11,7 @@ def build_signature(parameters, secret):
Parameters is a list of tuples.
"""
message = urllib.urlencode(sorted(parameters))
- return hmac.new(secret, message, hashlib.sha256).hexdigest()
+ return hmac.new(secret.encode('ascii'), message.encode('ascii'), hashlib.sha256).hexdigest()
def verify_signature(parameters, signature, secret):
"""
@@ -2,27 +2,19 @@
from django.contrib.auth.models import User, Permission
from django.contrib.contenttypes.models import ContentType
from django.utils import simplejson
+from simple_sso.utils import SIMPLE_KEYS
def load_json_user(json):
"""
Given a JSON string, returns a Django User instance.
"""
- simple_keys = [
- 'username',
- 'first_name',
- 'last_name',
- 'email',
- 'is_staff',
- 'is_superuser',
- ]
-
data = simplejson.loads(json)
try:
user = User.objects.get(username=data['username'])
except User.DoesNotExist:
user = User()
- for key in simple_keys:
+ for key in SIMPLE_KEYS:
setattr(user, key, data[key])
user.set_unusable_password()
user.save()
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
-# -*- coding: utf-8 -*-
from django.conf import settings
from django.contrib.auth import login
from django.contrib.auth.backends import ModelBackend
@@ -30,7 +29,7 @@ def get_request_token():
if 'request_token' not in data:
return False
params = [(key, value) for key,value in data.items() if key != 'signature']
- if not verify_signature(params, data['signature']):
+ if not verify_signature(params, data['signature'], settings.SIMPLE_SSO_SECRET):
return False
return data['request_token']
@@ -45,7 +44,7 @@ def verify_auth_token(data):
return False
auth_token = data['auth_token']
params = [('auth_token', auth_token), ('key', settings.SIMPLE_SSO_KEY)]
- signature = build_signature(params)
+ signature = build_signature(params, settings.SIMPLE_SSO_SECRET)
params.append(('signature', signature))
url = urljoin(settings.SIMPLE_SSO_SERVER, 'verify') + '/'
response = requests.get(url, dict(params))
@@ -57,7 +56,7 @@ def verify_auth_token(data):
if 'user' not in data:
return False
params = [(key, value) for key,value in data.items() if key != 'signature']
- if not verify_signature(params, data['signature']):
+ if not verify_signature(params, data['signature'], settings.SIMPLE_SSO_SECRET):
return False
return load_json_user(data['user'])
@@ -88,9 +87,9 @@ def login_view(request):
request.session['simple-sso-next'] = next
request_token = get_request_token()
if not request_token:
- raise HttpResponseBadRequest()
+ return HttpResponseBadRequest()
params = [('request_token', request_token), ('key', settings.SIMPLE_SSO_KEY)]
- signature = build_signature(params)
+ signature = build_signature(params, settings.SIMPLE_SSO_SECRET)
params.append(('signature', signature))
query_string = urllib.urlencode(params)
url = urljoin(settings.SIMPLE_SSO_SERVER, 'authorize') + '/'
@@ -104,7 +103,7 @@ def authenticate_view(request):
"""
user = verify_auth_token(request.GET)
if not user:
- raise HttpResponseBadRequest()
+ return HttpResponseBadRequest()
user.backend = "%s.%s" % (BACKEND.__module__, BACKEND.__class__.__name__)
login(request, user)
return HttpResponseRedirect(request.session.get('simple-sso-next', '/'))
@@ -48,7 +48,8 @@ def clean(self):
token = Token.objects.get(request_token=request_token, client=self.client, user__isnull=True)
except Token.DoesNotExist:
raise forms.ValidationError('Invalid request token')
- return token
+ data['token'] = token
+ return data
class VerificationForm(BaseForm):
@@ -64,4 +65,5 @@ def clean(self):
token = Token.objects.get(auth_token=auth_token, user__isnull=False, client=self.client)
except Token.DoesNotExist:
raise forms.ValidationError('Invalid auth token')
- return token
+ data['token'] = token
+ return data
@@ -2,23 +2,16 @@
from django.conf import settings
from django.utils import simplejson
from django_load.core import load_object
+from simple_sso.utils import SIMPLE_KEYS
def default_construct_user(user, client):
"""
Default user constructor. Ignores the client and returns the Django User
as a dictionary with the fields required by the specifications.
"""
- simple_keys = [
- 'username',
- 'first_name',
- 'last_name',
- 'email',
- 'is_staff',
- 'is_superuser',
- ]
data = {}
- for key in simple_keys:
+ for key in SIMPLE_KEYS:
data[key] = getattr(user, key)
data['permissions'] = []
for perm in user.user_permissions.select_related('content_type').all():
@@ -16,7 +16,7 @@ def request_token(request):
if form.is_valid():
token = Token.objects.create_for_client(form.client)
params = [('request_token', token.request_token)]
- signature = build_signature(params)
+ signature = build_signature(params, token.client.secret)
params.append(('signature', signature))
data = urllib.urlencode(params)
return HttpResponse(data)
@@ -28,11 +28,11 @@ def request_token(request):
def authorize(request):
form = AuthorizeForm(request.GET)
if form.is_valid():
- token = form.cleaned_data['request_token']
+ token = form.cleaned_data['token']
if request.user.is_authenticated():
url = urljoin(token.client.root_url, 'authenticate') + '/'
params = [('request_token', token.request_token), ('auth_token', token.auth_token)]
- signature = build_signature(params)
+ signature = build_signature(params, token.client.secret)
params.append(('signature', signature))
token.user = request.user
token.save()
@@ -48,10 +48,10 @@ def authorize(request):
def verify(request):
form = VerificationForm(request.GET)
if form.is_valid():
- token = form.cleaned_data['auth_token']
+ token = form.cleaned_data['token']
user = get_user_json(token.user, token.client)
params = [('user', user)]
- signature = build_signature(params)
+ signature = build_signature(params, token.client.secret)
params.append(('signature', signature))
data = urllib.urlencode(params)
token.delete()
Oops, something went wrong.

0 comments on commit 28c1941

Please sign in to comment.