Skip to content

Commit b0c42fd

Browse files
fix(authenticator.utils): get ID_KEY from authenticator configuration, not defaults (ansible#672)
- **tests(authentication): fix UID_KEY tests for authentication.determine_username_from_uid_social, introduce failing test** - **fix(authenticator.utils): get ID_KEY from authenticator configuration, not defaults** AAP-37203
1 parent 3bdea59 commit b0c42fd

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

ansible_base/authentication/utils/authentication.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,21 @@ def check_system_username(uid: str) -> None:
9595

9696

9797
def determine_username_from_uid_social(**kwargs) -> dict:
98-
uid_field = getattr(kwargs.get('backend', None), 'ID_KEY', 'username')
99-
if uid_field is None:
100-
uid_field = 'username'
98+
backend = kwargs.get('backend', None)
99+
uid_field = 'username'
100+
if backend:
101+
# Update our fallback if the authenticator has an ID_KEY field, ignore if ID_KEY is None
102+
if getattr(backend, "ID_KEY", None) is not None:
103+
uid_field = backend.ID_KEY
104+
# Favor the authenticator configuration variable ID_KEY if present
105+
if hasattr(backend, "setting"):
106+
uid_field_setting = backend.setting('ID_KEY', default=None)
107+
if uid_field_setting is not None:
108+
uid_field = uid_field_setting
101109
selected_username = kwargs.get('details', {}).get(uid_field, None)
102110
if not selected_username:
103111
raise AuthException(
104-
_('Unable to get associated username from attribute {uid_field}: %(details)s') % {'uid_field': uid_field, 'details': kwargs.get("details", None)}
112+
_('Unable to get associated username from attribute %(uid_field)s: %(details)s') % {'uid_field': uid_field, 'details': kwargs.get("details", None)}
105113
)
106114

107115
authenticator = kwargs.get('backend')

test_app/tests/authentication/utils/test_authentication.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
from test_app.models import User
1212

1313

14+
@pytest.fixture
15+
def oidc_authenticator_plugin_uid_key_overridden(oidc_authenticator, random_name):
16+
oidc_authenticator.configuration['ID_KEY'] = random_name
17+
oidc_authenticator.save()
18+
yield (oidc_authenticator, random_name)
19+
20+
1421
@pytest.mark.django_db
1522
class TestAuthenticationUtilsAuthentication:
1623
logger = 'ansible_base.authentication.utils.authentication.logger'
@@ -200,16 +207,26 @@ def test_determine_username_from_uid_social_happy_path(self, ldap_authenticator)
200207
)
201208
assert response == {'username': 'Bob'}
202209

210+
def test_determine_username_from_uid_social_authenticator_ID_KEY(self, oidc_authenticator_plugin_uid_key_overridden):
211+
backend_authenticator_class, uid_key = oidc_authenticator_plugin_uid_key_overridden
212+
backend = get_authenticator_class(backend_authenticator_class.type)(database_instance=backend_authenticator_class)
213+
kwargs = {
214+
'backend': backend,
215+
}
216+
with pytest.raises(AuthException) as ae:
217+
authentication.determine_username_from_uid_social(**kwargs)
218+
assert f'Unable to get associated username from attribute {uid_key}' in ae.value.backend
219+
203220
@pytest.mark.parametrize(
204221
"kwargs,expected_uid_field",
205222
[
206223
({}, 'username'),
207224
({'backend': None}, 'username'),
208-
({'backend': SimpleNamespace(ID_KEY='testing')}, 'testing'),
209-
({'backend': SimpleNamespace(ID_KEY=None)}, 'testing'),
225+
({'backend': SimpleNamespace(ID_KEY="id_key_test_value")}, 'id_key_test_value'),
226+
({'backend': SimpleNamespace(ID_KEY=None)}, 'username'),
210227
],
211228
)
212-
def test_determine_username_from_uid_social_authenticator_ID_KEY(self, kwargs, expected_uid_field):
229+
def test_determine_username_from_uid_social_authenticator_ID_KEY_fallback(self, kwargs, expected_uid_field):
213230
with pytest.raises(AuthException) as ae:
214231
authentication.determine_username_from_uid_social(**kwargs)
215-
assert f'Unable to get associated username from attribute {expected_uid_field}' in ae
232+
assert f'Unable to get associated username from attribute {expected_uid_field}' in ae.value.backend

test_app/tests/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import random
22
import re
3+
import string
34
from collections import defaultdict
45
from datetime import datetime, timedelta
56
from unittest import mock
@@ -29,6 +30,11 @@
2930
from test_app import models
3031

3132

33+
@pytest.fixture
34+
def random_name(length: int = 10) -> str:
35+
return ''.join(random.choices(string.ascii_lowercase, k=length))
36+
37+
3238
@pytest.fixture()
3339
def openapi_schema():
3440
request = RequestFactory().get('/api/v1/')

0 commit comments

Comments
 (0)