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

1from fastapi import HTTPException, status 

2 

3 

4def verify_api_token(token: str | None): 

5 invalid_token_exception = HTTPException( 

6 status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid Token" 

7 ) 

8 

9 if token is None: 

10 raise invalid_token_exception 

11 

12 try: 

13 token = token.split(" ")[1] 

14 except IndexError: 

15 raise invalid_token_exception 

16 

17 with open("./api-tokens.txt", "r") as file: 

18 tokens: list[str] = [x.strip() for x in file.readlines()] 

19 

20 if token not in tokens: 

21 raise invalid_token_exception