Update code in endpoints/countries.get.py
This commit is contained in:
parent
fea0b40757
commit
e30ae5b30d
@ -1,29 +1,129 @@
|
|||||||
from fastapi import APIRouter, Depends, HTTPException
|
from datetime import datetime, timedelta
|
||||||
from core.database import fake_users_db
|
from typing import Optional
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException, Response
|
||||||
|
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
|
||||||
|
from jose import JWTError, jwt
|
||||||
|
from passlib.context import CryptContext
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@router.get("/countries")
|
# Constants
|
||||||
async def african_countries_handler():
|
SECRET_KEY = "your-secret-key-here"
|
||||||
"""Get list of all African countries"""
|
ALGORITHM = "HS256"
|
||||||
african_countries = [
|
ACCESS_TOKEN_EXPIRE_MINUTES = 30
|
||||||
"Algeria", "Angola", "Benin", "Botswana", "Burkina Faso", "Burundi",
|
|
||||||
"Cameroon", "Cape Verde", "Central African Republic", "Chad", "Comoros",
|
# Password hashing
|
||||||
"Congo", "Democratic Republic of the Congo", "Djibouti", "Egypt",
|
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||||
"Equatorial Guinea", "Eritrea", "Ethiopia", "Gabon", "Gambia", "Ghana",
|
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="login")
|
||||||
"Guinea", "Guinea-Bissau", "Ivory Coast", "Kenya", "Lesotho", "Liberia",
|
|
||||||
"Libya", "Madagascar", "Malawi", "Mali", "Mauritania", "Mauritius",
|
# Models
|
||||||
"Morocco", "Mozambique", "Namibia", "Niger", "Nigeria", "Rwanda",
|
class User(BaseModel):
|
||||||
"Sao Tome and Principe", "Senegal", "Seychelles", "Sierra Leone",
|
username: str
|
||||||
"Somalia", "South Africa", "South Sudan", "Sudan", "Swaziland",
|
email: str
|
||||||
"Tanzania", "Togo", "Tunisia", "Uganda", "Zambia", "Zimbabwe"
|
full_name: Optional[str] = None
|
||||||
]
|
disabled: Optional[bool] = False
|
||||||
|
|
||||||
|
class UserInDB(User):
|
||||||
|
hashed_password: str
|
||||||
|
|
||||||
|
class Token(BaseModel):
|
||||||
|
access_token: str
|
||||||
|
token_type: str
|
||||||
|
|
||||||
|
class TokenData(BaseModel):
|
||||||
|
username: Optional[str] = None
|
||||||
|
|
||||||
|
# Helper functions
|
||||||
|
def verify_password(plain_password, hashed_password):
|
||||||
|
return pwd_context.verify(plain_password, hashed_password)
|
||||||
|
|
||||||
|
def get_password_hash(password):
|
||||||
|
return pwd_context.hash(password)
|
||||||
|
|
||||||
|
def get_user(username: str):
|
||||||
|
if username in fake_users_db:
|
||||||
|
user_dict = fake_users_db[username]
|
||||||
|
return UserInDB(**user_dict)
|
||||||
|
return None
|
||||||
|
|
||||||
|
def authenticate_user(username: str, password: str):
|
||||||
|
user = get_user(username)
|
||||||
|
if not user:
|
||||||
|
return False
|
||||||
|
if not verify_password(password, user.hashed_password):
|
||||||
|
return False
|
||||||
|
return user
|
||||||
|
|
||||||
|
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=15)
|
||||||
|
to_encode.update({"exp": expire})
|
||||||
|
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
|
||||||
|
return encoded_jwt
|
||||||
|
|
||||||
|
async def get_current_user(token: str = Depends(oauth2_scheme)):
|
||||||
|
credentials_exception = HTTPException(
|
||||||
|
status_code=401,
|
||||||
|
detail="Could not validate credentials",
|
||||||
|
headers={"WWW-Authenticate": "Bearer"},
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
||||||
|
username: str = payload.get("sub")
|
||||||
|
if username is None:
|
||||||
|
raise credentials_exception
|
||||||
|
token_data = TokenData(username=username)
|
||||||
|
except JWTError:
|
||||||
|
raise credentials_exception
|
||||||
|
user = get_user(username=token_data.username)
|
||||||
|
if user is None:
|
||||||
|
raise credentials_exception
|
||||||
|
return user
|
||||||
|
|
||||||
|
# Login endpoint
|
||||||
|
@router.post("/login", response_model=Token)
|
||||||
|
async def login(response: Response, form_data: OAuth2PasswordRequestForm = Depends()):
|
||||||
|
"""Authenticate user and return JWT token"""
|
||||||
|
user = authenticate_user(form_data.username, form_data.password)
|
||||||
|
if not user:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=401,
|
||||||
|
detail="Incorrect username or password",
|
||||||
|
headers={"WWW-Authenticate": "Bearer"},
|
||||||
|
)
|
||||||
|
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
|
||||||
|
access_token = create_access_token(
|
||||||
|
data={"sub": user.username}, expires_delta=access_token_expires
|
||||||
|
)
|
||||||
|
|
||||||
|
# Set cookie
|
||||||
|
response.set_cookie(
|
||||||
|
key="access_token",
|
||||||
|
value=f"Bearer {access_token}",
|
||||||
|
httponly=True,
|
||||||
|
max_age=1800,
|
||||||
|
expires=1800,
|
||||||
|
secure=True,
|
||||||
|
samesite="lax"
|
||||||
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"message": "African countries retrieved successfully",
|
"access_token": access_token,
|
||||||
"data": african_countries,
|
"token_type": "bearer",
|
||||||
"metadata": {
|
"message": "Login successful",
|
||||||
"total_countries": len(african_countries),
|
"user": {
|
||||||
"continent": "Africa"
|
"username": user.username,
|
||||||
|
"email": user.email,
|
||||||
|
"full_name": user.full_name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Protected route example
|
||||||
|
@router.get("/me", response_model=User)
|
||||||
|
async def read_users_me(current_user: User = Depends(get_current_user)):
|
||||||
|
"""Get current user information"""
|
||||||
|
return current_user
|
Loading…
x
Reference in New Issue
Block a user