11import jwt
22
3- from fastapi .security import (
4- OAuth2PasswordBearer ,
5- APIKeyHeader ,
6- )
3+ from fastapi .security import OAuth2PasswordBearer
74from pydantic import BaseModel
85from datetime import timedelta , datetime , timezone
96from .config import env_vars
107from typing import Annotated
11- from fastapi import Depends , HTTPException , status , Security
8+ from fastapi import Depends , HTTPException , status
129from jwt .exceptions import InvalidTokenError
1310
1411oauth2_scheme = OAuth2PasswordBearer (
1512 tokenUrl = "token" ,
1613)
1714
18- api_key_header = APIKeyHeader (name = "X-API-KEY" , scheme_name = "api-key" , auto_error = False )
19- api_key_inst_header = APIKeyHeader (
20- name = "INST" , scheme_name = "api-inst" , auto_error = False
21- )
22- # The following is for use by the frontend enduser only.
23- api_key_enduser_header = APIKeyHeader (
24- name = "ENDUSER" , scheme_name = "api-enduser" , auto_error = False
25- )
26-
2715
2816class Token (BaseModel ):
2917 access_token : str
@@ -34,29 +22,6 @@ class TokenData(BaseModel):
3422 username : str | None = None
3523
3624
37- def get_api_key (
38- api_key_header : str = Security (api_key_header ),
39- api_key_inst_header : str = Security (api_key_inst_header ),
40- api_key_enduser_header : str = Security (api_key_enduser_header ),
41- ) -> tuple :
42- """Retrieve the api key and enduser header key if present.
43-
44- Args:
45- api_key_header: The API key passed in the HTTP header.
46-
47- Returns:
48- A tuple with the api key and enduser header if present. Authentication happens elsewhere.
49- Raises:
50- HTTPException: If the API key is invalid or missing.
51- """
52- if api_key_header :
53- return (api_key_header , api_key_inst_header , api_key_enduser_header )
54- raise HTTPException (
55- status_code = status .HTTP_401_UNAUTHORIZED ,
56- detail = "Invalid or missing API Key" ,
57- )
58-
59-
6025def check_creds (username : str , password : str ) -> bool :
6126 if username == env_vars ["USERNAME" ] and password == env_vars ["PASSWORD" ]:
6227 return True
0 commit comments