Skip to content

Commit 7780b31

Browse files
committed
feat: refactor auth middleware
1 parent 8d39457 commit 7780b31

File tree

16 files changed

+147
-442
lines changed

16 files changed

+147
-442
lines changed

.isort.cfg

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
11
[settings]
22
known_third_party = django,gql,graphql,jwt,pydantic
3+
4+
recursive=True
5+
line_length=100
6+
# force_to_top=file1.py,file2.py
7+
skip_glob=alembic/*.py
8+
# known_future_library=future,pies
9+
# known_standard_library=std,std2
10+
# known_third_party=randomthirdparty
11+
# known_first_party=mylib1,mylib2
12+
indent=' '
13+
multi_line_output=3
14+
# length_sort=1
15+
# forced_separate=django.contrib,django.utils
16+
default_section=FIRSTPARTY
17+
no_lines_before=LOCALFOLDER
18+
include_trailing_comma=True
19+
combine_as_imports=True

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,29 @@ urlpatterns = [
3434
]
3535
```
3636

37+
```python
38+
# utils.py
39+
def context_builder():
40+
return {'version': 1}
41+
```
42+
3743
```python
3844
# settings.py
45+
MIDDLEWARE = [
46+
'django.middleware.security.SecurityMiddleware',
47+
'django.contrib.sessions.middleware.SessionMiddleware',
48+
'django.middleware.common.CommonMiddleware',
49+
'django.middleware.csrf.CsrfViewMiddleware',
50+
'django.contrib.auth.middleware.AuthenticationMiddleware',
51+
'django.contrib.messages.middleware.MessageMiddleware',
52+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
53+
'djgql.auth.middleware.BasicAuthMiddleware',
54+
]
3955
GRAPHQL_SCHEMA_FILE = os.path.join(BASE_DIR, 'starwar.gql')
4056
GRAPHQL = {
4157
'SCHEMA': 'starwar.schema.schema',
42-
'ENABLE_PLAYGROUND': True
58+
'ENABLE_PLAYGROUND': True,
59+
'CONTEXT_BUILDER': 'starwar.utils.context_builder
4360
}
4461
```
4562

@@ -50,6 +67,7 @@ from enum import Enum
5067
from django.conf import settings
5168
from gql import query, gql, type_resolver, enum_type, field_resolver
5269
from gql.build_schema import build_schema_from_file
70+
from djgql.auth import login_required
5371
from pydantic import BaseModel
5472

5573
type_defs = gql("""
@@ -90,7 +108,10 @@ class Droid(Character):
90108

91109

92110
@query
111+
@login_required
93112
def hero(parent, info, episode: typing.Optional[Episode]) -> typing.Optional[Character]:
113+
request = info.context['request']
114+
print(request.user)
94115
return Human(id='test')
95116

96117

djgql/auth/__init__.py

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,2 @@
1-
def get_authorization_header(request):
2-
"""
3-
Return request's 'Authorization:' header, as a bytestring.
4-
5-
Hide some test client ickyness where the header can be unicode.
6-
"""
7-
auth = request.META.get('HTTP_AUTHORIZATION', b'')
8-
if isinstance(auth, str):
9-
auth = auth.encode()
10-
return auth
11-
12-
13-
class BaseAuthentication:
14-
"""
15-
All authentication classes should extend BaseAuthentication.
16-
"""
17-
18-
def authenticate(self, request):
19-
"""
20-
Authenticate the request and return a two-tuple of (user, token).
21-
"""
22-
raise NotImplementedError(".authenticate() must be overridden.")
23-
24-
def authenticate_header(self, request):
25-
"""
26-
Return a string to be used as the value of the `WWW-Authenticate`
27-
header in a `401 Unauthenticated` response, or `None` if the
28-
authentication scheme should return `403 Permission Denied` responses.
29-
"""
30-
pass
1+
from .middleware import BaseAuthMiddleware, BasicAuthMiddleware # noqa
2+
from .decorator import login_required # noqa

djgql/auth/backends.py

Lines changed: 0 additions & 43 deletions
This file was deleted.

djgql/auth/decorator.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from functools import wraps
2+
3+
from djgql.exceptions import AuthenticationError
4+
5+
6+
def login_required(func):
7+
@wraps(func)
8+
def wrap(parent, info, *args, **kwargs):
9+
request = info.context['request']
10+
if 'authenticated' not in request.auth.scopes:
11+
raise AuthenticationError()
12+
info.context['user'] = request.user
13+
14+
return func(*args, **kwargs)
15+
16+
return wrap

djgql/auth/jwt/__init__.py

Lines changed: 0 additions & 123 deletions
This file was deleted.

djgql/auth/jwt/settings.py

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)