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
- checkout repository
- create and activate virtual environment
python3 -m venv venv
source venv/bin/activate
- 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
- install pip modules from project requirements
pip install -r requirements.txt
- migrate alembic revisions
alembic upgrade head
- run
python3 main.py
Read api documentation http://localhost:8080/auth/v1/docs
- Register user
curl -v -F password=321123 -F password2=321123 -F email=user@example.com \
--url http://localhost:8080/auth/v1/register
- Get a token pair (access and refresh)
curl -v -F password=321123 -F email=user@example.com \
--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)
- Refresh access token
curl -v --url http://localhost:8080/auth/v1/refresh \
-F refresh_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjo3LCJlbWFpbCI6InVzZXJAZXhhbXBsZS5jb20iLCJqdGkiOiIwMWVjNjRhOWZlZjc0ZWIwOWViMGI1YmY1NGViOWVjMSIsInRva2VuX3R5cGUiOiJyZWZyZXNoX3Rva2VuIiwiZXhwIjoxNjE1MzA0MDQ2fQ.QyRVKKkxRNcql84ri6HPcL78D348LOPKH_BmKGUdpFo
install HTTPie, httpie-jwt-auth, jq
- set login and password to environment variables
AUTH_EMAIL=admin@example.com
AUTH_PASS=321123
- 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')
- 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')
- Make request to users api with access token
http -v -A jwt -a $ACCESS_TOKEN :8080/auth/v1/users
pytest -v --cov=.
pytest -v --cov=. --cov-report=term-missing --cov-fail-under=100
# 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 -t auth_api ./
docker run -d -p 8080:8080 --name auth-api \
--hostname auth-api --env-file .env auth_api
- 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_
- pull, build and run
docker-compose up -d
- apply migrations
docker-compose exec api alembic upgrade head
full cleanup
docker-compose down --volumes --remove-orphans --rmi all