Skip to content

Commit cce1aa4

Browse files
committed
Add Python auth service
1 parent 3fdd277 commit cce1aa4

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

services/auth-python/Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM python:3.11-slim
2+
WORKDIR /app
3+
COPY requirements.txt .
4+
RUN pip install --no-cache-dir -r requirements.txt
5+
COPY app ./app
6+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]

services/auth-python/app/auth.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import uuid
2+
from fastapi import APIRouter, HTTPException
3+
from pydantic import BaseModel
4+
5+
router = APIRouter()
6+
7+
USER_DB = {
8+
"alice": "password123",
9+
"bob": "hunter2",
10+
"admin": "admin"
11+
}
12+
13+
SESSIONS = {}
14+
15+
class LoginPayload(BaseModel):
16+
username: str
17+
password: str
18+
19+
@router.post("/login")
20+
async def login(payload: LoginPayload):
21+
real_password = USER_DB.get(payload.username)
22+
if real_password and real_password == payload.password:
23+
token = str(uuid.uuid4())
24+
SESSIONS[token] = payload.username
25+
return {"token": token}
26+
raise HTTPException(status_code=401, detail="Invalid credentials")
27+
28+
@router.get("/validate")
29+
async def validate_token(token: str):
30+
username = SESSIONS.get(token)
31+
if not username:
32+
raise HTTPException(status_code=401, detail="Invalid token")
33+
return {"username": username}

services/auth-python/app/main.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from fastapi import FastAPI
2+
from app.auth import router as auth_router
3+
4+
app = FastAPI(
5+
title="Auth Service",
6+
version="1.0.0",
7+
docs_url="/docs", # Swagger UI
8+
redoc_url="/redoc" # ReDoc
9+
)
10+
app.include_router(auth_router, prefix="/auth")
11+
12+
@app.get("/")
13+
def healthcheck():
14+
return {"status": "ok"}

services/auth-python/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
fastapi==0.110.1
2+
uvicorn==0.29.0

0 commit comments

Comments
 (0)