Upload files to "/"
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
import math
|
||||
from fastapi import HTTPException
|
||||
|
||||
def haversine(lat1, lon1, lat2, lon2):
|
||||
R = 6371 # Earth radius in KM
|
||||
|
||||
dLat = math.radians(lat2 - lat1)
|
||||
dLon = math.radians(lon2 - lon1)
|
||||
|
||||
a = (
|
||||
math.sin(dLat / 2) ** 2 +
|
||||
math.cos(math.radians(lat1)) *
|
||||
math.cos(math.radians(lat2)) *
|
||||
math.sin(dLon / 2) ** 2
|
||||
)
|
||||
|
||||
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
|
||||
|
||||
return R * c
|
||||
|
||||
|
||||
def calculate_eta(distance):
|
||||
prep_time = 10 # mins
|
||||
travel_time = distance * 5 # mins per km
|
||||
|
||||
eta = prep_time + travel_time
|
||||
|
||||
return f"{int(eta)}-{int(eta + 5)} mins"
|
||||
|
||||
# ================= AUTH =================
|
||||
from passlib.context import CryptContext
|
||||
from jose import jwt
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||
|
||||
SECRET_KEY = "secret123"
|
||||
ALGORITHM = "HS256"
|
||||
|
||||
|
||||
def hash_password(password: str):
|
||||
password = password[:72] # 🔥 FIX
|
||||
return pwd_context.hash(password)
|
||||
|
||||
|
||||
def verify_password(plain, hashed):
|
||||
plain = plain[:72]
|
||||
return pwd_context.verify(plain, hashed)
|
||||
|
||||
|
||||
def create_token(data: dict):
|
||||
to_encode = data.copy()
|
||||
to_encode["exp"] = datetime.utcnow() + timedelta(days=30)
|
||||
return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
|
||||
|
||||
# Admin
|
||||
from fastapi import Depends
|
||||
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
||||
|
||||
security = HTTPBearer()
|
||||
|
||||
def get_current_admin(
|
||||
credentials: HTTPAuthorizationCredentials = Depends(security)
|
||||
):
|
||||
try:
|
||||
payload = jwt.decode(
|
||||
credentials.credentials,
|
||||
SECRET_KEY,
|
||||
algorithms=[ALGORITHM]
|
||||
)
|
||||
return payload
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
detail="Invalid token"
|
||||
)
|
||||
Reference in New Issue
Block a user