Update auth.py
Browse files
auth.py
CHANGED
|
@@ -7,9 +7,8 @@ from database import get_db
|
|
| 7 |
import jwt
|
| 8 |
from passlib.context import CryptContext
|
| 9 |
from datetime import datetime, timedelta
|
| 10 |
-
from jwt import JWTError
|
| 11 |
|
| 12 |
-
|
| 13 |
auth_views = AuthViews()
|
| 14 |
class AuthViews:
|
| 15 |
def __init__(self):
|
|
@@ -17,18 +16,18 @@ class AuthViews:
|
|
| 17 |
self.SECRET_KEY = "your-secret-key" # Replace with your actual secret key
|
| 18 |
self.ALGORITHM = "HS256"
|
| 19 |
self.ACCESS_TOKEN_EXPIRE_MINUTES = 30
|
|
|
|
| 20 |
|
| 21 |
def verify_token(token: str = Depends(oauth2_scheme)):
|
| 22 |
try:
|
| 23 |
-
payload = jwt.decode(token,
|
| 24 |
return payload.get("sub")
|
| 25 |
-
except
|
| 26 |
raise HTTPException(
|
| 27 |
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 28 |
detail="Could not validate credentials",
|
| 29 |
headers={"WWW-Authenticate": "Bearer"},
|
| 30 |
)
|
| 31 |
-
|
| 32 |
class UserCreate(BaseModel):
|
| 33 |
username: str
|
| 34 |
password: str
|
|
|
|
| 7 |
import jwt
|
| 8 |
from passlib.context import CryptContext
|
| 9 |
from datetime import datetime, timedelta
|
|
|
|
| 10 |
|
| 11 |
+
|
| 12 |
auth_views = AuthViews()
|
| 13 |
class AuthViews:
|
| 14 |
def __init__(self):
|
|
|
|
| 16 |
self.SECRET_KEY = "your-secret-key" # Replace with your actual secret key
|
| 17 |
self.ALGORITHM = "HS256"
|
| 18 |
self.ACCESS_TOKEN_EXPIRE_MINUTES = 30
|
| 19 |
+
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
| 20 |
|
| 21 |
def verify_token(token: str = Depends(oauth2_scheme)):
|
| 22 |
try:
|
| 23 |
+
payload = jwt.decode(token, auth_views.SECRET_KEY, algorithms=[auth_views.ALGORITHM])
|
| 24 |
return payload.get("sub")
|
| 25 |
+
except jwt.PyJWTError:
|
| 26 |
raise HTTPException(
|
| 27 |
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 28 |
detail="Could not validate credentials",
|
| 29 |
headers={"WWW-Authenticate": "Bearer"},
|
| 30 |
)
|
|
|
|
| 31 |
class UserCreate(BaseModel):
|
| 32 |
username: str
|
| 33 |
password: str
|