Coverage for microservice_websocket/app/utils/api_token.py: 90%
13 statements
« prev ^ index » next coverage.py v7.0.0, created at 2022-12-20 14:31 +0000
« prev ^ index » next coverage.py v7.0.0, created at 2022-12-20 14:31 +0000
1from fastapi import HTTPException, status
4def verify_api_token(token: str | None):
5 invalid_token_exception = HTTPException(
6 status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid Token"
7 )
9 if token is None:
10 raise invalid_token_exception
12 try:
13 token = token.split(" ")[1]
14 except IndexError:
15 raise invalid_token_exception
17 with open("./api-tokens.txt", "r") as file:
18 tokens: list[str] = [x.strip() for x in file.readlines()]
20 if token not in tokens:
21 raise invalid_token_exception