Skip to content

Commit

Permalink
Adding logged basic auth to mimic other apps
Browse files Browse the repository at this point in the history
Refactoring settings.py to match gateways order for easier reference
  • Loading branch information
john-westcott-iv committed Jan 18, 2024
1 parent 37ba5f4 commit 1337bb4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
Empty file.
22 changes: 22 additions & 0 deletions test_app/authentication/logged_basic_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import logging

from django.utils.encoding import smart_str
from rest_framework import authentication

logger = logging.getLogger('test_app.authentication.logged_basic_auth')


class LoggedBasicAuthentication(authentication.BasicAuthentication):
def authenticate(self, request):
ret = super(LoggedBasicAuthentication, self).authenticate(request)
if ret:
username = ret[0].username if ret[0] else '<none>'
logger.info(smart_str(f"User {username} performed a {request.method} to {request.path} through the API via basic auth"))
return ret

def authenticate_header(self, request):
return super(LoggedBasicAuthentication, self).authenticate_header(request)


# NOTE: This file is common to many of the services and will allow DRF to return a 401 instead of a 403 on failed login.
# This is the expected behavior we want so we need this file in test_app to mimic other applications
45 changes: 31 additions & 14 deletions test_app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,7 @@

DEBUG = True

# noqa: F405
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": "test_app/tests/sqllite_dbs/db.sqlite3",
"TEST": {
"NAME": "test_app/tests/sqllite_dbs/db_test.sqlite3",
},
}
}
ALLOWED_HOSTS = ["*"]

LOGGING = {
'version': 1,
Expand All @@ -38,8 +29,6 @@
for logger in LOGGING["loggers"]: # noqa: F405
LOGGING["loggers"][logger]["level"] = "ERROR" # noqa: F405

SECRET_KEY = "asdf1234"

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
Expand All @@ -55,6 +44,8 @@
'test_app',
]

AUTH_USER_MODEL = 'auth.User'

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
Expand All @@ -66,6 +57,24 @@
'crum.CurrentRequestUserMiddleware',
]

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': ['test_app.authentication.logged_basic_auth.LoggedBasicAuthentication'],
'DEFAULT_PERMISSION_CLASSES': ['rest_framework.permissions.IsAuthenticated'],
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 50,
}

# noqa: F405
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": "test_app/tests/sqllite_dbs/db.sqlite3",
"TEST": {
"NAME": "test_app/tests/sqllite_dbs/db_test.sqlite3",
},
}
}

ROOT_URLCONF = 'test_app.urls'

TEMPLATES = [
Expand All @@ -83,14 +92,22 @@
},
]

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True

ANSIBLE_BASE_TEAM_MODEL = 'test_app.Team'
ANSIBLE_BASE_ORGANIZATION_MODEL = 'test_app.Organization'

STATIC_URL = '/static/'

AUTH_USER_MODEL = 'auth.User'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

SECRET_KEY = "asdf1234"

ANSIBLE_BASE_AUTHENTICATOR_CLASS_PREFIXES = ['ansible_base.authentication.authenticator_plugins']

Expand Down

0 comments on commit 1337bb4

Please sign in to comment.