Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Securing api #123

Merged
merged 2 commits into from
Jul 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion jabme/app/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]

CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOWED_ORIGINS = [
"https://covaccinate.tech",
"http://localhost:3000",
"http://127.0.0.1:3000",
]
ROOT_URLCONF = "app.urls"

TEMPLATES = [
Expand Down
16 changes: 16 additions & 0 deletions jabme/users/decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os

from django.http import HttpResponse

allowed_ips = os.environ.get("ALLOWED_IPS", "localhost 127.0.0.1").split()


def login_by_ip(view_func):
def authorize(request, *args, **kwargs):
user_ip = request.META["REMOTE_ADDR"]
for ip in allowed_ips:
if ip == user_ip:
return view_func(request, *args, **kwargs)
return HttpResponse("Forbidden. F Off.")

return authorize
4 changes: 4 additions & 0 deletions jabme/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
from django.shortcuts import render
from django.template import Context, loader
from django.utils import timezone
from django.utils.decorators import method_decorator
from fake_useragent import UserAgent
from rest_framework.response import Response
from rest_framework.views import APIView
from users.decorators import login_by_ip
from users.models import District

PINCODE_REGEX = "^[1-9][0-9]{5}$"
Expand Down Expand Up @@ -72,6 +74,7 @@ def post(self, request):
return Response("Registered Successfully")


@method_decorator(login_by_ip, name="get")
class FindSlotView(APIView):
def get(self, request):
time_threshold = datetime.now(timezone.utc) - timedelta(
Expand Down Expand Up @@ -203,6 +206,7 @@ def get(self, request):
return Response(result)


@method_decorator(login_by_ip, name="post")
class UpdateEmailSentTime(APIView):
def post(self, request):
now = Now()
Expand Down