-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from Kagiso-Future-Media/sso
Single sign on
- Loading branch information
Showing
21 changed files
with
468 additions
and
328 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
from django.contrib.auth.backends import ModelBackend | ||
from django.db.models.signals import pre_save | ||
|
||
from . import http | ||
from .auth_api_client import AuthApiClient | ||
from .exceptions import CASUnexpectedStatusCode, EmailNotConfirmedError | ||
from .models import KagisoUser, save_user_to_cas | ||
from .exceptions import AuthAPIUnexpectedStatusCode, EmailNotConfirmedError | ||
from .models import KagisoUser | ||
|
||
|
||
class KagisoBackend(ModelBackend): | ||
|
@@ -17,8 +16,6 @@ class KagisoBackend(ModelBackend): | |
# Django AllAuth does this: | ||
# credentials = {'email': '[email protected], 'password': 'open'} | ||
def authenticate(self, email=None, username=None, password=None, **kwargs): | ||
cas_credentials = kwargs.get('cas_credentials') | ||
|
||
email = username if not email else email | ||
|
||
payload = { | ||
|
@@ -34,29 +31,16 @@ def authenticate(self, email=None, username=None, password=None, **kwargs): | |
if strategy: | ||
payload['strategy'] = strategy | ||
|
||
auth_api_client = AuthApiClient(cas_credentials) | ||
auth_api_client = AuthApiClient() | ||
status, data = auth_api_client.call('sessions', 'POST', payload) | ||
|
||
if status == http.HTTP_200_OK: | ||
local_user = KagisoUser.objects.filter(id=data['id']).first() | ||
if local_user: | ||
local_user.override_cas_credentials(cas_credentials) | ||
else: | ||
try: | ||
# Do not on save sync to CAS, as we just got the user's | ||
# data from CAS, and nothing has changed in the interim | ||
pre_save.disconnect(save_user_to_cas, sender=KagisoUser) | ||
local_user = KagisoUser() | ||
local_user.set_password(password) | ||
local_user.build_from_cas_data(data) | ||
local_user.save() | ||
finally: | ||
pre_save.connect(save_user_to_cas, sender=KagisoUser) | ||
user = KagisoUser.sync_user_data_locally(data) | ||
elif status == http.HTTP_404_NOT_FOUND: | ||
return None | ||
elif status == http.HTTP_422_UNPROCESSABLE_ENTITY: | ||
raise EmailNotConfirmedError() | ||
else: | ||
raise CASUnexpectedStatusCode(status, data) | ||
raise AuthAPIUnexpectedStatusCode(status, data) | ||
|
||
return local_user | ||
return user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import models, migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('kagiso_auth', '0006_auto_20150514_1045'), | ||
] | ||
|
||
operations = [ | ||
migrations.RenameField( | ||
model_name='kagisouser', | ||
old_name='date_joined', | ||
new_name='created', | ||
), | ||
migrations.AddField( | ||
model_name='kagisouser', | ||
name='created_via', | ||
field=models.CharField(null=True, max_length=100, blank=True), | ||
preserve_default=True, | ||
), | ||
migrations.AddField( | ||
model_name='kagisouser', | ||
name='last_sign_in_via', | ||
field=models.CharField(null=True, max_length=100, blank=True), | ||
preserve_default=True, | ||
), | ||
] |
Oops, something went wrong.