|
1 | | -from fastapi import FastAPI |
| 1 | +from datetime import timedelta |
| 2 | +from typing import Optional |
2 | 3 |
|
3 | | -import functions |
4 | | -import models |
5 | | -import assets |
| 4 | +from fastapi import FastAPI, Depends, HTTPException, status |
| 5 | +from fastapi.security import OAuth2PasswordRequestForm |
| 6 | +from pydantic import ValidationError |
| 7 | +from starlette.responses import JSONResponse |
| 8 | + |
| 9 | +from functions.hashing import get_current_active_user, authenticate_user, get_password_hash |
| 10 | +from functions.sessionkey import create_access_token |
| 11 | +from functions.user import create_user, get_user, is_teacher |
| 12 | +from models.token import Token |
| 13 | +from models.user import User |
| 14 | +from models.apikey import ApiKey |
6 | 15 |
|
7 | 16 | app = FastAPI() |
8 | 17 |
|
9 | 18 |
|
| 19 | +# Post |
| 20 | + |
| 21 | + |
| 22 | +@app.post("/token", response_model=Token) |
| 23 | +async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()): |
| 24 | + account = authenticate_user(form_data.username, form_data.password) |
| 25 | + if not account: |
| 26 | + raise HTTPException( |
| 27 | + status_code=status.HTTP_401_UNAUTHORIZED, |
| 28 | + detail="Incorrect username or password", |
| 29 | + headers={"WWW-Authenticate": "Bearer"}, |
| 30 | + ) |
| 31 | + access_token_expires = timedelta(weeks=4) |
| 32 | + access_token = create_access_token( |
| 33 | + data={"sub": account.EMAIL}, expires_delta=access_token_expires |
| 34 | + ) |
| 35 | + return {"access_token": access_token, "token_type": "bearer"} |
| 36 | + |
| 37 | + |
| 38 | +# User get Operrations |
| 39 | + |
| 40 | + |
| 41 | +@app.get("/users/me", response_model=User, description="shows the current active logged in user") |
| 42 | +async def read_users_me(current_user: User = Depends(get_current_active_user)): |
| 43 | + return current_user |
| 44 | + |
| 45 | + |
| 46 | +@app.get("/users/{id}", response_model=User) |
| 47 | +async def read_user_by_id(id: int, current_user: User = Depends(get_current_active_user)): |
| 48 | + if current_user.PERMISSION_LEVEL >= 3: |
| 49 | + return get_user(ID=id) |
| 50 | + else: |
| 51 | + return JSONResponse(status_code=status.HTTP_403_FORBIDDEN, |
| 52 | + content="Not sufficent Permissions to view other users") |
| 53 | + |
| 54 | + |
| 55 | +# Put |
| 56 | + |
| 57 | +@app.put("/admin/create/user") |
| 58 | +async def admin_create_user(user: User, apikey: Optional[ApiKey] = None, |
| 59 | + current_user: User = Depends(get_current_active_user)): |
| 60 | + if current_user.PERMISSION_LEVEL >= 3: |
| 61 | + user.PASSWORD_HASH = get_password_hash(user.PASSWORD_HASH) |
| 62 | + create_user(user) |
| 63 | + return JSONResponse(status_code=status.HTTP_201_CREATED, content="User was successfully created") |
| 64 | + else: |
| 65 | + return JSONResponse( |
| 66 | + status_code=status.HTTP_403_FORBIDDEN, |
| 67 | + content="Not sufficent Permissions to create other users" |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +@app.put("/create/user") |
| 72 | +async def form_create_user(api_key: str, u_email: str, u_password: str, u_class: str): |
| 73 | + try: |
| 74 | + ApiKey(APIKEY=api_key) |
| 75 | + except ValidationError as e: |
| 76 | + return JSONResponse( |
| 77 | + status_code=status.HTTP_403_FORBIDDEN, |
| 78 | + content=e.errors() |
| 79 | + ) |
| 80 | + |
| 81 | + PERMISSION_LEVEL = 0 |
| 82 | + if is_teacher(u_email): |
| 83 | + PERMISSION_LEVEL = 1 |
10 | 84 |
|
| 85 | + NAME = u_email.split(".")[0].capitalize() |
| 86 | + LASTNAME = u_email.split(".")[1].split("@")[0].capitalize() |
11 | 87 |
|
| 88 | + if LASTNAME[:-2].isdigit(): |
| 89 | + LASTNAME = LASTNAME[:-2] |
12 | 90 |
|
13 | 91 |
|
| 92 | + try: |
| 93 | + account = User( |
| 94 | + EMAIL=u_email, |
| 95 | + PASSWORD_HASH=get_password_hash(u_password), |
| 96 | + NAME=NAME, |
| 97 | + LASTNAME=LASTNAME, |
| 98 | + CLASS=u_class, |
| 99 | + PERMISSION_LEVEL=PERMISSION_LEVEL, |
| 100 | + ACTIVE=False |
| 101 | + ) |
| 102 | + except ValidationError as e: |
| 103 | + raise HTTPException( |
| 104 | + status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, |
| 105 | + detail=e.errors() |
| 106 | + ) |
| 107 | + create_user(account) |
| 108 | + return JSONResponse( |
| 109 | + status_code=status.HTTP_200_OK, |
| 110 | + content="User succesfully created" |
| 111 | + ) |
0 commit comments