2025-06-05 10:07:49 +00:00

136 lines
3.0 KiB
Markdown

# 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:
```bash
git clone https://github.com/yourusername/identityservice.git
cd identityservice
```
2. Install dependencies:
```bash
pip install -r requirements.txt
```
3. 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:
```bash
alembic upgrade head
```
## Running the Application
Start the server:
```bash
uvicorn main:app --reload
```
The API will be available at http://localhost:8000
## API Documentation
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- OpenAPI JSON: http://localhost:8000/openapi.json
## 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
```