Spaces:
Running
Running
Monteeq CI commited on
Commit ·
b7698ad
1
Parent(s): 3c22770
🚀 Deploy from GitHub @ 2f5ff0ba488e96705bf080ea29acd493f2455338
Browse files- app/api/v1/endpoints/auth.py +1 -1
- app/api/v1/endpoints/security_ops.py +0 -170
- app/crud/user.py +5 -1
- tests/test_user_avatar.py +63 -0
app/api/v1/endpoints/auth.py
CHANGED
|
@@ -79,7 +79,7 @@ def register_user(request: Request, user_in: schemas.UserCreate, db: Session = D
|
|
| 79 |
if db_user:
|
| 80 |
raise HTTPException(status_code=400, detail="Username already taken")
|
| 81 |
|
| 82 |
-
user = crud_user.create_user(db, user=user_in, is_onboarded=
|
| 83 |
user.is_verified = False # Mandatory verification
|
| 84 |
db.commit()
|
| 85 |
|
|
|
|
| 79 |
if db_user:
|
| 80 |
raise HTTPException(status_code=400, detail="Username already taken")
|
| 81 |
|
| 82 |
+
user = crud_user.create_user(db, user=user_in, is_onboarded=False, ip_address=request.client.host)
|
| 83 |
user.is_verified = False # Mandatory verification
|
| 84 |
db.commit()
|
| 85 |
|
app/api/v1/endpoints/security_ops.py
DELETED
|
@@ -1,170 +0,0 @@
|
|
| 1 |
-
from fastapi import APIRouter, Depends, HTTPException, status, Body
|
| 2 |
-
from sqlalchemy.orm import Session
|
| 3 |
-
from typing import List
|
| 4 |
-
import pyotp
|
| 5 |
-
import qrcode
|
| 6 |
-
import io
|
| 7 |
-
import base64
|
| 8 |
-
import secrets
|
| 9 |
-
import json
|
| 10 |
-
import hashlib
|
| 11 |
-
from app.db.session import get_db
|
| 12 |
-
from app.core.dependencies import get_current_user
|
| 13 |
-
from app.models.models import User, UserSession
|
| 14 |
-
from app.schemas import schemas
|
| 15 |
-
from app.core.config import BASE_URL
|
| 16 |
-
|
| 17 |
-
router = APIRouter()
|
| 18 |
-
|
| 19 |
-
@router.post("/me/2fa/setup")
|
| 20 |
-
def setup_totp(
|
| 21 |
-
db: Session = Depends(get_db),
|
| 22 |
-
current_user: User = Depends(get_current_user)
|
| 23 |
-
):
|
| 24 |
-
"""Generate a new TOTP secret and return the provisioning URI (QR code)."""
|
| 25 |
-
if current_user.totp_secret:
|
| 26 |
-
# User already has 2FA set up (or at least a secret)
|
| 27 |
-
pass # We allow re-setup
|
| 28 |
-
|
| 29 |
-
secret = pyotp.random_base32()
|
| 30 |
-
current_user.totp_secret = secret
|
| 31 |
-
db.commit()
|
| 32 |
-
|
| 33 |
-
# Generate provisioning URI
|
| 34 |
-
uri = pyotp.totp.TOTP(secret).provisioning_uri(
|
| 35 |
-
name=current_user.email,
|
| 36 |
-
issuer_name="Monteeq"
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
# Generate QR Code as base64
|
| 40 |
-
img = qrcode.make(uri)
|
| 41 |
-
buffered = io.BytesIO()
|
| 42 |
-
img.save(buffered, format="PNG")
|
| 43 |
-
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 44 |
-
|
| 45 |
-
return {"qr_code": f"data:image/png;base64,{img_str}", "uri": uri, "secret": secret}
|
| 46 |
-
|
| 47 |
-
@router.post("/me/2fa/verify")
|
| 48 |
-
def verify_and_enable_totp(
|
| 49 |
-
code: str = Body(..., embed=True),
|
| 50 |
-
db: Session = Depends(get_db),
|
| 51 |
-
current_user: User = Depends(get_current_user)
|
| 52 |
-
):
|
| 53 |
-
"""Verify the first TOTP code and enable 2FA for the account."""
|
| 54 |
-
if not current_user.totp_secret:
|
| 55 |
-
raise HTTPException(
|
| 56 |
-
status_code=status.HTTP_400_BAD_REQUEST,
|
| 57 |
-
detail="2FA setup not initialized"
|
| 58 |
-
)
|
| 59 |
-
|
| 60 |
-
totp = pyotp.TOTP(current_user.totp_secret)
|
| 61 |
-
if totp.verify(code):
|
| 62 |
-
current_user.two_factor_enabled = True
|
| 63 |
-
db.commit()
|
| 64 |
-
return {"message": "2FA enabled successfully"}
|
| 65 |
-
else:
|
| 66 |
-
raise HTTPException(
|
| 67 |
-
status_code=status.HTTP_400_BAD_REQUEST,
|
| 68 |
-
detail="Invalid verification code"
|
| 69 |
-
)
|
| 70 |
-
|
| 71 |
-
@router.post("/me/2fa/disable")
|
| 72 |
-
def disable_totp(
|
| 73 |
-
db: Session = Depends(get_db),
|
| 74 |
-
current_user: User = Depends(get_current_user)
|
| 75 |
-
):
|
| 76 |
-
"""Disable TOTP 2FA."""
|
| 77 |
-
current_user.two_factor_enabled = False
|
| 78 |
-
current_user.totp_secret = None
|
| 79 |
-
db.commit()
|
| 80 |
-
return {"message": "2FA disabled successfully"}
|
| 81 |
-
|
| 82 |
-
@router.get("/me/sessions", response_model=List[schemas.SessionOut])
|
| 83 |
-
def get_sessions(
|
| 84 |
-
db: Session = Depends(get_db),
|
| 85 |
-
current_user: User = Depends(get_current_user)
|
| 86 |
-
):
|
| 87 |
-
"""List active sessions for the current user."""
|
| 88 |
-
sessions = db.query(UserSession).filter(UserSession.user_id == current_user.id).order_by(UserSession.last_active.desc()).all()
|
| 89 |
-
|
| 90 |
-
return [
|
| 91 |
-
schemas.SessionOut(
|
| 92 |
-
id=s.id,
|
| 93 |
-
device_info=s.device_info or "Unknown Device",
|
| 94 |
-
ip_address=s.ip_address or "Unknown IP",
|
| 95 |
-
last_active=s.last_active,
|
| 96 |
-
is_current=False # logic later
|
| 97 |
-
) for s in sessions
|
| 98 |
-
]
|
| 99 |
-
|
| 100 |
-
@router.delete("/me/sessions/{session_id}")
|
| 101 |
-
def revoke_session(
|
| 102 |
-
session_id: int,
|
| 103 |
-
db: Session = Depends(get_db),
|
| 104 |
-
current_user: User = Depends(get_current_user)
|
| 105 |
-
):
|
| 106 |
-
"""Revoke a specific session."""
|
| 107 |
-
session = db.query(UserSession).filter(
|
| 108 |
-
UserSession.id == session_id,
|
| 109 |
-
UserSession.user_id == current_user.id
|
| 110 |
-
).first()
|
| 111 |
-
|
| 112 |
-
if not session:
|
| 113 |
-
raise HTTPException(status_code=404, detail="Session not found")
|
| 114 |
-
|
| 115 |
-
db.delete(session)
|
| 116 |
-
db.commit()
|
| 117 |
-
return {"message": "Session revoked"}
|
| 118 |
-
|
| 119 |
-
@router.post("/me/deactivate")
|
| 120 |
-
def deactivate_account(
|
| 121 |
-
db: Session = Depends(get_db),
|
| 122 |
-
current_user: User = Depends(get_current_user)
|
| 123 |
-
):
|
| 124 |
-
"""Deactivate the current user's account."""
|
| 125 |
-
current_user.is_active = False
|
| 126 |
-
db.commit()
|
| 127 |
-
return {"message": "Account deactivated"}
|
| 128 |
-
|
| 129 |
-
@router.post("/me/2fa/recovery-codes", response_model=List[str])
|
| 130 |
-
def generate_recovery_codes(
|
| 131 |
-
db: Session = Depends(get_db),
|
| 132 |
-
current_user: User = Depends(get_current_user)
|
| 133 |
-
):
|
| 134 |
-
"""Generate 10 new recovery codes and replace old ones."""
|
| 135 |
-
codes = []
|
| 136 |
-
hashed_codes = []
|
| 137 |
-
for _ in range(10):
|
| 138 |
-
# Format: XXXX-XXXX-XXXX
|
| 139 |
-
code = secrets.token_hex(6).upper()
|
| 140 |
-
formatted_code = f"{code[:4]}-{code[4:8]}-{code[8:]}"
|
| 141 |
-
codes.append(formatted_code)
|
| 142 |
-
|
| 143 |
-
# Store clean hash (no dashes, upper)
|
| 144 |
-
clean_code = code.upper()
|
| 145 |
-
hashed_codes.append(hashlib.sha256(clean_code.encode()).hexdigest())
|
| 146 |
-
|
| 147 |
-
current_user.recovery_codes = json.dumps(hashed_codes)
|
| 148 |
-
db.commit()
|
| 149 |
-
return codes
|
| 150 |
-
|
| 151 |
-
@router.get("/me/2fa/recovery-codes-status")
|
| 152 |
-
def check_recovery_codes(
|
| 153 |
-
current_user: User = Depends(get_current_user)
|
| 154 |
-
):
|
| 155 |
-
"""Check how many recovery codes are left."""
|
| 156 |
-
if not current_user.recovery_codes:
|
| 157 |
-
return {"count": 0}
|
| 158 |
-
codes = json.loads(current_user.recovery_codes)
|
| 159 |
-
return {"count": len(codes)}
|
| 160 |
-
|
| 161 |
-
@router.delete("/me/delete")
|
| 162 |
-
def delete_account(
|
| 163 |
-
db: Session = Depends(get_db),
|
| 164 |
-
current_user: User = Depends(get_current_user)
|
| 165 |
-
):
|
| 166 |
-
"""Permanently delete the current user's account and all associated data."""
|
| 167 |
-
# SQLAlchemy cascade will handle related records if configured correctly
|
| 168 |
-
db.delete(current_user)
|
| 169 |
-
db.commit()
|
| 170 |
-
return {"message": "Account deleted forever"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/crud/user.py
CHANGED
|
@@ -42,11 +42,15 @@ def create_user(db: Session, user: schemas.UserCreate, google_id: str = None, is
|
|
| 42 |
if not detected_country and ip_address:
|
| 43 |
detected_country = get_country_from_ip(ip_address)
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
db_user = User(
|
| 46 |
username=user.username,
|
| 47 |
email=user.email,
|
| 48 |
full_name=user.full_name,
|
| 49 |
-
profile_pic=
|
| 50 |
hashed_password=hashed_password,
|
| 51 |
google_id=google_id,
|
| 52 |
is_onboarded=is_onboarded,
|
|
|
|
| 42 |
if not detected_country and ip_address:
|
| 43 |
detected_country = get_country_from_ip(ip_address)
|
| 44 |
|
| 45 |
+
profile_pic = user.profile_pic
|
| 46 |
+
if not profile_pic and user.username:
|
| 47 |
+
profile_pic = f"https://api.dicebear.com/7.x/adventurer/svg?seed={user.username}"
|
| 48 |
+
|
| 49 |
db_user = User(
|
| 50 |
username=user.username,
|
| 51 |
email=user.email,
|
| 52 |
full_name=user.full_name,
|
| 53 |
+
profile_pic=profile_pic,
|
| 54 |
hashed_password=hashed_password,
|
| 55 |
google_id=google_id,
|
| 56 |
is_onboarded=is_onboarded,
|
tests/test_user_avatar.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
from sqlalchemy import create_engine
|
| 3 |
+
from sqlalchemy.orm import sessionmaker
|
| 4 |
+
|
| 5 |
+
from app.models.models import User
|
| 6 |
+
from app.crud import user as crud_user
|
| 7 |
+
from app.schemas import schemas
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
@pytest.fixture
|
| 11 |
+
def db_session():
|
| 12 |
+
engine = create_engine("sqlite:///:memory:")
|
| 13 |
+
tables = [User.__table__]
|
| 14 |
+
User.metadata.create_all(engine, tables=tables)
|
| 15 |
+
session = sessionmaker(bind=engine)()
|
| 16 |
+
try:
|
| 17 |
+
yield session
|
| 18 |
+
finally:
|
| 19 |
+
session.close()
|
| 20 |
+
engine.dispose()
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def test_create_user_default_dicebear_avatar(db_session):
|
| 24 |
+
user_in = schemas.UserCreate(
|
| 25 |
+
username="alexedit",
|
| 26 |
+
email="alex@example.com",
|
| 27 |
+
password="Password123!",
|
| 28 |
+
full_name="Alex Editor"
|
| 29 |
+
)
|
| 30 |
+
user = crud_user.create_user(db_session, user=user_in, is_onboarded=False)
|
| 31 |
+
assert user.id is not None
|
| 32 |
+
assert user.profile_pic == "https://api.dicebear.com/7.x/adventurer/svg?seed=alexedit"
|
| 33 |
+
assert user.is_onboarded is False
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def test_create_user_explicit_avatar_preserved(db_session):
|
| 37 |
+
custom_pic = "https://api.dicebear.com/7.x/bottts/svg?seed=RoboFan"
|
| 38 |
+
user_in = schemas.UserCreate(
|
| 39 |
+
username="robofan",
|
| 40 |
+
email="robo@example.com",
|
| 41 |
+
password="Password123!",
|
| 42 |
+
profile_pic=custom_pic
|
| 43 |
+
)
|
| 44 |
+
user = crud_user.create_user(db_session, user=user_in)
|
| 45 |
+
assert user.profile_pic == custom_pic
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def test_update_user_dicebear_avatar(db_session):
|
| 49 |
+
user_in = schemas.UserCreate(
|
| 50 |
+
username="sammy",
|
| 51 |
+
email="sammy@example.com",
|
| 52 |
+
password="Password123!"
|
| 53 |
+
)
|
| 54 |
+
user = crud_user.create_user(db_session, user=user_in)
|
| 55 |
+
new_avatar = "https://api.dicebear.com/7.x/lorelei/svg?seed=Felix"
|
| 56 |
+
|
| 57 |
+
updated = crud_user.update_user(
|
| 58 |
+
db_session,
|
| 59 |
+
user_id=user.id,
|
| 60 |
+
user_update=schemas.UserUpdate(profile_pic=new_avatar, is_onboarded=True)
|
| 61 |
+
)
|
| 62 |
+
assert updated.profile_pic == new_avatar
|
| 63 |
+
assert updated.is_onboarded is True
|