Spaces:
Sleeping

StockScout / backend /auth.py
Sedin-HarishViswanath
Implement Triple-Check Hub safety and Smart-Port Email fallback
f5a3781
Raw History Blame Contribute Delete
5.2 kB
from datetime import datetime, timedelta
from typing import Optional
from jose import JWTError, jwt
import bcrypt
from fastapi.security import OAuth2PasswordBearer
from fastapi import Depends, HTTPException, status
from sqlalchemy.orm import Session
from .database import SessionLocal
from .models import User
# Secret key for JWT encoding/decoding
# In production, this should be in .env
SECRET_KEY = "your-secret-key-keep-it-secret"
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 60 # 1 hour
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="auth/token")
def verify_password(plain_password: str, hashed_password: str) -> bool:
return bcrypt.checkpw(plain_password.encode("utf-8"), hashed_password.encode("utf-8"))
def get_password_hash(password: str) -> str:
salt = bcrypt.gensalt()
return bcrypt.hashpw(password.encode("utf-8"), salt).decode("utf-8")
def generate_otp():
import random
return str(random.randint(100000, 999999))
def send_verification_email(email: str, code: str):
import smtplib
import os
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from dotenv import load_dotenv
load_dotenv()
smtp_server = os.getenv("SMTP_SERVER", "smtp.gmail.com")
smtp_port = int(os.getenv("SMTP_PORT", "587"))
smtp_user = os.getenv("SMTP_USER")
smtp_password = os.getenv("SMTP_PASSWORD")
email_from = os.getenv("EMAIL_FROM")
if not all([smtp_user, smtp_password, email_from]):
print(f"CRITICAL: SMTP settings missing from environment. Cannot send email to {email}.")
print(f"DEBUG: Code for {email}: {code}")
return False
message = MIMEMultipart()
message["From"] = email_from
message["To"] = email
message["Subject"] = "StockScout Verification Code"
body = f"""
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: auto; padding: 20px; border: 1px solid #eee; border-radius: 10px;">
<h2 style="color: #6366f1;">Welcome to StockScout!</h2>
<p>Your verification code is:</p>
<div style="background: #f4f4f5; padding: 15px; text-align: center; border-radius: 5px;">
<strong style="font-size: 32px; color: #4338ca; letter-spacing: 5px;">{code}</strong>
</div>
<p style="margin-top: 20px;">Please enter this code in the app to verify your account.</p>
<p style="color: #64748b; font-size: 12px; margin-top: 30px;">If you did not request this code, please ignore this email.</p>
</div>
"""
message.attach(MIMEText(body, "html"))
try:
print(f"Attempting SMTP via {smtp_server}:{smtp_port}...")
# If using port 465, use SMTP_SSL
if str(smtp_port) == "465":
with smtplib.SMTP_SSL(smtp_server, smtp_port, timeout=10) as server:
server.login(smtp_user, smtp_password)
server.send_message(message)
else:
# Standard TLS on 587
with smtplib.SMTP(smtp_server, smtp_port, timeout=10) as server:
server.starttls()
server.login(smtp_user, smtp_password)
server.send_message(message)
print(f"Email successfully sent to {email}")
return True
except smtplib.SMTPAuthenticationError:
print("SMTP ERROR: Authentication failed. Please check your SMTP_PASSWORD (likely needs a fresh Gmail App Password).")
return False
except Exception as e:
print(f"SMTP ERROR ({smtp_port}): {str(e)}")
# If 587 failed, maybe try 465 as a last resort?
if str(smtp_port) == "587":
try:
print("Trying fallback to SSL on 465...")
with smtplib.SMTP_SSL(smtp_server, 465, timeout=10) as server:
server.login(smtp_user, smtp_password)
server.send_message(message)
print(f"Email sent via SSL fallback!")
return True
except:
pass
return False
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
to_encode = data.copy()
if expires_delta:
expire = datetime.utcnow() + expires_delta
else:
expire = datetime.utcnow() + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
async def get_current_user(token: str = Depends(oauth2_scheme), db: Session = Depends(get_db)):
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
email: str = payload.get("sub")
if email is None:
raise credentials_exception
except JWTError:
raise credentials_exception
user = db.query(User).filter(User.email == email).first()
if user is None:
raise credentials_exception
return user