Skip to content

Commit 4470aea

Browse files
Initial commit
0 parents  commit 4470aea

File tree

20 files changed

+547
-0
lines changed

20 files changed

+547
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 SimpleJWT
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Template Repository for DRF SimpleJWT Apps
2+
3+
Initially created: 3 July 2020
4+
5+
TL;DR: Django server repository setup for SimpleJWT. Test user: `test` and pw `test`.
6+
7+
---
8+
### Example repositories
9+
10+
- Android: [Andrew-Chen-Wang/mobile-auth-example](https://github.com/Andrew-Chen-Wang/mobile-auth-example)
11+
- iOS: [Andrew-Chen-Wang/mobile-auth-example](https://github.com/Andrew-Chen-Wang/mobile-auth-example)
12+
13+
---
14+
### Introduction
15+
16+
This template repository is dedicated to generating
17+
a Django + DRF server with SimpleJWT already setup.
18+
The purpose of this is to easily create repositories
19+
that demonstrate clear usage of SimpleJWT.
20+
21+
If you're not using a frontend framework like React
22+
or some kind of mobile device not using a web browser,
23+
then please use session authentication. I.e. if you're
24+
using plain HTML with Jinja 2 template tags, use the
25+
built-in session authentication middlewear as that
26+
is proven to be the safest and thus far never broken
27+
method of secure authentication.
28+
29+
Note: this template repository is adopted from
30+
[Andrew-Chen-Wang/mobile-auth-example](https://github.com/Andrew-Chen-Wang/mobile-auth-example)
31+
for Android and iOS usage. The license is Apache 2.0
32+
for that example repository.
33+
34+
---
35+
### Usage
36+
37+
1. To generate a repository using this template,
38+
press "Use this template" (highlighted in green).
39+
Note, this will NOT create a fork of the repository.
40+
2. Create your git repository, connect via the ssh remote, and pull.
41+
3. `cd server` to get your terminal/cmd into the server directory.
42+
4. To run the server, create a virtual environment `virtualenv venv && source venv/bin/activate`, install packages `pip install -r requirements.txt` -- the requirements.txt file is inside the server subdirectory -- and do `python manage.py migrate && python manage.py runserver`.
43+
- Again, make sure when you do this, you are inside the server directory on your terminal/cmd.
44+
- On Windows, you should do `venv\Scripts\activate` instead of `source venv/bin/activate`
45+
5. If you're writing for an example repository, please create
46+
a new directory labeled with the name of the framework (e.g. jwt-ios),
47+
and add its `.gitignore`. Please use the
48+
[github/gitignore](https://github.com/github/gitignore) repository.
49+
Provide detailed instructions if necessary.
50+
51+
A default user with the username `test` and password `test` have been created.
52+
53+
This repository does not come with throttling, but **it is
54+
highly recommended that you add throttling to your entire
55+
project.** You can use a third-party package called
56+
Django-ratelimit or DRF's internal throttling mechanism.
57+
Django-ratelimit is more extensive -- covering Django views,
58+
as well -- and thus more supported by SimpleJWT.
59+
60+
---
61+
### License
62+
63+
This repository is licensed under the
64+
[MIT License](https://github.com/SimpleJWT/drf-SimpleJWT-server-template/blob/master/LICENSE).

server/.gitignore

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
pip-wheel-metadata/
24+
share/python-wheels/
25+
*.egg-info/
26+
.installed.cfg
27+
*.egg
28+
MANIFEST
29+
30+
# PyInstaller
31+
# Usually these files are written by a python script from a template
32+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
33+
*.manifest
34+
*.spec
35+
36+
# Installer logs
37+
pip-log.txt
38+
pip-delete-this-directory.txt
39+
40+
# Unit test / coverage reports
41+
htmlcov/
42+
.tox/
43+
.nox/
44+
.coverage
45+
.coverage.*
46+
.cache
47+
nosetests.xml
48+
coverage.xml
49+
*.cover
50+
.hypothesis/
51+
.pytest_cache/
52+
53+
# Translations
54+
*.mo
55+
*.pot
56+
57+
# Django stuff:
58+
*.log
59+
local_settings.py
60+
db.sqlite3
61+
db.sqlite3-journal
62+
63+
# Flask stuff:
64+
instance/
65+
.webassets-cache
66+
67+
# Scrapy stuff:
68+
.scrapy
69+
70+
# Sphinx documentation
71+
docs/_build/
72+
73+
# PyBuilder
74+
target/
75+
76+
# Jupyter Notebook
77+
.ipynb_checkpoints
78+
79+
# IPython
80+
profile_default/
81+
ipython_config.py
82+
83+
# pyenv
84+
.python-version
85+
86+
# pipenv
87+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
88+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
89+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
90+
# install all needed dependencies.
91+
#Pipfile.lock
92+
93+
# celery beat schedule file
94+
celerybeat-schedule
95+
96+
# SageMath parsed files
97+
*.sage.py
98+
99+
# Environments
100+
.env
101+
.venv
102+
env/
103+
venv/
104+
ENV/
105+
env.bak/
106+
venv.bak/
107+
108+
# Spyder project settings
109+
.spyderproject
110+
.spyproject
111+
112+
# Rope project settings
113+
.ropeproject
114+
115+
# mkdocs documentation
116+
/site
117+
118+
# mypy
119+
.mypy_cache/
120+
.dmypy.json
121+
dmypy.json
122+
123+
# Pyre type checker
124+
.pyre/

server/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Django Server
2+
3+
The backend works by using Django, Django Rest Framework, and DRF SimpleJWT.
4+
5+
For this demonstration, SimpleJWT utilizes the refresh and access token methodology. The client sends its credentials to the server once and receives an access and refresh token. Everytime you want to do authentication on a view, the client will send the access token; however, that access token expires (in our case, in 5 minutes for security reasons). Once it expires, instead of resending the credentials, we use the refresh token to get a new access token.
6+
7+
If the refresh token expires (after 1 day for security reasons), the client needs to send the username and password again.
8+
9+
### Running the server
10+
11+
1. Create a virtual environment and install the packages: `virtualenv venv && source venv/bin/activate && pip install -r requirements.txt`.
12+
- Again, make sure when you do this, you are inside the server directory on your terminal/cmd.
13+
- On Windows, you should do `venv\Scripts\activate` instead of `source venv/bin/activate`
14+
2. Run the server: `python manage.py migrate && python manage.py runserver`
15+
16+
A default user with the username `test` and password `test` have been created.
17+
18+
### Other suggestions
19+
20+
I also suggest you use a rate limiter, either provided by Django Rest Framework or a more sophisticated one like django-ratelimit so that you can rate limit across your entire application, not just your REST API.

server/manage.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python
2+
"""Django's command-line utility for administrative tasks."""
3+
import os
4+
import sys
5+
6+
7+
def main():
8+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'server.settings')
9+
try:
10+
from django.core.management import execute_from_command_line
11+
except ImportError as exc:
12+
raise ImportError(
13+
"Couldn't import Django. Are you sure it's installed and "
14+
"available on your PYTHONPATH environment variable? Did you "
15+
"forget to activate a virtual environment?"
16+
) from exc
17+
execute_from_command_line(sys.argv)
18+
19+
20+
if __name__ == '__main__':
21+
main()

server/public/__init__.py

Whitespace-only changes.

server/public/admin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.

server/public/apps.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class PublicConfig(AppConfig):
5+
name = 'public'
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 3.0.3 on 2020-03-02 21:13
2+
3+
from django.db import migrations
4+
from django.contrib.auth.models import User
5+
6+
7+
def create_user(apps, schema_editor):
8+
User.objects.create_superuser("test", password="test")
9+
10+
11+
class Migration(migrations.Migration):
12+
13+
dependencies = [
14+
]
15+
16+
operations = [
17+
migrations.RunPython(create_user)
18+
]

server/public/migrations/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)