Identity Service API

A FastAPI-based API for managing user identities and authentication that supports flexible login with either username or email.

Features

  • User registration with either email or username
  • Flexible authentication (login with either email or username)
  • JWT token-based authentication
  • Password hashing with bcrypt
  • SQLite database with SQLAlchemy ORM
  • Database migrations with Alembic
  • API documentation with Swagger UI and ReDoc

Requirements

  • Python 3.8+
  • FastAPI
  • SQLAlchemy
  • Alembic
  • PyJWT
  • PassLib
  • Uvicorn
  • Email validator

Installation

  1. Clone the repository:
git clone https://github.com/yourusername/identityservice.git
cd identityservice
  1. Install dependencies:
pip install -r requirements.txt
  1. Set up environment variables (optional):

Create a .env file in the root directory with the following variables:

SECRET_KEY=your-secret-key
ACCESS_TOKEN_EXPIRE_MINUTES=30

Database Setup

The application uses SQLite by default. The database will be created at /app/storage/db/db.sqlite.

To apply migrations:

alembic upgrade head

Running the Application

Start the server:

uvicorn main:app --reload

The API will be available at http://localhost:8000

API Documentation

API Endpoints

Authentication

  • POST /api/v1/auth/login - OAuth2 compatible login (accepts username or email in the username field)
  • POST /api/v1/auth/login/flexible - Login with separate email and username fields

Users

  • POST /api/v1/users/ - Create a new user
  • GET /api/v1/users/me - Get current user info
  • PUT /api/v1/users/me - Update current user info

Health Check

  • GET /health - API health check

Environment Variables

Variable Description Default
SECRET_KEY JWT secret key Auto-generated secure token
ACCESS_TOKEN_EXPIRE_MINUTES JWT token expiration time 30 minutes
BACKEND_CORS_ORIGINS CORS allowed origins ["*"]

Project Structure

.
├── alembic.ini
├── app
│   ├── api
│   │   ├── api_v1
│   │   │   ├── api.py
│   │   │   └── endpoints
│   │   │       ├── auth.py
│   │   │       └── users.py
│   │   └── deps.py
│   ├── core
│   │   ├── config.py
│   │   └── security.py
│   ├── crud
│   │   └── user.py
│   ├── db
│   │   ├── base.py
│   │   ├── base_class.py
│   │   └── session.py
│   ├── models
│   │   └── user.py
│   └── schemas
│       ├── auth.py
│       └── user.py
├── main.py
├── migrations
│   ├── env.py
│   ├── README
│   ├── script.py.mako
│   └── versions
│       └── 001_create_user_table.py
└── requirements.txt
Description
Project: Identity Service
Readme 43 KiB
Languages
Python 97.7%
Mako 2.3%