Spaces:
Building
Building
Update auth_controller.py
Browse files- auth_controller.py +17 -12
auth_controller.py
CHANGED
@@ -1,24 +1,29 @@
|
|
1 |
-
from fastapi import APIRouter,
|
2 |
from config_provider import get_config, ServiceConfig
|
3 |
-
from
|
4 |
-
import
|
5 |
|
6 |
router = APIRouter()
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
if not user:
|
15 |
raise HTTPException(status_code=401, detail="Invalid username or password")
|
16 |
|
17 |
-
|
18 |
-
if not bcrypt.checkpw(password.encode(), hashed):
|
19 |
raise HTTPException(status_code=401, detail="Invalid username or password")
|
20 |
|
21 |
-
return {"
|
22 |
|
23 |
@router.post("/change_password")
|
24 |
async def change_password(request: Request):
|
|
|
1 |
+
from fastapi import APIRouter, Depends, HTTPException
|
2 |
from config_provider import get_config, ServiceConfig
|
3 |
+
from pydantic import BaseModel
|
4 |
+
import hashlib
|
5 |
|
6 |
router = APIRouter()
|
7 |
|
8 |
+
class LoginRequest(BaseModel):
|
9 |
+
username: str
|
10 |
+
password: str
|
11 |
+
|
12 |
+
def verify_password(stored_hash, input_password):
|
13 |
+
# Basit SHA256 hash kontrolü (salt + hash mekanizması uygulanabilir)
|
14 |
+
input_hash = hashlib.sha256(input_password.encode()).hexdigest()
|
15 |
+
return stored_hash == input_hash
|
16 |
+
|
17 |
+
@router.post("/auth/login")
|
18 |
+
def login(request: LoginRequest, config: ServiceConfig = Depends(get_config)):
|
19 |
+
user = next((u for u in config.data.get('users', []) if u['username'] == request.username), None)
|
20 |
if not user:
|
21 |
raise HTTPException(status_code=401, detail="Invalid username or password")
|
22 |
|
23 |
+
if not verify_password(user['password_hash'], request.password):
|
|
|
24 |
raise HTTPException(status_code=401, detail="Invalid username or password")
|
25 |
|
26 |
+
return { "status": "success" }
|
27 |
|
28 |
@router.post("/change_password")
|
29 |
async def change_password(request: Request):
|