Skip to content

Commit 636407c

Browse files
committed
Update README with JWT / Session authentication
1 parent 75cc776 commit 636407c

File tree

1 file changed

+52
-16
lines changed

1 file changed

+52
-16
lines changed

README.md

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ The structure is inspired by [cookiecutter-django](https://github.com/pydanny/co
77
Few important things:
88

99
* Linux / Ubuntu is our primary OS and things are tested for that. It will mostly not work on Mac & certainly not work on Windows.
10-
* It uses Postgres as primary database.
10+
* It uses Postgres as the primary database.
1111
* It comes with GitHub Actions support, [based on that article](https://hacksoft.io/github-actions-in-action-setting-up-django-and-postgres/)
1212
* It comes with examples for writing tests with fakes & factories, based on the following articles - <https://www.hacksoft.io/blog/improve-your-tests-django-fakes-and-factories>, <https://www.hacksoft.io/blog/improve-your-tests-django-fakes-and-factories-advanced-usage>
1313
* It comes with [`whitenoise`](http://whitenoise.evans.io/en/stable/) setup.
@@ -32,11 +32,46 @@ CORS_ALLOW_ALL_ORIGINS = False
3232
CORS_ORIGIN_WHITELIST = env.list('CORS_ORIGIN_WHITELIST', default=[])
3333
```
3434

35-
### DRF
35+
## Authentication - JWT
3636

37-
We have removed the default authentication classes, since they were causing trouble.
37+
The project is using <https://github.com/Styria-Digital/django-rest-framework-jwt> for having authentication via JWT capabilities.
3838

39-
## Authentication - General
39+
### Settings
40+
41+
All JWT related settings are located in `config/settings/jwt.py`.
42+
43+
> ⚠️ We highly recommend reading the entire settings page from the project documentation - <https://styria-digital.github.io/django-rest-framework-jwt/#additional-settings> - to figure out your needs & the proper defaults for you!
44+
45+
The default settings also include the JWT token as a cookie.
46+
47+
The specific details about how the cookie is set, can be found here - <https://github.com/Styria-Digital/django-rest-framework-jwt/blob/master/src/rest_framework_jwt/compat.py#L43>
48+
49+
### APIs
50+
51+
The JWT related APIs are:
52+
53+
1. `/api/auth/jwt/login/`
54+
1. `/api/auth/jwt/logout/`
55+
56+
The current implementation of the login API returns just the token:
57+
58+
```json
59+
{
60+
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InJhZG9yYWRvQGhhY2tzb2Z0LmlvIiwiaWF0IjoxNjQxMjIxMDMxLCJleHAiOjE2NDE4MjU4MzEsImp0aSI6ImIyNTEyNmY4LTM3ZDctNGI5NS04Y2M0LTkzZjI3MjE4ZGZkOSIsInVzZXJfaWQiOjJ9.TUoQQPSijO2O_3LN-Pny4wpQp-0rl4lpTs_ulkbxzO4"
61+
}
62+
```
63+
64+
This can be changed from `auth_jwt_response_payload_handler`.
65+
66+
67+
### Requiring authentication
68+
69+
We follow this concept:
70+
71+
1. All APIs are public by default (no default authentication classes)
72+
1. If you want a certain API to require authentication, you add the `ApiAuthMixin` to it.
73+
74+
## Authentication - Sessions
4075

4176
This project is using the already existing [**cookie-based session authentication**](https://docs.djangoproject.com/en/3.1/topics/auth/default/#how-to-log-a-user-in) in Django:
4277

@@ -95,22 +130,15 @@ We have the following general cases:
95130
1. If the backend is located on `*.domain.com` and the frontend is located on `*.domain.com`, the configuration is going to work out of the box.
96131
1. If the backend is located on `somedomain.com` and the frontend is located on `anotherdomain.com`, then you'll need to set `SESSION_COOKIE_SAMESITE = 'None'` and `SESSION_COOKIE_SECURE = True`
97132

98-
### Reading list
99-
100-
Since cookies can be somewhat elusive, check the following urls:
101-
102-
1. <https://docs.djangoproject.com/en/3.1/ref/settings/#sessions> - It's a good idea to just read every description for `SESSION_*`
103-
1. <https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies> - It's a good idea to read everything, several times.
104-
105-
## Authentication APIs
133+
### APIs
106134

107-
1. `POST` <http://localhost:8000/api/auth/login/> requires JSON body with `email` and `password`.
108-
1. `GET` <http://localhost:8000/api/auth/me/> returns the current user information, if the request is authenticated (has the corresponding `sessionid` cookie)
109-
1. `GET` or `POST` <http://localhost:8000/api/auth/logout/> will remove the `sessionid` cookie, effectively logging you out.
135+
1. `POST` to `/api/auth/session/login/` requires JSON body with `email` and `password`.
136+
1. `GET` to `/api/auth/me/` returns the current user information, if the request is authenticated (has the corresponding `sessionid` cookie)
137+
1. `GET` or `POST` to `/api/auth/logout/` will remove the `sessionid` cookie, effectively logging you out.
110138

111139
### `HTTP Only` / `SameSite`
112140

113-
The current implementation of `/auth/login` does 2 things:
141+
The current implementation of `/api/auth/session/login` does 2 things:
114142

115143
1. Sets a `HTTP Only` cookie with the session id.
116144
1. Returns the actual session id from the JSON payload.
@@ -119,6 +147,14 @@ The second thing is required, because Safari is not respecting the `SameSite = N
119147

120148
More on the issue here - <https://www.chromium.org/updates/same-site/incompatible-clients>
121149

150+
### Reading list
151+
152+
Since cookies can be somewhat elusive, check the following urls:
153+
154+
1. <https://docs.djangoproject.com/en/3.1/ref/settings/#sessions> - It's a good idea to just read every description for `SESSION_*`
155+
1. <https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies> - It's a good idea to read everything, several times.
156+
157+
122158
## Example List API
123159

124160
You can find the `UserListApi` in [`styleguide_example/users/apis.py`](https://github.com/HackSoftware/Styleguide-Example/blob/master/styleguide_example/users/apis.py#L12)

0 commit comments

Comments
 (0)