In today's tech landscape, a secure and flexible authentication system is crucial for any application. Whether you're building a web app, mobile app, or any other service, having a robust authentication mechanism is essential. In this blog, I'll walk you through the process of creating an authentication server using Python, FastAPI, and Docker. The server will be general enough to support both custom user login/signup and third-party provider logins like Google and Azure.
Project Overview
The goal of this project is to create an authentication server that is:
Customizable: Easily adaptable to different login/signup requirements.
Scalable: Dockerized and ready to deploy on any environment.
Flexible: Supports both custom credentials and third-party providers.
Tech Stack
Python: The programming language we'll use to develop the authentication server.
FastAPI: A modern, fast (high-performance) web framework for building APIs with Python.
Docker: To containerize the application, making it portable and easy to deploy.
Step-by-Step Implementation
Setting Up FastAPI
Start by setting up a new FastAPI project. Install the necessary dependencies:
`pip install fastapi uvicorn python-dotenv`
Configuring the Environment
Create a .env file and store your environment-specific configurations like database credentials and API keys for third-party providers.
Example .env file:
SECRET_KEY=your_secret_key
DATABASE_URL=your_database_url
GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_client_secret
In config.py, load these configurations using python-dotenv:
from dotenv import load_dotenv
import os
load_dotenv()
SECRET_KEY = os.getenv("SECRET_KEY")
DATABASE_URL = os.getenv("DATABASE_URL")
GOOGLE_CLIENT_ID = os.getenv("GOOGLE_CLIENT_ID")
GOOGLE_CLIENT_SECRET = os.getenv("GOOGLE_CLIENT_SECRET")
Main Application Entry Point In main.py, register all the router in this file.
Database & Models
Creating the Authentication Router
In app/routers/authentication/router.py, define your authentication routes:
This authentication router will be used to create login and signup API along with token refresh API. we will use JWT Bearer type of token.
app/routers/authentication/schema.py, will define all the schema required for this routers.
Utility Functions Implement utility functions in utility.py for tasks like password hashing, JWT token decoding and encoding.
Dockerizing the Application Create a Dockerfile to containerize the application.
Creating docker-compose file : This will help us to run the both container, database and authentication app from a single file. We will also create staging for this whole app in future.
Conclusion
By following this guide, you’ll have a fully functional, customizable, and scalable authentication server. This server will be capable of handling both custom user logins and third-party provider logins. Using FastAPI and Docker ensures that the server is high-performing and easily deployable, fitting well into modern application ecosystems.