Skip to content

iliadmitriev/auth

Repository files navigation

Auth

CI tests codecov

JWT auth service built using Django, Simple JWT, DRF

Minimal Python version 3.10

install

  1. install python3 and create virtual env
python3 -m venv venv
source venv/bin/activate
  1. install requirements
pip install -r requirements.txt
  1. 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
  1. export variables from .env file
export $(cat .env | xargs)
  1. 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
  1. create a db (run migrations)
python3 manage.py migrate
  1. compile messages
python3 manage.py compilemessages
  1. create superuser
python3 manage.py createsuperuser

development


  1. set environment variables
DJANGO_DEBUG=True
  1. make migrations and migrate
python3 manage.py makemigrations
python3 manage.py migrate
  1. make messages
python3 manage.py makemessages -a
python3 manage.py compilemessages
  1. run
python3 manage.py runserver 0:8000

testing

run tests

  1. run all tests
python3 manage.py test
  1. run with keeping db in case of test fails
python3 manage.py test --keepdb
  1. run all tests with details
python3 manage.py test --verbosity=2

⚠️ parallel test running doesn't work under Windows and macOS

run tests with coverage

  1. install coverage
pip install coverage
  1. run with coverage
coverage run --source='.' manage.py test
  1. print report with missing lines
coverage report -m
  1. generate detailed html report
coverage html
open htmlcov/index.html

⚠️ coverage doesn't work when running test parallel under Windows and macOS

how to use

  1. 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/
  1. 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/
  1. get restricted data using auth tocken
curl -v -H 'Accept: application/json; indent=4' \
  -H 'Authorization: Bearer youraccessotoken' \
  http://localhost:8000/auth/user/
  1. 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/

⚠️ by default token is expiring in 5 minutes, after that you will get message Token is invalid or expired with 403 Forbidden http code

production


prepare

  1. migrate migrations
python3 manage.py migrate --noinput
  1. collect static
python3 manage.py collectstatic --noinput
  1. compile messages
python3 manage.py compilemessages

Web server

uWSGI

  1. install uWSGI
pip install uWSGI
  1. Run
uwsgi --ini uwsgi.ini

Docker

Build image

  1. 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
  1. build docker image
docker build -t auth ./
  1. create postgres instance
docker run -d -p 5432:5432 --name auth-postgres --env-file .env postgres:13.2-alpine
  1. create memcached instance
docker run -d -p 11211:11211 --name auth-memcached --env-file .env memcached:alpine
  1. create auth instance
docker run -d -p 8000:8000 --name auth-api --env-file .env auth
  1. run migrations
docker exec -ti auth-api python3 manage.py migrate
  1. create super user
docker exec -ti auth-api python3 manage.py createsuperuser

docker-compose run

  1. 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
  1. run services interactively with logs
docker-compose up

or run services as daemons

docker-compose up -d
  1. migrate
docker-compose exec api python3 manage.py migrate
  1. create super user
docker-compose exec api python3 manage.py createsuperuser