Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Setup for a Django Project #12

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
FIX: Allow names to be optional in the SAML Response
  • Loading branch information
RevolutionTech committed Jan 30, 2018
commit cc0f13105076eae10c64936a76a7ddbb040da4dc
11 changes: 9 additions & 2 deletions saml_service_provider/auth_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@

class SAMLServiceProviderBackend(object):

@staticmethod
def get_attribute_or_none(attributes, attribute_name):
try:
return attributes[attribute_name][0]
except IndexError:
return

def update_attributes(self, user, attributes):
# Set name
user.first_name = attributes['First name'][0]
user.last_name = attributes['Last name'][0]
user.first_name = self.get_attribute_or_none(attributes, 'First name') or ''
user.last_name = self.get_attribute_or_none(attributes, 'Last name') or ''
user.save()

def authenticate(self, saml_authentication=None):
Expand Down
22 changes: 22 additions & 0 deletions saml_service_provider/tests/test_auth_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,28 @@ def testNewUserIsCreatedAndAuthenticated(self):
# Verify that a new user was created
self.assertEquals(User.objects.count(), num_users + 1)

def testNewUserWithoutNamesIsCreated(self):
# Count the number of users
num_users = User.objects.count()

# Authenticate with the SAMLServiceProvider backend
saml_authentication = mock.Mock(
is_authenticated=lambda: True,
get_attributes=lambda: {'First name': [], 'Last name': []},
get_nameid=lambda: self.NEW_USER_USERNAME
)
user = self.auth_backend.authenticate(saml_authentication)

# Verify that the user authenticated is the new user
self.assertEquals(user, User.objects.get(username=self.NEW_USER_USERNAME))

# Verify that the user does not have a name
self.assertEquals(user.first_name, '')
self.assertEquals(user.last_name, '')

# Verify that a new user was created
self.assertEquals(User.objects.count(), num_users + 1)

def testGetUserUsesAuthUser(self):
# Verify that the user is looked up by PK
self.assertEquals(self.auth_backend.get_user(self.user.pk), self.user)
Expand Down