Skip to content

Commit 333e597

Browse files
[AAP-48680] Alter AzureAD to allow for flexable username creation (#752)
## Description <!-- Mandatory: Provide a clear, concise description of the changes and their purpose --> - What is being changed? This adds a setting to the Azure AD authentication to allow for a selected fields to be the UID we recieve. - Why is this change needed? A customer is trying to migrate users from LDAP to Azure AD but the usernames do not match. - How does this change address the issue? This allows for custom usernames coming from AD so the logic which migrates users in 2.5 should now be able to be leveraged if they alter the username coming back from AD. ## Type of Change <!-- Mandatory: Check one or more boxes that apply --> - [ ] Bug fix (non-breaking change which fixes an issue) - [X] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] Documentation update - [ ] Test update - [ ] Refactoring (no functional changes) - [ ] Development environment change - [ ] Configuration change ## Self-Review Checklist <!-- These items help ensure quality - they complement our automated CI checks --> - [X] I have performed a self-review of my code - [X] I have added relevant comments to complex code sections - [X] I have updated documentation where needed - [X] I have considered the security impact of these changes - [X] I have considered performance implications - [X] I have thought about error handling and edge cases - [X] I have tested the changes in my local environment ## Testing Instructions <!-- Optional for test-only changes. Mandatory for all other changes --> <!-- Must be detailed enough for reviewers to reproduce --> ### Prerequisites <!-- List any specific setup required --> ### Steps to Test 1. Create an Azure AD authenticator in an existing 2.5 and authenticate as a user, note the user ID. 2. Apply this patch and restart your gateway service. 3. Log in through Azure AD again and ensure authentication works and you are the same user as before. 4. Log out, log in as admin and delete your AD user. 5. Log in through AD again and ensure you got the same username. 6. Log out, log in as admin and delete your AD user. 7. Edit your AD authenticator and save (with no changes) (this will inject the new setting into your authenticator). 8. Log in as your AD user, ensure you got the same username as before. 9. Log out, log in as admin and delete your AD user. Also alter your authenticator and set the new setting (Field to use as username) to `email`. 10. Log out and log in with your AD account. Ensure your new user id is the email of the user instead of the old username. ### Expected Results <!-- Describe what should happen after following the steps --> ## Additional Context <!-- Optional but helpful information --> ### Required Actions <!-- Check if changes require work in other areas --> <!-- Remove section if no external actions needed --> - [ ] Requires documentation updates <!-- API docs, feature docs, deployment guides --> - [ ] Requires downstream repository changes <!-- Specify repos: django-ansible-base, eda-server, etc. --> - [ ] Requires infrastructure/deployment changes <!-- CI/CD, installer updates, new services --> - [ ] Requires coordination with other teams <!-- UI team, platform services, infrastructure --> - [ ] Blocked by PR/MR: #XXX <!-- Reference blocking PRs/MRs with brief context --> ### Screenshots/Logs <!-- Add if relevant to demonstrate the changes -->
1 parent 690133c commit 333e597

File tree

2 files changed

+47
-0
lines changed
  • ansible_base/authentication/authenticator_plugins
  • test_app/tests/authentication/authenticator_plugins

2 files changed

+47
-0
lines changed

ansible_base/authentication/authenticator_plugins/azuread.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ class AzureADConfiguration(BaseAuthenticatorConfiguration):
4545
ui_field_label=_("Groups Claim"),
4646
)
4747

48+
USERNAME_FIELD = CharField(
49+
help_text=_("The name of the field from the assertion to use as the username. If not set will default to name"),
50+
required=False,
51+
allow_null=True,
52+
default=None,
53+
ui_field_label=_("Field to use as username"),
54+
)
55+
4856

4957
class AuthenticatorPlugin(SocialAuthMixin, SocialAuthValidateCallbackMixin, AzureADOAuth2, AbstractAuthenticatorPlugin):
5058
configuration_class = AzureADConfiguration
@@ -59,3 +67,14 @@ def groups_claim(self):
5967

6068
def get_user_groups(self, extra_groups=[]):
6169
return extra_groups
70+
71+
def get_user_details(self, response):
72+
"""
73+
Return user details from Azure AD account
74+
75+
This method is an override from social-core/social_core/backends/azuread.py
76+
It allows us to control what the username is.
77+
"""
78+
return_object = super().get_user_details(response)
79+
return_object['username'] = response.get(self.setting("USERNAME_FIELD"), return_object['username'])
80+
return return_object

test_app/tests/authentication/authenticator_plugins/test_azuread.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,31 @@ def __init__(self):
109109
# assert that AD returns expected user groups
110110
assert ad.get_user_groups() == []
111111
assert ad.get_user_groups(["a", "b"]) == ["a", "b"]
112+
113+
114+
def test_get_user_details():
115+
116+
class MockedDb:
117+
def __init__(self, username_field):
118+
self.slug = "fake"
119+
self.configuration = {"USERNAME_FIELD": username_field}
120+
121+
ad = get_authenticator_plugin("ansible_base.authentication.authenticator_plugins.azuread")
122+
ad.database_instance = MockedDb(None)
123+
124+
username = 'bob'
125+
126+
127+
response = {
128+
"name": username,
129+
"given_name": "Joe",
130+
"family_name": "LastName",
131+
"email": email,
132+
"upn": "upn123",
133+
}
134+
135+
assert ad.get_user_details(response)['username'] == username
136+
137+
ad.database_instance = MockedDb('email')
138+
139+
assert ad.get_user_details(response)['username'] == email

0 commit comments

Comments
 (0)