Skip to content

Commit 168c962

Browse files
committed
auth added
1 parent f4a3834 commit 168c962

File tree

6 files changed

+117
-0
lines changed

6 files changed

+117
-0
lines changed

djapy/core/auth/__init__.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
__all__ = [
2+
"BaseAuthMechanism",
3+
"SessionAuth",
4+
"djapy_auth",
5+
"djapy_method"
6+
]
7+
8+
from djapy.core.auth.auth import BaseAuthMechanism, SessionAuth
9+
from djapy.core.auth.dec import djapy_auth, djapy_method
Binary file not shown.
2.96 KB
Binary file not shown.
3.04 KB
Binary file not shown.

djapy/core/auth/auth.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from django.http import HttpRequest
2+
3+
4+
class BaseAuthMechanism:
5+
def __init__(self, *args, **kwargs):
6+
self.message_response = None
7+
8+
def authenticate(self, request: HttpRequest, *args, **kwargs):
9+
pass
10+
11+
def authorize(self, request: HttpRequest, *args, **kwargs):
12+
pass
13+
14+
def schema(self):
15+
return {}
16+
17+
def required_headers(self):
18+
pass
19+
20+
def app_schema(self):
21+
return {}
22+
23+
def set_message_response(self, message_response: dict):
24+
self.message_response = message_response
25+
26+
27+
class SessionAuth(BaseAuthMechanism):
28+
def __init__(self, *args, **kwargs):
29+
self.message_response = {"message": "You are not authenticated"}
30+
super().__init__(*args, **kwargs)
31+
32+
def authenticate(self, request: HttpRequest, *args, **kwargs):
33+
return self.message_response
34+
35+
def required_headers(self):
36+
return [
37+
"Cookie"
38+
]
39+
40+
def schema(self):
41+
return {
42+
"SessionAuth": {
43+
"type": "apiKey",
44+
"in": "cookie",
45+
"name": "sessionid"
46+
},
47+
"CSRFTokenAuth": {
48+
"type": "apiKey",
49+
"in": "cookie",
50+
"name": "csrftoken"
51+
}
52+
}
53+
54+
def app_schema(self):
55+
return {
56+
"SessionAuth": [],
57+
"CSRFTokenAuth": []
58+
}

djapy/core/auth/dec.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import inspect
2+
import json
3+
from functools import wraps
4+
from typing import Dict, Callable, List, Type
5+
6+
from django.http import HttpRequest, JsonResponse
7+
8+
from . import BaseAuthMechanism
9+
from ..defaults import ALLOW_METHODS_LITERAL, DEFAULT_METHOD_NOT_ALLOWED_MESSAGE
10+
11+
12+
def djapy_auth(auth_mechanism_class: Type[BaseAuthMechanism],
13+
permissions: List[str] = None) -> Callable:
14+
def decorator(view_func):
15+
@wraps(view_func)
16+
def _wrapped_view(request: HttpRequest, *args, **kwargs):
17+
return view_func(request, *args, **kwargs)
18+
19+
_wrapped_view.auth_mechanism = auth_mechanism_class()
20+
_wrapped_view.permissions = permissions
21+
return _wrapped_view
22+
23+
if inspect.isfunction(auth_mechanism_class):
24+
return decorator(auth_mechanism_class)
25+
26+
return decorator
27+
28+
29+
def djapy_method(allowed_method_or_list: ALLOW_METHODS_LITERAL | List[ALLOW_METHODS_LITERAL],
30+
message_response: Dict[str, str] = None) -> Callable:
31+
message_response = message_response or DEFAULT_METHOD_NOT_ALLOWED_MESSAGE
32+
try:
33+
json.dumps(message_response)
34+
except TypeError as e:
35+
raise TypeError(f"Invalid message_response: {message_response}") from e
36+
37+
def decorator(view_func):
38+
@wraps(view_func)
39+
def _wrapped_view(request: HttpRequest, *args, **kwargs):
40+
if request.method not in allowed_method_or_list:
41+
view_func.djapy_message_response = message_response
42+
return view_func(request, *args, **kwargs)
43+
44+
if isinstance(allowed_method_or_list, str):
45+
_wrapped_view.djapy_allowed_method = [allowed_method_or_list]
46+
else:
47+
_wrapped_view.djapy_allowed_method = allowed_method_or_list
48+
return _wrapped_view
49+
50+
return decorator

0 commit comments

Comments
 (0)