ciyidogan commited on
Commit
a76636c
·
verified ·
1 Parent(s): 6aaab23

Update auth_controller.py

Browse files
Files changed (1) hide show
  1. auth_controller.py +17 -12
auth_controller.py CHANGED
@@ -1,24 +1,29 @@
1
- from fastapi import APIRouter, HTTPException, Request, Depends
2
  from config_provider import get_config, ServiceConfig
3
- from service_config import ServiceConfig
4
- import bcrypt
5
 
6
  router = APIRouter()
7
 
8
- @router.post("/login")
9
- async def login(request: Request, config: ServiceConfig = Depends(get_config)):
10
- data = await request.json()
11
- username = data.get("username")
12
- password = data.get("password")
13
- user = next((u for u in config.users if u["username"] == username), None)
 
 
 
 
 
 
14
  if not user:
15
  raise HTTPException(status_code=401, detail="Invalid username or password")
16
 
17
- hashed = user["password_hash"].encode()
18
- if not bcrypt.checkpw(password.encode(), hashed):
19
  raise HTTPException(status_code=401, detail="Invalid username or password")
20
 
21
- return {"message": "Login successful"}
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):