1

I've successfully implemented a baseline Django/pysaml2 integration with SAML assertion using the djangosaml2 package. Users are created dynamically based on a successful authentication which is great! Now I would like to inject a short list of custom attributes into my Django 1.6 "extended" user model.

The djangosaml2 documentation states (see: https://pypi.python.org/pypi/djangosaml2#user-attributes) that I should use the provided Django signal:

...for these cases djangosaml2 provides a Django signal that you can listen to. In order to do so you can add the following code to your app:

from djangosaml2.signals import pre_user_save

def custom_update_user(sender=user, attributes=attributes, user_modified=user_modified)
    ...
    return True  # I modified the user object

Here are my questions:

  • Where do I place the above signal code?
  • How do I tell my extended user model to set an employee id if my extended user model look like this?

models.py

from django.db import models
from django.contrib.auth.models import User

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    employee_id = models.CharField(blank=True,max_length=150)
Derek Nutile
  • 211
  • 3
  • 10

1 Answers1

0

Alternative to signals approach is to use code in this pull request and in settings.py define:

SAML_PROFILE_MODULE = 'app.UserProfile'

SAML_ATTRIBUTE_MAPPING = {
    'samlAttributeThatDefinesID': ('employee_id',),
}

Values in SAML_ATTRIBUTE_MAPPING should be tuples of field names from app.UserProfile or auth.User models that you want to populate with saml attribute given as the key.

Disclaimer: this PR is still open and it is the question if it will make its way to the upstream repo.

Bula
  • 1,590
  • 1
  • 14
  • 33
  • Great solution @Bula! I do want to note that your solution works well if the extended user profile already exists, but it will not create the profile even though the djangosaml2 package will create the user if SAML_CREATE_UNKNOWN_USER is True. I worked around this by using a post_save signal in my project similar to this [article](http://stackoverflow.com/questions/1910359/creating-a-extended-user-profile). – Derek Nutile Feb 25 '14 at 00:23
  • Thanks @DerekNutile, glad that this solution was of use to you. – Bula Feb 25 '14 at 12:21