|
|
|
@ -1,63 +1,31 @@
|
|
|
|
|
from fastapi import FastAPI, Depends, HTTPException, status
|
|
|
|
|
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
|
|
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
|
from typing import Optional
|
|
|
|
|
from jose import JWTError, jwt
|
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
from pymongo import MongoClient
|
|
|
|
|
from jose import JWTError, jwt
|
|
|
|
|
from bson.objectid import ObjectId
|
|
|
|
|
from app.utils import get_password_hash, verify_password
|
|
|
|
|
|
|
|
|
|
# Best workaround found for _id typed as ObjectId (creating Exception bcause JSON doesn't support custom types countrary to BSON, used by Mongo)
|
|
|
|
|
# also allows to create DTOs at the time, but not at it's best (project structure is chaotic FTM :s)
|
|
|
|
|
from serializers import friends_serialize, pins_serialize, users_serialize
|
|
|
|
|
from app.serializers import * # Import all serializers (detailed in __init__.py)
|
|
|
|
|
|
|
|
|
|
import hashlib
|
|
|
|
|
# Import all models (detailed in __init__.py)
|
|
|
|
|
from app.models import *
|
|
|
|
|
|
|
|
|
|
# Constants for JWT
|
|
|
|
|
SECRET_KEY = "_2YfT44$xF.Tg_xI63UH3D7:N+>pZN2';j%>7H@?e0:Xor'pV[" # temporary of course :)
|
|
|
|
|
ALGORITHM = "HS256" # TODO: check if broken (don't believe)
|
|
|
|
|
ACCESS_TOKEN_EXPIRE_MINUTES = 30 # TODO: check what to add here / maybe need to evaluate criticity of that?
|
|
|
|
|
# Contains all constants
|
|
|
|
|
from app.config import *
|
|
|
|
|
|
|
|
|
|
# Database setup
|
|
|
|
|
client = MongoClient("mongodb://localhost:27017/", username="mongoadmin", password="secret")
|
|
|
|
|
db = client["memorymap"]
|
|
|
|
|
client = MongoClient(MONGODB_URL, username=MONGODB_USERNAME, password=MONGODB_PASSWORD)
|
|
|
|
|
db = client[MONGODB_DATABASE]
|
|
|
|
|
|
|
|
|
|
# FastAPI app instance
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
# OAuth2 scheme
|
|
|
|
|
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="login")
|
|
|
|
|
|
|
|
|
|
# Pydantic models
|
|
|
|
|
class User(BaseModel):
|
|
|
|
|
uid: str = Field(..., alias="_id")
|
|
|
|
|
username: str
|
|
|
|
|
password: str
|
|
|
|
|
|
|
|
|
|
class Token(BaseModel):
|
|
|
|
|
access_token: str
|
|
|
|
|
token_type: str
|
|
|
|
|
|
|
|
|
|
class TokenData(BaseModel):
|
|
|
|
|
username: Optional[str] = None
|
|
|
|
|
|
|
|
|
|
class Pin(BaseModel):
|
|
|
|
|
title: str
|
|
|
|
|
description: str
|
|
|
|
|
|
|
|
|
|
class Friend(BaseModel):
|
|
|
|
|
user_id: str
|
|
|
|
|
|
|
|
|
|
# Collections
|
|
|
|
|
users_collection = db["users"]
|
|
|
|
|
pins_collection = db["pins"]
|
|
|
|
|
friends_collection = db["friends"]
|
|
|
|
|
|
|
|
|
|
# Utility functions
|
|
|
|
|
def verify_password(plain_password, hashed_password):
|
|
|
|
|
return hashlib.sha256(plain_password.encode()).hexdigest() == hashed_password
|
|
|
|
|
|
|
|
|
|
def get_password_hash(password):
|
|
|
|
|
return hashlib.sha256(password.encode()).hexdigest()
|
|
|
|
|
oauth2_scheme = OAuth2PasswordBearer(tokenUrl=TOKEN_URL)
|
|
|
|
|
|
|
|
|
|
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
|
|
|
|
|
to_encode = data.copy()
|
|
|
|
@ -94,6 +62,11 @@ async def get_current_user(token: str = Depends(oauth2_scheme)):
|
|
|
|
|
|
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
# Collections
|
|
|
|
|
users_collection = db["users"]
|
|
|
|
|
pins_collection = db["pins"]
|
|
|
|
|
friends_collection = db["friends"]
|
|
|
|
|
|
|
|
|
|
# Routes - TODO: find workaround to display 401/409/... HTTP error codes in openapi.json
|
|
|
|
|
@app.post("/register", response_model=Token)
|
|
|
|
|
async def register(user: User):
|