Spaces:
Runtime error
Runtime error
New commits
Browse files- src/auth.py +28 -8
- src/routers/students.py +2 -3
src/auth.py
CHANGED
@@ -8,8 +8,9 @@ from src import crud, models
|
|
8 |
from src.database import get_db
|
9 |
from typing import Optional, Dict, Any # Added Any
|
10 |
from datetime import datetime, timedelta
|
11 |
-
import jwt
|
12 |
from typing import Union # For type hinting
|
|
|
|
|
13 |
|
14 |
# JWT Configuration - Loaded from models.py (which loads from .env)
|
15 |
SECRET_KEY = models.JWT_SECRET_KEY
|
@@ -18,6 +19,24 @@ ACCESS_TOKEN_EXPIRE_MINUTES = 30
|
|
18 |
|
19 |
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/token/login") # Path to token endpoint
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
# --- JWT Helper Functions ---
|
22 |
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None) -> str:
|
23 |
to_encode = data.copy()
|
@@ -110,15 +129,16 @@ async def authenticate_user(
|
|
110 |
Authenticates a user by username and password.
|
111 |
Returns the ORM User model if successful, raises HTTPException otherwise.
|
112 |
"""
|
113 |
-
|
114 |
|
115 |
-
if not
|
116 |
-
|
117 |
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
|
|
122 |
|
123 |
# Tag-based authentication (User/Student Authentication via RFID tag)
|
124 |
async def authenticate_tag_user_or_student( # Renamed for clarity
|
|
|
8 |
from src.database import get_db
|
9 |
from typing import Optional, Dict, Any # Added Any
|
10 |
from datetime import datetime, timedelta
|
|
|
11 |
from typing import Union # For type hinting
|
12 |
+
from jose import JWTError, jwt
|
13 |
+
from passlib.context import CryptContext # For password hashing
|
14 |
|
15 |
# JWT Configuration - Loaded from models.py (which loads from .env)
|
16 |
SECRET_KEY = models.JWT_SECRET_KEY
|
|
|
19 |
|
20 |
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/token/login") # Path to token endpoint
|
21 |
|
22 |
+
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") # Password hashing context
|
23 |
+
# Password hashing context from models.py
|
24 |
+
|
25 |
+
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
26 |
+
"""
|
27 |
+
Verifies a plain password against a hashed password.
|
28 |
+
Uses the CryptContext to verify the password.
|
29 |
+
"""
|
30 |
+
return pwd_context.verify(plain_password, hashed_password)
|
31 |
+
|
32 |
+
def get_password_hash(password: str) -> str:
|
33 |
+
"""
|
34 |
+
Hashes a password using the CryptContext.
|
35 |
+
This is used when creating or updating user passwords.
|
36 |
+
"""
|
37 |
+
return pwd_context.hash(password)
|
38 |
+
|
39 |
+
|
40 |
# --- JWT Helper Functions ---
|
41 |
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None) -> str:
|
42 |
to_encode = data.copy()
|
|
|
129 |
Authenticates a user by username and password.
|
130 |
Returns the ORM User model if successful, raises HTTPException otherwise.
|
131 |
"""
|
132 |
+
user = await run_in_threadpool(crud.get_user_by_username, db, username)
|
133 |
|
134 |
+
if not user:
|
135 |
+
return None # User not found, return None
|
136 |
|
137 |
+
is_password_valid = verify_password(password, user.hashed_password)
|
138 |
+
if not is_password_valid:
|
139 |
+
return None
|
140 |
+
|
141 |
+
return user # Return the ORM User model if password is valid
|
142 |
|
143 |
# Tag-based authentication (User/Student Authentication via RFID tag)
|
144 |
async def authenticate_tag_user_or_student( # Renamed for clarity
|
src/routers/students.py
CHANGED
@@ -13,9 +13,8 @@ from src.utils import format_student_clearance_details
|
|
13 |
|
14 |
router = APIRouter(
|
15 |
prefix="/api/students",
|
16 |
-
tags=["
|
17 |
-
|
18 |
-
)
|
19 |
|
20 |
@router.post("/", response_model=models.StudentResponse, status_code=status.HTTP_201_CREATED)
|
21 |
async def create_student_endpoint(
|
|
|
13 |
|
14 |
router = APIRouter(
|
15 |
prefix="/api/students",
|
16 |
+
tags=["students"],
|
17 |
+
)
|
|
|
18 |
|
19 |
@router.post("/", response_model=models.StudentResponse, status_code=status.HTTP_201_CREATED)
|
20 |
async def create_student_endpoint(
|