Commit ff28793e authored by nanahira's avatar nanahira

improve auth

parent aefe7700
Pipeline #17243 passed with stages
in 25 minutes and 3 seconds
...@@ -8,8 +8,8 @@ services: ...@@ -8,8 +8,8 @@ services:
- ./models:/app/models - ./models:/app/models
- ./main.py:/app/main.py:ro - ./main.py:/app/main.py:ro
- ./hydra_node:/app/hydra_node:ro - ./hydra_node:/app/hydra_node:ro
- ./static:/app/static:ro #- ./static:/app/static:ro
environment: environment:
# TOKEN_SERVER: https://api.moecube.com/accounts/authUser # TOKEN_SERVER: https://api.moecube.com/accounts/authUser
TOKEN: mycard TOKEN: mycard
WITH_STATIC: 1 #WITH_STATIC: 1
...@@ -52,16 +52,9 @@ hostname = socket.gethostname() ...@@ -52,16 +52,9 @@ hostname = socket.gethostname()
sent_first_message = False sent_first_message = False
def verify_token(req: Request): def verify_token(req: Request):
if TOKEN: if not TOKEN and not TOKEN_SERVER:
valid = "Authorization" in req.headers and req.headers["Authorization"] == "Bearer "+TOKEN # no auth
if valid:
return True return True
if not TOKEN_SERVER and not valid:
raise HTTPException(
status_code=401,
detail="Unauthorized"
)
if TOKEN_SERVER:
if "Authorization" not in req.headers: if "Authorization" not in req.headers:
raise HTTPException( raise HTTPException(
status_code=401, status_code=401,
...@@ -70,6 +63,9 @@ def verify_token(req: Request): ...@@ -70,6 +63,9 @@ def verify_token(req: Request):
token = req.headers["Authorization"] token = req.headers["Authorization"]
if token.startswith("Bearer "): if token.startswith("Bearer "):
token = token[7:] token = token[7:]
if TOKEN and token == TOKEN:
return True
if TOKEN_SERVER:
tokenAuthResult = requests.get(TOKEN_SERVER, headers={"Authorization": "Bearer "+token}) tokenAuthResult = requests.get(TOKEN_SERVER, headers={"Authorization": "Bearer "+token})
if tokenAuthResult.status_code >= 400: if tokenAuthResult.status_code >= 400:
raise HTTPException( raise HTTPException(
...@@ -77,6 +73,10 @@ def verify_token(req: Request): ...@@ -77,6 +73,10 @@ def verify_token(req: Request):
detail="Unauthorized" detail="Unauthorized"
) )
return True return True
raise HTTPException(
status_code=401,
detail="Unauthorized"
)
#Initialize fastapi #Initialize fastapi
app = FastAPI() app = FastAPI()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment