Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion ansible_base/authentication/utils/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ def determine_username_from_uid_social(**kwargs) -> dict:
if authenticator.setting('USERNAME_IS_FULL_EMAIL', False):
selected_username = kwargs.get('details', {}).get('email', None)
else:
selected_username = kwargs.get('details', {}).get('username', None)
if authenticator.type == 'SAML':
selected_username = kwargs.get('uid', None)
else:
selected_username = kwargs.get('details', {}).get('username', None)
if not selected_username:
raise_auth_exception(
_('Unable to get associated username from details, expected entry "username". Full user details: %(details)s')
Expand Down
21 changes: 21 additions & 0 deletions test_app/tests/authentication/utils/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,27 @@ def test_determine_username_from_uid_social_happy_path(self, ldap_authenticator)
)
assert response == {'username': 'Bob'}

@pytest.mark.parametrize(
"auth_fixture, expected_username, uid_param, details_username",
[
# SAML authenticator uses uid parameter (line 141)
("saml_authenticator", "SamlUser", "SamlUser", "should_not_be_used"),
# Non-SAML authenticators use details['username'] (line 143)
("ldap_authenticator", "LdapUser", "should_not_be_used", "LdapUser"),
("oidc_authenticator", "OidcUser", "should_not_be_used", "OidcUser"),
("keycloak_authenticator", "KeycloakUser", "should_not_be_used", "KeycloakUser"),
],
)
def test_determine_username_from_uid_social_authenticator_types(self, request, auth_fixture, expected_username, uid_param, details_username):
"""Test username selection logic for different authenticator types (lines 140-143)"""
authenticator = request.getfixturevalue(auth_fixture)
response = authentication.determine_username_from_uid_social(
details={'username': details_username},
backend=get_authenticator_class(authenticator.type)(database_instance=authenticator),
uid=uid_param,
)
assert response == {'username': expected_username}

def test_raise_auth_exception(self):
try:
authentication.raise_auth_exception('testing')
Expand Down