django-saml2-auth
django-saml2-auth copied to clipboard
Update user informations from SAML2/Okta
Is there a way to update all user informations (first name, last name, email) from SAML2 provider at each login? How? Thanks. ;)
OK, I've seen there is possibility to have a trigger after login, but it would be nice to have both the current user and the SAML2 credentials in the parameters. :D
I want/need the same. It'd be nice to include a stock method that does the update. I ended up with:
def update_user(user_identity):
user_email = user_identity[settings.SAML2_AUTH.get('ATTRIBUTES_MAP', {}).get('email', 'Email')][0]
user_name = user_identity[settings.SAML2_AUTH.get('ATTRIBUTES_MAP', {}).get('username', 'UserName')][0]
user_first_name = user_identity[settings.SAML2_AUTH.get('ATTRIBUTES_MAP', {}).get('first_name', 'FirstName')][0]
user_last_name = user_identity[settings.SAML2_AUTH.get('ATTRIBUTES_MAP', {}).get('last_name', 'LastName')][0]
u = User.objects.get(username=user_name)
u.email = user_email
u.first_name = user_first_name
u.last_name = user_last_name
u.save()
@ambsw-technology where did you put that function? I am trying to accomplish a similar feat (updating the user's department in my app). I can't tell that the function is even being called. Can you share the relevant portion of your settings.py and where you have the update_user() function? Thanks!
@kherrett
SAML2_AUTH = {
...
'TRIGGER': {
'BEFORE_LOGIN': 'app_name.saml.update_user',
},
I assume this method works in the base application, but I'm now working on a fork/branch that was refactored to make every part of the system pluggable. That lets me do things like this and this.
FYI here's my method (in app_name/saml.py
):
def update_user(user_identity):
user_email = user_identity[settings.SAML2_AUTH.get('ATTRIBUTES_MAP', {}).get('email', 'Email')][0]
user_name = user_identity[settings.SAML2_AUTH.get('ATTRIBUTES_MAP', {}).get('username', 'UserName')][0]
user_first_name = user_identity[settings.SAML2_AUTH.get('ATTRIBUTES_MAP', {}).get('first_name', 'FirstName')][0]
user_last_name = user_identity[settings.SAML2_AUTH.get('ATTRIBUTES_MAP', {}).get('last_name', 'LastName')][0]
u = User.objects.get(username=user_name)
u.email = user_email
u.first_name = user_first_name
u.last_name = user_last_name
u.save()