Skip to content

jwt registration and authentication api used for educational purposes

License

Notifications You must be signed in to change notification settings

iliadmitriev/auth-api

Repository files navigation

auth-api

CI unittests codecov CodeFactor Documentation Status

JWT auth service for educational purposes. It's build using aiohttp, psycopg2, aioredis, SQLAlchemy, alembic, marshmallow, PyJWT, pytest

New realization of https://github.com/iliadmitriev/auth started from a scratch

Contents

installation

  1. checkout repository
  2. create and activate virtual environment
python3 -m venv venv
source venv/bin/activate
  1. create .env file with environment variables and export them to shell
cat > .env << _EOF_
SECRET_KEY=testsecretkey
POSTGRES_HOST=192.168.10.1
POSTGRES_PORT=5432
POSTGRES_DB=auth
POSTGRES_USER=auth
POSTGRES_PASSWORD=authsecret
REDIS_LOCATION=redis://192.168.10.1:6379/0
_EOF_

export $(cat .env | xargs)

secret key should be a random string which is kept in secret 4. create db instances (postgres, redis)

docker run -d --name auth-redis --hostname auth-redis \
    -p 6379:6379 redis:6.2.5-alpine3.14

docker run -d --name auth-postgres --hostname auth-postgres \
    -p 5432:5432 --env-file .env postgres:13.4-alpine3.14
  1. install pip modules from project requirements
pip install -r requirements.txt
  1. migrate alembic revisions
alembic upgrade head
  1. run
python3 main.py

How to use

Read api documentation http://localhost:8080/auth/v1/docs

With curl

  1. Register user
curl -v -F password=321123 -F password2=321123 -F [email protected] \
  --url http://localhost:8080/auth/v1/register
  1. Get a token pair (access and refresh)
curl -v -F password=321123 -F [email protected] \
  --url http://localhost:8080/auth/v1/login

access_token - is needed to authenticate your queries (it expires in 5 minutes)

refresh_token - is needed to refresh access token (it expires in 24 hours)

  1. Refresh access token
curl -v --url http://localhost:8080/auth/v1/refresh \
 -F refresh_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjo3LCJlbWFpbCI6InVzZXJAZXhhbXBsZS5jb20iLCJqdGkiOiIwMWVjNjRhOWZlZjc0ZWIwOWViMGI1YmY1NGViOWVjMSIsInRva2VuX3R5cGUiOiJyZWZyZXNoX3Rva2VuIiwiZXhwIjoxNjE1MzA0MDQ2fQ.QyRVKKkxRNcql84ri6HPcL78D348LOPKH_BmKGUdpFo

With HTTPie

install HTTPie, httpie-jwt-auth, jq

  1. set login and password to environment variables
[email protected]
AUTH_PASS=321123
  1. Login and get refresh token (expires in 24h)
REFRESH_TOKEN=$(http :8080/auth/v1/login email=$AUTH_EMAIL password=$AUTH_PASS | jq --raw-output '.refresh_token')
  1. Using refresh token, get an access token(expires in 5 min, repeat step 3 in 5 min)
ACCESS_TOKEN=$(http :8080/auth/v1/refresh refresh_token=$REFRESH_TOKEN | jq --raw-output '.access_token') 
  1. Make request to users api with access token
http -v -A jwt -a $ACCESS_TOKEN :8080/auth/v1/users

Testing

Run tests

pytest -v --cov=.

Run tests with coverage

pytest -v --cov=. --cov-report=term-missing --cov-fail-under=100

Run tests with html report

# run tests and generate report
pytest -v --cov=. --cov-report=term-missing --cov-fail-under=100 --cov-report=html

# open report
open htmlcov/index.html 

Docker

Build

docker build -t auth_api ./

Run

docker run -d -p 8080:8080 --name auth-api \
  --hostname auth-api --env-file .env auth_api

Docker-compose

  1. create .env file with environment variables and export them to shell
cat > .env << _EOF_
SECRET_KEY=testsecretkey
POSTGRES_HOST=auth-postgres
POSTGRES_PORT=5432
POSTGRES_DB=auth
POSTGRES_USER=auth
POSTGRES_PASSWORD=authsecret
REDIS_LOCATION=redis://auth-redis:6379/0
_EOF_
  1. pull, build and run
docker-compose up -d
  1. apply migrations
docker-compose exec api alembic upgrade head

full cleanup

docker-compose down --volumes --remove-orphans --rmi all