Skip to content

Commit 4bdaea0

Browse files
committed
feat: added option for api auth
1 parent 9ed2271 commit 4bdaea0

2 files changed

Lines changed: 8 additions & 7 deletions

File tree

src/webapp/authn.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616

1717
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
1818

19-
oauth2_apikey_scheme = OAuth2PasswordBearer(
20-
scheme_name="api_key_scheme",
21-
tokenUrl="token-from-api-key",
22-
)
23-
2419
api_key_header = APIKeyHeader(name="X-API-KEY", scheme_name="api-key", auto_error=False)
2520
# The INST value may be empty for Datakinder or cross-institution access.
2621
api_key_inst_header = APIKeyHeader(

src/webapp/main.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,24 @@ def read_root() -> Any:
9898
async def access_token_from_api_key(
9999
sql_session: Annotated[Session, Depends(get_session)],
100100
form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
101+
api_key_enduser_tuple: str = Security(get_api_key),
101102
) -> Token:
102103
"""Generate a token from an API key."""
104+
local_session.set(sql_session)
103105

106+
user = authenticate_api_key(api_key_enduser_tuple, local_session.get())
104107
valid = check_creds(form_data.username, form_data.password)
108+
logger.info(f"api_key input: {api_key_enduser_tuple}")
109+
logger.info(f"user: {user}")
110+
logger.info(f"valid creds: {valid}")
105111

106-
if not valid:
112+
if not user and not valid:
107113
raise HTTPException(
108114
status_code=status.HTTP_401_UNAUTHORIZED,
109115
detail="Invalid API key and credentials",
110116
headers={"WWW-Authenticate": "X-API-KEY"},
111117
)
112-
email = form_data.username
118+
email = user.email if user else form_data.username
113119
access_token_expires = timedelta(
114120
minutes=int(env_vars["ACCESS_TOKEN_EXPIRE_MINUTES"])
115121
)

0 commit comments

Comments
 (0)