Skip to content

Commit 3ba129a

Browse files
committed
refactor: user application and presentation use integer ids
1 parent 0273b4f commit 3ba129a

3 files changed

Lines changed: 17 additions & 34 deletions

File tree

src/core/security/two_factor_auth.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import secrets
66
from typing import Literal
7-
from uuid import UUID
87

98
import pyotp
109

@@ -34,7 +33,7 @@ def __init__(
3433
self._email_service = email_service
3534
self._settings = get_settings()
3635

37-
async def setup_totp(self, user_id: UUID) -> dict[str, str]:
36+
async def setup_totp(self, user_id: int) -> dict[str, str]:
3837
"""Set up TOTP for a user.
3938
4039
Returns:
@@ -66,7 +65,7 @@ async def setup_totp(self, user_id: UUID) -> dict[str, str]:
6665
"qr_code_data": f"otpauth://totp/{issuer}:{user.email}?secret={secret}&issuer={issuer}",
6766
}
6867

69-
async def verify_totp_setup(self, user_id: UUID, code: str) -> dict[str, list[str]]:
68+
async def verify_totp_setup(self, user_id: int, code: str) -> dict[str, list[str]]:
7069
"""Verify TOTP setup and enable 2FA.
7170
7271
Args:
@@ -100,7 +99,7 @@ async def verify_totp_setup(self, user_id: UUID, code: str) -> dict[str, list[st
10099

101100
return {"backup_codes": backup_codes}
102101

103-
async def disable_totp(self, user_id: UUID, code: str) -> bool:
102+
async def disable_totp(self, user_id: int, code: str) -> bool:
104103
"""Disable TOTP 2FA for a user.
105104
106105
Args:
@@ -147,7 +146,7 @@ async def disable_totp(self, user_id: UUID, code: str) -> bool:
147146

148147
return True
149148

150-
async def send_email_2fa_code(self, user_id: UUID) -> bool:
149+
async def send_email_2fa_code(self, user_id: int) -> bool:
151150
"""Send a 2FA code via email.
152151
153152
Args:
@@ -199,7 +198,7 @@ async def send_email_2fa_code(self, user_id: UUID) -> bool:
199198

200199
return True
201200

202-
async def verify_email_2fa_code(self, user_id: UUID, code: str) -> bool:
201+
async def verify_email_2fa_code(self, user_id: int, code: str) -> bool:
203202
"""Verify an email-based 2FA code.
204203
205204
Args:
@@ -290,7 +289,7 @@ async def verify_2fa_code(
290289
return False
291290

292291
async def regenerate_backup_codes(
293-
self, user_id: UUID, verify_code: str
292+
self, user_id: int, verify_code: str
294293
) -> dict[str, list[str]]:
295294
"""Regenerate backup codes for a user.
296295

src/modules/user/presentation/dependency.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from fastapi import Depends
22
from sqlalchemy.ext.asyncio import AsyncSession
33

4-
from uuid import UUID
5-
64
from src.core.database.postgres.session import get_db, get_unit_of_work
75
from src.core.dependency.tenant import get_current_tenant_id
86
from src.core.email.factory import create_email_service
@@ -55,7 +53,7 @@
5553

5654
def get_user_repository(
5755
db: AsyncSession = Depends(get_db),
58-
tenant_id: UUID = Depends(get_current_tenant_id),
56+
tenant_id: int = Depends(get_current_tenant_id),
5957
) -> UserRepository:
6058
return SQLAlchemyUserRepository(db, tenant_id)
6159

@@ -66,7 +64,7 @@ def get_email_service() -> EmailService:
6664

6765
def get_refresh_token_repository(
6866
db: AsyncSession = Depends(get_db),
69-
tenant_id: UUID = Depends(get_current_tenant_id),
67+
tenant_id: int = Depends(get_current_tenant_id),
7068
) -> RefreshTokenRepository:
7169
return SQLAlchemyRefreshTokenRepository(db, tenant_id)
7270

@@ -77,14 +75,14 @@ def get_token_revocation_service() -> TokenRevocationService:
7775

7876
def get_audit_service(
7977
db: AsyncSession = Depends(get_db),
80-
tenant_id: UUID = Depends(get_current_tenant_id),
78+
tenant_id: int = Depends(get_current_tenant_id),
8179
) -> AuditService:
8280
return AuditService(SQLAlchemyAuditRepository(db, tenant_id))
8381

8482

8583
def get_account_lockout_service(
8684
db: AsyncSession = Depends(get_db),
87-
tenant_id: UUID = Depends(get_current_tenant_id),
85+
tenant_id: int = Depends(get_current_tenant_id),
8886
) -> AccountLockoutService:
8987
return AccountLockoutService(SQLAlchemyLoginAttemptRepository(db, tenant_id))
9088

src/modules/user/presentation/routers/two_factor_router.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ async def setup_totp(
6060
- Microsoft Authenticator
6161
- Any TOTP-compatible authenticator app
6262
"""
63-
from uuid import UUID
64-
65-
command = SetupTOTPCommand(user_id=UUID(current_user_id))
63+
command = SetupTOTPCommand(user_id=int(current_user_id))
6664
result = await handler.execute(command)
6765

6866
return SuccessResponse(
@@ -88,10 +86,8 @@ async def verify_totp_setup(
8886
After scanning the QR code, submit the 6-digit code from your authenticator app
8987
to complete the setup. This will return backup codes - store them safely!
9088
"""
91-
from uuid import UUID
92-
9389
command = VerifyTOTPSetupCommand(
94-
user_id=UUID(current_user_id),
90+
user_id=int(current_user_id),
9591
code=request.code,
9692
)
9793
result = await handler.execute(command)
@@ -118,10 +114,8 @@ async def disable_totp(
118114
119115
Requires either a current TOTP code or a backup code to verify identity.
120116
"""
121-
from uuid import UUID
122-
123117
command = DisableTOTPCommand(
124-
user_id=UUID(current_user_id),
118+
user_id=int(current_user_id),
125119
code=request.code,
126120
)
127121
result = await handler.execute(command)
@@ -152,9 +146,7 @@ async def send_email_2fa_code(
152146
Alternative to TOTP for users who prefer email-based verification.
153147
The code will expire in 10 minutes.
154148
"""
155-
from uuid import UUID
156-
157-
command = SendEmail2FACodeCommand(user_id=UUID(current_user_id))
149+
command = SendEmail2FACodeCommand(user_id=int(current_user_id))
158150
result = await handler.execute(command)
159151

160152
if result:
@@ -179,10 +171,8 @@ async def verify_email_2fa_code(
179171
handler: VerifyEmail2FACodeHandler = Depends(get_verify_email_2fa_code_handler),
180172
):
181173
"""Verify an email-based 2FA code."""
182-
from uuid import UUID
183-
184174
command = VerifyEmail2FACodeCommand(
185-
user_id=UUID(current_user_id),
175+
user_id=int(current_user_id),
186176
code=request.code,
187177
)
188178
result = await handler.execute(command)
@@ -215,10 +205,8 @@ async def regenerate_backup_codes(
215205
This will invalidate all previous backup codes and generate new ones.
216206
Requires a current TOTP code for verification.
217207
"""
218-
from uuid import UUID
219-
220208
command = RegenerateBackupCodesCommand(
221-
user_id=UUID(current_user_id),
209+
user_id=int(current_user_id),
222210
verify_code=request.verify_code,
223211
)
224212
result = await handler.execute(command)
@@ -246,10 +234,8 @@ async def verify_2fa(
246234
Used in the login flow when 2FA is required.
247235
Supports TOTP, email, and backup code methods.
248236
"""
249-
from uuid import UUID
250-
251237
command = Verify2FACommand(
252-
user_id=UUID(current_user_id),
238+
user_id=int(current_user_id),
253239
code=request.code,
254240
method=request.method,
255241
)

0 commit comments

Comments
 (0)