-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ENH] Add authentication to
/query
route (#323)
* add implicit OAuth flow + Google token verification for /query route * add dependencies for Google auth library * check auth env vars on startup * mock token/token verification and disable auth as needed in tests * add tests of auth utilities and filter irrelevant warnings * test empty query succeeds when auth is disabled
- Loading branch information
Showing
9 changed files
with
383 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
"""Functions for handling authentication. Same ones as used in Neurobagel's federation API.""" | ||
|
||
import os | ||
|
||
from fastapi import HTTPException, status | ||
from fastapi.security.utils import get_authorization_scheme_param | ||
from google.auth.exceptions import GoogleAuthError | ||
from google.auth.transport import requests | ||
from google.oauth2 import id_token | ||
|
||
AUTH_ENABLED = os.environ.get("NB_ENABLE_AUTH", "True").lower() == "true" | ||
CLIENT_ID = os.environ.get("NB_QUERY_CLIENT_ID", None) | ||
|
||
|
||
def check_client_id(): | ||
"""Check if the CLIENT_ID environment variable is set.""" | ||
# By default, if CLIENT_ID is not provided to verify_oauth2_token, | ||
# Google will simply skip verifying the audience claim of ID tokens. | ||
# This however can be a security risk, so we mandate that CLIENT_ID is set. | ||
if AUTH_ENABLED and CLIENT_ID is None: | ||
raise ValueError( | ||
"Authentication has been enabled (NB_ENABLE_AUTH) but the environment variable NB_QUERY_CLIENT_ID is not set. " | ||
"Please set NB_QUERY_CLIENT_ID to the Google client ID for your Neurobagel query tool deployment, to verify the audience claim of ID tokens." | ||
) | ||
|
||
|
||
def verify_token(token: str): | ||
"""Verify the Google ID token. Raise an HTTPException if the token is invalid.""" | ||
# Adapted from https://developers.google.com/identity/gsi/web/guides/verify-google-id-token#python | ||
try: | ||
# Extract the token from the "Bearer" scheme | ||
# (See https://github.com/tiangolo/fastapi/blob/master/fastapi/security/oauth2.py#L473-L485) | ||
# TODO: Check also if scheme of token is "Bearer"? | ||
_, param = get_authorization_scheme_param(token) | ||
id_info = id_token.verify_oauth2_token( | ||
param, requests.Request(), CLIENT_ID | ||
) | ||
# TODO: Remove print statement or turn into logging | ||
print("Token verified: ", id_info) | ||
except (GoogleAuthError, ValueError) as exc: | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail=f"Invalid token: {exc}", | ||
headers={"WWW-Authenticate": "Bearer"}, | ||
) from exc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.