Question

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)
Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top