A FastAPI-based cloud backend prototype for a smart lock system.
This is a simplified prototype designed for rapid development and testing:
- In-Memory Data Storage: No database setup required - all data stored in memory
- Simplified Authentication: Any password works for existing users (demo purposes)
- Sample Data: Pre-loaded with sample users for immediate testing
- API: Complete REST API
The application follows a clean architecture pattern with clear separation of concerns:
app/
├── models/ # Data models and schemas (Pydantic)
├── services/ # Business logic layer (in-memory storage)
├── api/ # API endpoints and routing
└── core/ # Configuration
- PermissionManager: Manages user permissions and card data generation
- Database: In-memory data storage (replaces SQLite for prototype)
- WebInterface: API endpoints for web interface interactions
- GatewayCommService: Manages communication with smart lock gateways
- Permission: Represents room access permissions with time slots
- AccessLog: Records access attempts and results
-
Install dependencies:
uv sync
-
Run the application:
uv run python main.py
-
Access the API documentation:
- Swagger UI: http://localhost:8001/docs
- ReDoc: http://localhost:8001/redoc
-
Run the demo:
uv run python demo.py
- Username:
admin
, Password:any_password
- Username:
alice
, Password:any_password
- Username:
bob
, Password:any_password
Run python demo.py
to see a complete demonstration of:
- User authentication
- Permission creation and management
- Access logging
- Card data generation
- Login:
POST /auth/login
with any sample username - Create Permission:
POST /permissions/
- View Permissions:
GET /permissions/user/{user_id}
- Log Access:
POST /access-logs/
- Generate Card:
POST /permissions/generate-card/{user_id}
POST /auth/login
- User loginPOST /auth/logout
- User logoutPOST /auth/refresh
- Refresh authentication tokenGET /auth/me
- Get current user session
POST /users/
- Create new userGET /users/{user_id}
- Get user by IDPUT /users/{user_id}
- Update user information
POST /permissions/
- Create permissionGET /permissions/user/{user_id}
- Get user permissionsPUT /permissions/{user_id}/{room_id}
- Update permissionDELETE /permissions/{user_id}/{room_id}
- Revoke permissionPOST /permissions/generate-card/{user_id}
- Generate card data
POST /access-logs/
- Create access log entryGET /access-logs/
- Get access logs with filtersGET /access-logs/user/{user_id}
- Get user access logsGET /access-logs/room/{room_id}
- Get room access logs
POST /gateways/
- Register gatewayGET /gateways/
- Get all gatewaysGET /gateways/{gateway_id}
- Get specific gatewayDELETE /gateways/{gateway_id}
- Unregister gatewayPOST /gateways/{gateway_id}/sync
- Sync with gatewayPOST /gateways/{gateway_id}/card-update
- Send card update
POST /reports/
- Generate reportGET /reports/types
- Get available report types
Represents access permissions with time-based restrictions:
{
"user_id": "string",
"room_id": "string",
"time_slots": [
{
"start_time": "09:00:00",
"end_time": "17:00:00",
"day_of_week": "mon", # Monday-Friday
"is_active": True,
}
]
}
Records access attempts:
{
"timestamp": "2025-07-28T12:00:00Z",
"user_id": "string",
"room_id": "string",
"access_granted": true,
"device_id": "string"
}
app/
├── models/ # Pydantic models
│ ├── __init__.py
│ ├── permission.py # Permission and TimeSlot models
│ ├── access_log.py # AccessLog model
│ ├── user.py # User model
│ ├── gateway.py # Gateway and Message models
│ ├── session.py # Session and authentication models
│ └── report.py # Report models
├── services/ # Business logic
│ ├── __init__.py
│ ├── database.py # Database service
│ ├── permission_manager.py # Permission management
│ ├── gateway_comm_service.py # Gateway communication
│ └── session_manager.py # Session management
├── api/ # API endpoints
│ ├── __init__.py
│ ├── auth.py # Authentication endpoints
│ ├── permissions.py # Permission management endpoints
│ ├── users.py # User management endpoints
│ ├── access_logs.py # Access log endpoints
│ ├── gateways.py # Gateway endpoints
│ └── reports.py # Report generation endpoints
└── core/ # Configuration
├── __init__.py
└── config.py # Application settings
# Install test dependencies
uv add --dev pytest pytest-asyncio httpx
# Run tests
uv run pytest
The application uses environment variables for configuration. Create a .env
file:
SECRET_KEY=your-secret-key-change-in-production
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30
GATEWAY_TIMEOUT=30
MAX_RETRY_ATTEMPTS=3
DEBUG=false
- Change the
SECRET_KEY
in production - Use HTTPS in production environments
- Configure CORS appropriately for your frontend
- Use of proper password hashing
- Validate and sanitize all inputs using pydantic