JWT auth service built using Django, Simple JWT, DRF
Minimal Python version 3.10
- install python3 and create virtual env
python3 -m venv venv
source venv/bin/activate
- install requirements
pip install -r requirements.txt
- put django secret key into file .env generate DJANGO_SECRET_KEY
echo DJANGO_SECRET_KEY=\'$(python3 -c 'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())')\' >> .env
or just create test secret key (don't do this in production)
echo DJANGO_SECRET_KEY=testsecretkey >> .env
- export variables from .env file
export $(cat .env | xargs)
- create services postgres and memcached
docker run -d --name auth-postgres --hostname auth-postgres \
-p 5432:5432 --env-file .env postgres:14-alpine
docker run -d -p 11211:11211 --name auth-memcached memcached:alpine
- create a db (run migrations)
python3 manage.py migrate
- compile messages
python3 manage.py compilemessages
- create superuser
python3 manage.py createsuperuser
- set environment variables
DJANGO_DEBUG=True
- make migrations and migrate
python3 manage.py makemigrations
python3 manage.py migrate
- make messages
python3 manage.py makemessages -a
python3 manage.py compilemessages
- run
python3 manage.py runserver 0:8000
- run all tests
python3 manage.py test
- run with keeping db in case of test fails
python3 manage.py test --keepdb
- run all tests with details
python3 manage.py test --verbosity=2
- install coverage
pip install coverage
- run with coverage
coverage run --source='.' manage.py test
- print report with missing lines
coverage report -m
- generate detailed html report
coverage html
open htmlcov/index.html
- register new account
curl -v -H 'Accept: application/json; indent=4' \
-H 'Content-Type: application/json' \
-d '
{
"email": "[email protected]",
"password": "yourpassword",
"password2": "yourpassword"
}' \
http://localhost:8000/auth/register/
- get auth token
curl -v -H 'Accept: application/json; indent=4' \
-H 'Content-Type: application/json' \
-d '
{
"username": "[email protected]",
"password": "yourpassword"
}' \
http://localhost:8000/auth/token/
- get restricted data using auth tocken
curl -v -H 'Accept: application/json; indent=4' \
-H 'Authorization: Bearer youraccessotoken' \
http://localhost:8000/auth/user/
- refresh token
curl -v -H 'Accept: application/json; indent=4' \
-H 'Content-Type: application/json' \
-d '
{
"refresh": "your refresh token goes here"
}' \
http://localhost:8000/auth/token/refresh/
Token is invalid or expired
with 403 Forbidden
http code
- migrate migrations
python3 manage.py migrate --noinput
- collect static
python3 manage.py collectstatic --noinput
- compile messages
python3 manage.py compilemessages
- install uWSGI
pip install uWSGI
- Run
uwsgi --ini uwsgi.ini
- create .env file with environment variables
DJANGO_SECRET_KEY=testsecretkey
POSTGRES_HOST=192.168.10.1
POSTGRES_PORT=5432
POSTGRES_DB=auth
POSTGRES_USER=auth
POSTGRES_PASSWORD=authsecret
MEMCACHED_LOCATION=192.168.10.1:11211
- build docker image
docker build -t auth ./
- create postgres instance
docker run -d -p 5432:5432 --name auth-postgres --env-file .env postgres:13.2-alpine
- create memcached instance
docker run -d -p 11211:11211 --name auth-memcached --env-file .env memcached:alpine
- create auth instance
docker run -d -p 8000:8000 --name auth-api --env-file .env auth
- run migrations
docker exec -ti auth-api python3 manage.py migrate
- create super user
docker exec -ti auth-api python3 manage.py createsuperuser
- create .env file with environment variables
DJANGO_SECRET_KEY=testsecretkey
POSTGRES_HOST=192.168.10.1
POSTGRES_PORT=5432
POSTGRES_DB=auth
POSTGRES_USER=auth
POSTGRES_PASSWORD=authsecret
MEMCACHED_LOCATION=192.168.10.1:11211
- run services interactively with logs
docker-compose up
or run services as daemons
docker-compose up -d
- migrate
docker-compose exec api python3 manage.py migrate
- create super user
docker-compose exec api python3 manage.py createsuperuser