A Full-Fledged Authentication System with Production-Level Concepts
- Multiple Authentication Methods: Supports email-password, username-password, and phone number-password combinations.
- Secure Password Handling: Implements password hashing using industry-standard algorithms.
- Fast and Scalable: Built with FastAPI for high performance and scalability.
- MongoDB Integration: Stores user credentials and data in a reliable NoSQL database.
- Redis Caching: Enhances performance with caching using Redis.
- Kafka Message Queue: Utilizes Kafka for efficient message queuing.
- Bloom Filters: Employs bloom filters for fast lookup, as used by tech giants like Google, Amazon, and Facebook.
- JWT Authentication: Implements JWT authentication for secure and efficient user authentication.
- Access Tokens: Utilizes access tokens for secure authentication.
- OTP Service: Offers OTP service via mail and SMS, with support for AWS SNS and AWS SMS.
- Refresh Token: Implements refresh token logic for password-less login.
- Google OAuth2: Supports Google OAuth2 for user signup and login through their Google account.
- Backend Framework: FastAPI
- Database: MongoDB
- Caching: Redis
- Message Queue: Kafka
- Password Hashing: Industry-standard algorithms
- Programming Language: Python
- OTP Service: Supports mail and SMS, with AWS SNS and AWS SMS integration
- Google OAuth2: Supports user signup and login through Google account
You can get the project up and running using either Docker Compose (the easiest method) or by setting it up manually for more control.
π Important:
- For Docker setup, set
DEVELOPMENT_ENV = "docker"in your.envfile.- For local development, either set
DEVELOPMENT_ENV = "local"or comment out the line entirely.This ensures the application loads the correct configuration and prevents environment-related issues.
This is the simplest method and handles all service dependencies automatically. It will build the necessary images and start all services in one go.
-
Clone the Repository
git clone https://github.com/Madhur-Prakash/Auth.git cd Auth -
Set up environment variables:
# Copy the .env.sample file to .env and fill in the required values. -
Start Services Use Docker Compose to launch the entire stack in detached mode (
-d).docker-compose up -d --build
-
Access Services Once running, you can access the different components at these endpoints:
Service URL Purpose FastAPI App http://localhost:8005/docsThe main FastAPI application. Logging Service http://localhost:8000/docsCentralized request/response logs. Redis Stack UI http://localhost:8001In-memory cache and message broker UI. Mailhog http://localhost:8025Catches outgoing emails for testing. Kafka UI (Kafdrop) http://localhost:9000Web UI for managing Kafka topics. MongoDB (Admin) http://localhost:8081Database administration interface.
-
Clone the Repository
git clone https://github.com/Madhur-Prakash/Auth.git cd Auth -
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Set up MongoDB:
# Install MongoDB and start the service. -
Set up Redis:
# Run this command to start Redis Stack in detached mode: docker run -d --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest # access Redis Stack at π http://localhost:8001
-
Set up Kafka and Zookeeper:
docker run -d \ --name kafka \ -p 2181:2181 \ -p 9092:9092 \ -e KAFKA_LISTENERS="INTERNAL://:29092,EXTERNAL://:9092" \ -e KAFKA_ADVERTISED_LISTENERS="INTERNAL://kafka:29092,EXTERNAL://localhost:9092" \ -e KAFKA_LISTENER_SECURITY_PROTOCOL_MAP="INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT" \ -e KAFKA_INTER_BROKER_LISTENER_NAME="INTERNAL" \ -e KAFKA_ZOOKEEPER_SESSION_TIMEOUT="6000" \ -e KAFKA_RESTART_ATTEMPTS="10" \ -e KAFKA_RESTART_DELAY="5" \ -e ZOOKEEPER_AUTOPURGE_PURGE_INTERVAL="0" \ obsidiandynamics/kafka
docker run -d \ --name kafdrop \ -p 9000:9000 \ --link kafka:kafka \ -e KAFKA_BROKERCONNECT="kafka:29092" \ obsidiandynamics/kafdrop # access Kafka at π http://localhost:9000 # --link kafka:kafka ensures Kafdrop can see the Kafka container by hostname kafka
-
Set up Mailhog:
# Run this command to start Mailhog in detached mode: docker run -d --name mailhog -p 1025:1025 -p 8025:8025 mailhog/mailhog # access Mailhog at π http://localhost:8025
-
Set up external logging service:
- Clone the repository:
git clone https://github.com/Madhur-Prakash/centralized-logging.git cd centralized-logging - Create docker image:
docker build -t logging . - Run docker:
docker run -d --name logging -p 8000:8000 logging # access the logging service at π `http://localhost:8000/docs`
- Clone the repository:
-
Set up environment variables:
# Copy the .env.sample file to .env and fill in the required values.
- Start the FastAPI server:
uvicorn app:app --port 8005 --reload
- Start the kafka worker:
python authentication/config/kafka1_config.py
- Access the API documentation at:
http://127.0.0.1:8005/docs # for detailed docs visit π http://127.0.0.1:8005/scalar
What I Learned by Building a Full Auth System from Scratch - Medium
Auth/
βββ .dockerignore
βββ .env.sample
βββ .gitignore # gitignore file for GitHub
βββ CHANGELOG.md
βββ Dockerfile.auth
βββ Dockerfile.kafka1
βββ Dockerfile.kafka2
βββ LICENSE
βββ README.md # Project documentation
βββ __init__.py # initializes package
βββ app.py # main FastAPI app
βββ authentication
β βββ __init__.py # initializes package
β βββ config
β β βββ __init__.py # initializes package
β β βββ bloom_filter.py
β β βββ celery_app.py
β β βββ database.py # database configuration
β β βββ kafka1_config.py
β β βββ kafka2_config.py
β β βββ rate_limiting.py
β β βββ redis_config.py
β βββ fake_user.py
β βββ helper
β β βββ __init__.py # initializes package
β β βββ auth_token.py
β β βββ hashing.py
β β βββ oauth2.py
β β βββ utils.py # utility functions
β βββ models
β β βββ __init__.py # initializes package
β β βββ models.py # models
β βββ otp_service
β β βββ __init__.py # initializes package
β β βββ otp_verify.py
β β βββ send_mail.py
β βββ src
β β βββ __init__.py # initializes package
β β βββ auth_user.py
β β βββ google_auth.py
β βββ templates
β βββ create_new_password.html
β βββ google_login.html
β βββ index.html
β βββ login.html
β βββ otp.html
β βββ phone_number.html
β βββ reset_password.html
β βββ signup.html
β βββ success.html
β βββ user.html
β βββ user_login.html
βββ credentials.json
βββ docker-compose.yml
βββ requirements.txt
βββ test_api
β βββ __init__.py # initializes package
β βββ locust.py
β βββ test_login.py
β βββ user_api_hit.py
βββ token.pickle
βββ waitforkafka.sh
- Implement OAuth2 for social login (e.g., Github, Facebook).
- Enhance rate-limiting for login attempts to prevent brute-force attacks.
Contributions are welcome! To contribute:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Commit your changes and submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
Madhur Prakash
GitHub | Medium