def create_label(label: labels_schema.LabelCreate, Authorization: Optional[str] = Header(None), db: Session = Depends(get_db)): user_token = jwt_token(Authorization) if isinstance(user_token, dict): label_obj = labels_model.Label(**label.dict()) label_obj.owner_id = user_token.get("user_id") return label_obj.save_db(db) return JSONResponse(content={"detail": "Access denied"}, status_code=403)
async def get_servers(skip: int = 0, limit: int = 100, Authorization: Optional[str] = Header(None), db: Session = Depends(get_db)): user_token = jwt_token(Authorization) if isinstance( user_token, dict) and user_token.get("user_email") == "*****@*****.**": return servers_models.Server.get_servers(db, skip, limit) return JSONResponse(content={"msg": "Access denied"}, status_code=403)
def get_label_by_id(label_id: str = Body(default=None, embed=True), Authorization: Optional[str] = Header(None), db: Session = Depends(get_db)): user_token = jwt_token(Authorization) if isinstance(user_token, dict): label = labels_model.Label.get_label_by_id(db, label_id) try: if user_token.get("user_id") == str(label.owner_id): return label except Exception as err: print(err) return JSONResponse(content={"detail": "Access denied"}, status_code=403)
async def get_server_by_id(server_id: str = Body(default=None, embed=True), secret_key: str = Body(default=None, embed=True), Authorization: Optional[str] = Header(None), db: Session = Depends(get_db)): user_token = jwt_token(Authorization) if isinstance(user_token, dict): try: server = servers_models.Server.get_server_by_id( db, server_id, secret_key) if server: return server except Exception as err: print(err)
async def get_labels(Authorization: Optional[str] = Header(None), skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): """ :param Authorization: JWT Token required for Authorization :param skip: Default 0 :param limit: Default 100 :param db: get_db function returns db Session :return: all labels or Access Denied JSONResponse """ user_token = jwt_token(Authorization) if isinstance(user_token, dict): return labels_model.Label.get_labels(db, skip, limit) return JSONResponse(content={"detail": "Access denied"}, status_code=403)
async def login_verification(request: Request, call_next): response = await call_next(request) if request.url in [ "http://localhost:8000/auth/login", "http://localhost:8000/auth/registration", "http://localhost:8000/redoc", "http://localhost:8000/docs", "http://localhost:8000/openapi.json", ]: return response token = request.headers.get("authorization") if token and "Bearer" in token: token = jwt_token(token) if token: return response return JSONResponse(content={"detail": "Could not validate credentials"}, status_code=401)
async def create_server(server: servers_schema.ServerCreate, Authorization: Optional[str] = Header(None), db: Session = Depends(get_db)): user_token = jwt_token(Authorization) if isinstance(user_token, dict): label = labels_model.Label.get_label_by_id(db, server.label_id) if label and str(label.owner_id) == user_token.get("user_id"): ciphertext = encrypt(server.password, server.secret_key) server = server.dict(exclude={"secret_key", "password"}) server.update(**{ "ciphertext": ciphertext, "owner_id": user_token.get("user_id") }) server = servers_models.Server(**server) print(server) server.save_db(db) return server return JSONResponse(content={"msg": "Access denied"}, status_code=403)
async def delete_user(Authorization: Optional[str] = Header(None), db: Session = Depends(get_db)): user_token = jwt_token(Authorization) if isinstance(user_token, dict): return users_model.User.delete(db)