Spaces:
Runtime error
Runtime error
ScamDetect Bot commited on
Commit ·
ed8044d
1
Parent(s): 34499d6
Auto-sync backend and fix configuration
Browse files- backend/api/routes/auth.py +58 -0
- backend/models/schemas.py +3 -0
- backend/requirements.txt +1 -0
backend/api/routes/auth.py
CHANGED
|
@@ -10,9 +10,12 @@ from database import get_db
|
|
| 10 |
import models.db_models as db_models
|
| 11 |
import models.schemas as schemas
|
| 12 |
import os
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Configuration
|
| 15 |
SECRET_KEY = os.getenv("JWT_SECRET_KEY", "09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7")
|
|
|
|
| 16 |
ALGORITHM = "HS256"
|
| 17 |
ACCESS_TOKEN_EXPIRE_MINUTES = 60 * 24 * 7 # 1 week
|
| 18 |
|
|
@@ -99,3 +102,58 @@ def read_users_me(current_user: db_models.User = Depends(get_current_user)):
|
|
| 99 |
if not current_user:
|
| 100 |
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Not authenticated")
|
| 101 |
return current_user
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
import models.db_models as db_models
|
| 11 |
import models.schemas as schemas
|
| 12 |
import os
|
| 13 |
+
from google.oauth2 import id_token
|
| 14 |
+
from google.auth.transport import requests
|
| 15 |
|
| 16 |
# Configuration
|
| 17 |
SECRET_KEY = os.getenv("JWT_SECRET_KEY", "09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7")
|
| 18 |
+
GOOGLE_CLIENT_ID = os.getenv("GOOGLE_CLIENT_ID", "YOUR_GOOGLE_CLIENT_ID")
|
| 19 |
ALGORITHM = "HS256"
|
| 20 |
ACCESS_TOKEN_EXPIRE_MINUTES = 60 * 24 * 7 # 1 week
|
| 21 |
|
|
|
|
| 102 |
if not current_user:
|
| 103 |
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Not authenticated")
|
| 104 |
return current_user
|
| 105 |
+
|
| 106 |
+
@router.post("/google", response_model=schemas.Token)
|
| 107 |
+
def google_auth(google_token: schemas.GoogleToken, db: Session = Depends(get_db)):
|
| 108 |
+
try:
|
| 109 |
+
# Verify the Google token
|
| 110 |
+
idinfo = id_token.verify_oauth2_token(
|
| 111 |
+
google_token.token,
|
| 112 |
+
requests.Request(),
|
| 113 |
+
GOOGLE_CLIENT_ID,
|
| 114 |
+
clock_skew_in_seconds=10
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
email = idinfo['email']
|
| 118 |
+
name = idinfo.get('name', email.split('@')[0])
|
| 119 |
+
|
| 120 |
+
# Check if user exists
|
| 121 |
+
user = db.query(db_models.User).filter(db_models.User.email == email).first()
|
| 122 |
+
|
| 123 |
+
if not user:
|
| 124 |
+
# Create a new user if they don't exist
|
| 125 |
+
# Generate a random password since they use Google to login
|
| 126 |
+
import secrets
|
| 127 |
+
random_password = secrets.token_urlsafe(32)
|
| 128 |
+
hashed_password = get_password_hash(random_password)
|
| 129 |
+
|
| 130 |
+
# Ensure username is unique
|
| 131 |
+
base_username = name.lower().replace(" ", "")
|
| 132 |
+
username = base_username
|
| 133 |
+
counter = 1
|
| 134 |
+
while db.query(db_models.User).filter(db_models.User.username == username).first():
|
| 135 |
+
username = f"{base_username}{counter}"
|
| 136 |
+
counter += 1
|
| 137 |
+
|
| 138 |
+
user = db_models.User(
|
| 139 |
+
username=username,
|
| 140 |
+
email=email,
|
| 141 |
+
hashed_password=hashed_password
|
| 142 |
+
)
|
| 143 |
+
db.add(user)
|
| 144 |
+
db.commit()
|
| 145 |
+
db.refresh(user)
|
| 146 |
+
|
| 147 |
+
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
|
| 148 |
+
access_token = create_access_token(
|
| 149 |
+
data={"sub": user.username}, expires_delta=access_token_expires
|
| 150 |
+
)
|
| 151 |
+
return {"access_token": access_token, "token_type": "bearer"}
|
| 152 |
+
|
| 153 |
+
except ValueError as e:
|
| 154 |
+
# Invalid token
|
| 155 |
+
raise HTTPException(
|
| 156 |
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 157 |
+
detail=f"Invalid Google token: {str(e)}",
|
| 158 |
+
headers={"WWW-Authenticate": "Bearer"},
|
| 159 |
+
)
|
backend/models/schemas.py
CHANGED
|
@@ -23,6 +23,9 @@ class Token(BaseModel):
|
|
| 23 |
class TokenData(BaseModel):
|
| 24 |
username: Optional[str] = None
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
class FeatureExplanation(BaseModel):
|
| 27 |
feature: str
|
| 28 |
description: str
|
|
|
|
| 23 |
class TokenData(BaseModel):
|
| 24 |
username: Optional[str] = None
|
| 25 |
|
| 26 |
+
class GoogleToken(BaseModel):
|
| 27 |
+
token: str
|
| 28 |
+
|
| 29 |
class FeatureExplanation(BaseModel):
|
| 30 |
feature: str
|
| 31 |
description: str
|
backend/requirements.txt
CHANGED
|
@@ -46,6 +46,7 @@ python-jose>=3.3.0
|
|
| 46 |
passlib>=1.7.4
|
| 47 |
bcrypt>=4.1.2
|
| 48 |
email-validator>=2.1.0
|
|
|
|
| 49 |
python-dateutil>=2.8.2
|
| 50 |
reportlab>=4.0.0
|
| 51 |
|
|
|
|
| 46 |
passlib>=1.7.4
|
| 47 |
bcrypt>=4.1.2
|
| 48 |
email-validator>=2.1.0
|
| 49 |
+
google-auth>=2.23.0
|
| 50 |
python-dateutil>=2.8.2
|
| 51 |
reportlab>=4.0.0
|
| 52 |
|