-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
281 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from functools import wraps | ||
from django.http import HttpResponseForbidden | ||
from myapp.models import * | ||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
def require_authorization_header_with_api_token(view_func): | ||
@wraps(view_func) | ||
def _wrapped_view(request, *args, **kwargs): | ||
# Get the latest AppSettings object from the database | ||
app_settings = AppSettings.objects.last() | ||
if not app_settings: | ||
logger.error('No API key configured. Returning 403 Forbidden.') | ||
return HttpResponseForbidden("Unauthorized. Invalid or missing authorization token.") | ||
|
||
# Retrieve the API token from AppSettings | ||
api_token = app_settings.api_token | ||
|
||
# Get the Authorization header from the request | ||
authorization_header = request.META.get('HTTP_AUTHORIZATION') | ||
|
||
# Check if the Authorization header is present and matches the API token | ||
if authorization_header != f'Bearer {api_token}': | ||
logger.info('Request with invalid or missing API token. Returning 403 Forbidden.') | ||
return HttpResponseForbidden("Unauthorized. Invalid or missing authorization token.") | ||
|
||
# Token is valid; proceed with the request | ||
return view_func(request, *args, **kwargs) | ||
|
||
return _wrapped_view |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Generated by Django 5.1.3 on 2024-11-28 10:34 | ||
|
||
import uuid | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('myapp', '0011_alter_item_expiry_date_alter_item_issue_date_and_more'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='AppSettings', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('api_token', models.CharField(default=uuid.uuid4, max_length=64, unique=True)), | ||
('updated_at', models.DateTimeField(auto_now=True)), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Generated by Django 5.1.3 on 2024-11-28 11:49 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('myapp', '0012_appsettings'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterModelOptions( | ||
name='appsettings', | ||
options={'verbose_name': 'API Settings', 'verbose_name_plural': 'API Settings'}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters