From b0cab01ab962e4a0f704012eac28786597726be1 Mon Sep 17 00:00:00 2001 From: Botond Hende Date: Sun, 25 Aug 2024 15:48:55 +0200 Subject: fastapi authentication wip --- __main__.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to '__main__.py') diff --git a/__main__.py b/__main__.py index 16b0f63..deacefa 100644 --- a/__main__.py +++ b/__main__.py @@ -1,15 +1,22 @@ -import os.path -from fastapi import FastAPI -from .datatemplates.agent_id import AgentId -from .modules import daemon -from .config import Config +from typing import Annotated + +from fastapi import FastAPI, Depends, Request +from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm app = FastAPI() +oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token") + + +@app.post("/token") +async def login(form_data: OAuth2PasswordRequestForm = Depends()): + user = form_data.username + return {"access_token": user, "token_type": "bearer"} + -@app.post("/tasks") -async def get_tasks(agent_id: AgentId): - return "foo" +@app.get("/{callsign}/tasks") +async def get_tasks(callsign: str, token: Annotated[str, Depends(oauth2_scheme)]): + return f'{{"callsign": "{callsign}", "token": "{token}"}}' # if __name__ == "__main__": # with open(os.path.join(os.path.dirname(__file__), Config.TOKEN_FILE_NAME)) as f: -- cgit v1.2.3-70-g09d2