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

Update backends.py #1259

Open
wants to merge 10 commits into
base: replace_cas
Choose a base branch
from
Open
3 changes: 3 additions & 0 deletions CONFIGURATION_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,9 @@ Set `USE_AI_ENHANCEMENT` to True to activate this application.<br>
* `OIDC_DEFAULT_AFFILIATION`
> default value: ``
>>
* `OIDC_DEFAULT_AFFILIATION`
> valeur par défaut : ``
>> Affiliation par défaut d’un utilisateur authentifié par OIDC.<br>
* `OIDC_NAME`
> default value: ``
>>
Expand Down
4 changes: 4 additions & 0 deletions CONFIGURATION_FR.md
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,10 @@ Mettre `USE_AI_ENHANCEMENT` à True pour activer cette application.<br>
> valeur par défaut : `preferred_username`
>> Noms des Claim permettant de récupérer<br>
>> l’attribut login mais dépendant de l’attribut du client dans l’IDP.<br>
* `OIDC_CLAIM_AFFILIATION`
> valeur par défaut : `affiliations`
>> Noms des Claim permettant de récupérer<br>
>> l’attribut affiliations.<br>
* `OIDC_CLAIM_GIVEN_NAME`
> valeur par défaut : `given_name`
>> Noms des Claim permettant de récupérer les attributs nom, prénom, email<br>
Expand Down
49 changes: 42 additions & 7 deletions pod/authentication/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def update_owner_params(user, params) -> None:
settings, "OIDC_DEFAULT_ACCESS_GROUP_CODE_NAMES", []
)

OIDC_CLAIM_AFFILIATION = getattr(settings, "OIDC_CLAIM_AFFILIATION", "affiliations")

class OIDCBackend(OIDCAuthenticationBackend):
"""OIDC backend authentication."""
Expand All @@ -92,13 +93,26 @@ def create_user(self, claims):
user.last_name = claims.get(OIDC_CLAIM_FAMILY_NAME, "")
user.username = claims.get(OIDC_CLAIM_PREFERRED_USERNAME, "")
user.owner.affiliation = OIDC_DEFAULT_AFFILIATION
for code_name in OIDC_DEFAULT_ACCESS_GROUP_CODE_NAMES:
try:
user.owner.accessgroup_set.add(
AccessGroup.objects.get(code_name=code_name)
)
except ObjectDoesNotExist:
pass
affiliations = claims.get(OIDC_CLAIM_AFFILIATION, "")
if affiliations:
for affiliation in affiliations:
if CREATE_GROUP_FROM_AFFILIATION:
accessgroup, group_created = AccessGroup.objects.get_or_create(code_name=affiliation)
if group_created:
accessgroup.display_name = affiliation
accessgroup.auto_sync = True
accessgroup.sites.add(Site.objects.get_current())
accessgroup.save()
user.owner.accessgroup_set.add(accessgroup)
else:
user.owner.accessgroup_set.add(accessgroup)
else:
user.owner.affiliation = OIDC_DEFAULT_AFFILIATION
for code_name in OIDC_DEFAULT_ACCESS_GROUP_CODE_NAMES:
try:
user.owner.accessgroup_set.add(AccessGroup.objects.get(code_name=code_name))
except ObjectDoesNotExist:
pass
user.is_staff = is_staff_affiliation(affiliation=user.owner.affiliation)
user.owner.save()
user.save()
Expand All @@ -110,6 +124,27 @@ def update_user(self, user, claims):
user.first_name = claims.get(OIDC_CLAIM_GIVEN_NAME, "")
user.last_name = claims.get(OIDC_CLAIM_FAMILY_NAME, "")
user.username = claims.get(OIDC_CLAIM_PREFERRED_USERNAME, "")
affiliations = claims.get(OIDC_CLAIM_AFFILIATION, "")
if affiliations:
for affiliation in affiliations:
if CREATE_GROUP_FROM_AFFILIATION:
accessgroup, group_created = AccessGroup.objects.get_or_create(code_name=affiliation)
if group_created:
accessgroup.display_name = affiliation
accessgroup.auto_sync = True
accessgroup.sites.add(Site.objects.get_current())
accessgroup.save()
user.owner.accessgroup_set.add(accessgroup)
else:
user.owner.accessgroup_set.add(accessgroup)
else:
user.owner.affiliation = OIDC_DEFAULT_AFFILIATION
for code_name in OIDC_DEFAULT_ACCESS_GROUP_CODE_NAMES:
try:
user.owner.accessgroup_set.add(AccessGroup.objects.get(code_name=code_name))
except ObjectDoesNotExist:
pass

user.save()

user.owner.auth_type = "OIDC"
Expand Down
13 changes: 13 additions & 0 deletions pod/main/configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,19 @@
"pod_version_end": "",
"pod_version_init": "3.1"
},
"OIDC_CLAIM_AFFILIATION": {
"default_value": "affiliations",
"description": {
"en": [
""
],
"fr": [
"Noms du Claim permettant de récupérer l'attribut affiliations"
]
},
"pod_version_end": "",
"pod_version_init": "3.1"
},
"OIDC_DEFAULT_ACCESS_GROUP_CODE_NAMES": {
"default_value": "[]",
"description": {
Expand Down