-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat[bckend-proxy-middleware]: Added Nginx as a reverse proxy for API…
… requests and implemented a middleware to filter API requests based on user roles.
- Loading branch information
Showing
8 changed files
with
103 additions
and
23 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,11 @@ | ||
FROM nginx:1.25.4-alpine | ||
RUN apk add --no-cache openssl gettext | ||
RUN rm /etc/nginx/conf.d/default.conf | ||
COPY nginx.conf.dev /etc/nginx/nginx.conf.template | ||
COPY env.sh /docker-entrypoint.sh | ||
RUN chmod +x /docker-entrypoint.sh | ||
CMD ["/docker-entrypoint.sh"] | ||
|
||
|
||
|
||
|
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,4 @@ | ||
#!/bin/sh | ||
|
||
envsubst < /etc/nginx/nginx.conf.template > /etc/nginx/conf.d/default.conf | ||
exec nginx -g 'daemon off;' |
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,21 @@ | ||
events { | ||
} | ||
|
||
http { | ||
sendfile on; | ||
|
||
server { | ||
listen 80; | ||
root /www/data/; | ||
access_log /var/log/nginx/access.log; | ||
|
||
add_header X-Content-Type-Options "nosniff" always; | ||
add_header Referrer-Policy "no-referrer-when-downgrade" always; | ||
add_header Permissions-Policy "interest-cohort=()" always; | ||
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; | ||
|
||
location /api/ { | ||
proxy_pass http://api:8000/api/; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# custom_middleware.py | ||
from rest_framework import status | ||
from django.http import JsonResponse | ||
from rest_framework_simplejwt.tokens import UntypedToken | ||
from db.models import User | ||
from rest_framework_simplejwt.exceptions import InvalidToken, TokenError | ||
|
||
class AdminUserMiddleware: | ||
def __init__(self, get_response): | ||
self.get_response = get_response | ||
def __call__(self, request): | ||
auth_header = request.headers.get('Authorization') | ||
if not auth_header: | ||
return self.get_response(request) | ||
try: | ||
token = UntypedToken(token = auth_header.split()[1]) | ||
except (InvalidToken, TokenError): | ||
return JsonResponse({'error': "Invalid or expired token"}, status=status.HTTP_401_UNAUTHORIZED) | ||
is_superuser = token.payload.get('is_superuser') | ||
is_admin = '/api/admin/' in request.path | ||
is_user = '/api/user/' in request.path | ||
# If the user is trying to access the wrong API, return Forbidden | ||
if is_admin and not is_superuser: | ||
return JsonResponse({'error': "Access is not allowed"}, status=status.HTTP_403_FORBIDDEN) | ||
if is_user and is_superuser: | ||
return JsonResponse({'error': "Access is not allowed"}, status=status.HTTP_403_FORBIDDEN) | ||
return self.get_response(request) |
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