Skip to content

Commit 1bf711f

Browse files
author
David Newswanger
authored
Add authentication backend for renamed user accounts (ansible#611)
1 parent 9365ce4 commit 1bf711f

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

ansible_base/lib/backends/__init__.py

Whitespace-only changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import logging
2+
3+
from django.conf import settings
4+
from django.contrib.auth.backends import ModelBackend
5+
6+
PREFIX = getattr(settings, "RENAMED_USERNAME_PREFIX", None)
7+
logger = logging.getLogger('ansible_base.authentication.authenticator_plugins.github_enterprise_team')
8+
9+
10+
class PrefixedUserAuthenticationMixin:
11+
def authenticate(self, request, **kwargs):
12+
if not PREFIX:
13+
return None
14+
if username := kwargs.get("username", None):
15+
if not username.startswith(PREFIX):
16+
kwargs["username"] = PREFIX + username
17+
return super().authenticate(request, **kwargs)
18+
19+
20+
class PrefixedUserAuthBackend(PrefixedUserAuthenticationMixin, ModelBackend):
21+
pass

test_app/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
# set some vanilla social auth plugins so that we can test the social_auth based
9393
# users in the resource registry
9494
AUTHENTICATION_BACKENDS = [
95+
'ansible_base.lib.backends.prefixed_user_auth.PrefixedUserAuthBackend',
9596
'social_core.backends.github.GithubOAuth2',
9697
]
9798

@@ -197,3 +198,5 @@
197198
}
198199
RESOURCE_SERVICE_PATH = "/api/v1/service-index/"
199200
RESOURCE_SERVER_SYNC_ENABLED = False
201+
202+
RENAMED_USERNAME_PREFIX = "dab:"
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import pytest
2+
from rest_framework.test import APIClient
3+
4+
from ansible_base.lib.utils.response import get_relative_url
5+
from test_app.models import User
6+
7+
8+
@pytest.fixture
9+
@pytest.mark.django_db(transaction=True)
10+
def prefixed_user(local_authenticator):
11+
user = User.objects.create(username='dab:foo')
12+
user.set_password("pass")
13+
user.save()
14+
return user
15+
16+
17+
def test_prefixed_user_can_login_with_original_username(prefixed_user):
18+
url = get_relative_url("rest_framework:login")
19+
me_url = get_relative_url("user-me")
20+
client = APIClient()
21+
22+
data = {"username": "foo", "password": "pass"}
23+
resp = client.post(url, data=data, follow=True)
24+
resp = client.get(me_url)
25+
26+
assert resp.status_code == 200
27+
assert resp.data["username"] == "dab:foo"
28+
29+
30+
def test_prefixed_user_can_login_with_prefixed_username(prefixed_user):
31+
url = get_relative_url("rest_framework:login")
32+
me_url = get_relative_url("user-me")
33+
client = APIClient()
34+
35+
data = {"username": "dab:foo", "password": "pass"}
36+
resp = client.post(url, data=data, follow=True)
37+
resp = client.get(me_url)
38+
39+
assert resp.status_code == 200
40+
assert resp.data["username"] == "dab:foo"

0 commit comments

Comments
 (0)