-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow customizing username when creating user through OIDC (#971)
* add ability to cutomize claim user for username generation on oidc login * update documentation with new OIDC options * oidc: also normalize custom claim as username * improve tests * improve docs * some more cleanup --------- Co-authored-by: Sascha Ißbrücker <[email protected]>
- Loading branch information
1 parent
fc48b26
commit 2973812
Showing
4 changed files
with
93 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ | |
from django.test import TestCase, override_settings | ||
from django.urls import URLResolver | ||
|
||
from bookmarks import utils | ||
|
||
|
||
class OidcSupportTest(TestCase): | ||
def test_should_not_add_oidc_urls_by_default(self): | ||
|
@@ -55,9 +57,83 @@ def test_default_settings(self): | |
base_settings = importlib.import_module("siteroot.settings.base") | ||
importlib.reload(base_settings) | ||
|
||
self.assertEqual( | ||
True, | ||
base_settings.OIDC_VERIFY_SSL, | ||
) | ||
self.assertEqual(True, base_settings.OIDC_VERIFY_SSL) | ||
self.assertEqual("openid email profile", base_settings.OIDC_RP_SCOPES) | ||
self.assertEqual("email", base_settings.OIDC_USERNAME_CLAIM) | ||
|
||
del os.environ["LD_ENABLE_OIDC"] # Remove the temporary environment variable | ||
|
||
@override_settings(LD_ENABLE_OIDC=True, OIDC_USERNAME_CLAIM="email") | ||
def test_username_should_use_email_by_default(self): | ||
claims = { | ||
"email": "[email protected]", | ||
"name": "test name", | ||
"given_name": "test given name", | ||
"preferred_username": "test preferred username", | ||
"nickname": "test nickname", | ||
"groups": [], | ||
} | ||
|
||
username = utils.generate_username(claims["email"], claims) | ||
|
||
self.assertEqual(claims["email"], username) | ||
|
||
@override_settings(LD_ENABLE_OIDC=True, OIDC_USERNAME_CLAIM="preferred_username") | ||
def test_username_should_use_custom_claim(self): | ||
claims = { | ||
"email": "[email protected]", | ||
"name": "test name", | ||
"given_name": "test given name", | ||
"preferred_username": "test preferred username", | ||
"nickname": "test nickname", | ||
"groups": [], | ||
} | ||
|
||
username = utils.generate_username(claims["email"], claims) | ||
|
||
self.assertEqual(claims["preferred_username"], username) | ||
|
||
@override_settings(LD_ENABLE_OIDC=True, OIDC_USERNAME_CLAIM="nonexistant_claim") | ||
def test_username_should_fallback_to_email_for_non_existing_claim(self): | ||
claims = { | ||
"email": "[email protected]", | ||
"name": "test name", | ||
"given_name": "test given name", | ||
"preferred_username": "test preferred username", | ||
"nickname": "test nickname", | ||
"groups": [], | ||
} | ||
|
||
username = utils.generate_username(claims["email"], claims) | ||
|
||
self.assertEqual(claims["email"], username) | ||
|
||
@override_settings(LD_ENABLE_OIDC=True, OIDC_USERNAME_CLAIM="preferred_username") | ||
def test_username_should_fallback_to_email_for_empty_claim(self): | ||
claims = { | ||
"email": "[email protected]", | ||
"name": "test name", | ||
"given_name": "test given name", | ||
"preferred_username": "", | ||
"nickname": "test nickname", | ||
"groups": [], | ||
} | ||
|
||
username = utils.generate_username(claims["email"], claims) | ||
|
||
self.assertEqual(claims["email"], username) | ||
|
||
@override_settings(LD_ENABLE_OIDC=True, OIDC_USERNAME_CLAIM="preferred_username") | ||
def test_username_should_be_normalized(self): | ||
claims = { | ||
"email": "[email protected]", | ||
"name": "test name", | ||
"given_name": "test given name", | ||
"preferred_username": "NormalizedUser", | ||
"nickname": "test nickname", | ||
"groups": [], | ||
} | ||
|
||
username = utils.generate_username(claims["email"], claims) | ||
|
||
del os.environ["LD_ENABLE_OIDC"] | ||
self.assertEqual("NormalizedUser", username) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters