Skip to content

Commit

Permalink
Add support for CLI auth with Terra (#48)
Browse files Browse the repository at this point in the history
* Add support for CLI auth with Terra

* Improve error message for missing auth header
  • Loading branch information
dinvlad authored and simonjmendelsohn committed Feb 15, 2024
1 parent 5aa5747 commit 9cb1997
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
14 changes: 7 additions & 7 deletions src/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,18 @@ async def _get_azure_b2c_user(auth_header: str):
return decoded_token


async def get_auth_key_user(
request: Request, authenticate_user: bool = True
) -> dict:
auth_key = request.headers.get(AUTH_HEADER)
if not auth_key:
logger.error("no authorization key provided")
async def get_cli_user(req: Request) -> dict:
auth_header = req.headers.get(AUTH_HEADER)
if not auth_header:
logger.error("no authorization token or key provided")
return {}
elif constants.TERRA:
return _get_terra_user(auth_header)

db: firestore.AsyncClient = current_app.config["DATABASE"]
doc = (
await db.collection("users").document("auth_keys").get()
).to_dict().get(auth_key)
).to_dict().get(auth_header)

if not doc:
logger.error("invalid authorization key")
Expand Down
12 changes: 6 additions & 6 deletions src/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from quart import Blueprint, current_app, request

from src.auth import get_auth_key_user
from src.auth import get_cli_user
from src.utils import custom_logging
from src.utils.api_functions import (process_parameter, process_status,
process_task)
Expand All @@ -16,7 +16,7 @@

@bp.route("/upload_file", methods=["POST"])
async def upload_file() -> Tuple[dict, int]:
user = await get_auth_key_user(request)
user = await get_cli_user(request)
if not user:
return {"error": "unauthorized"}, 401

Expand Down Expand Up @@ -58,7 +58,7 @@ async def upload_file() -> Tuple[dict, int]:

@bp.route("/get_doc_ref_dict", methods=["GET"])
async def get_doc_ref_dict() -> Tuple[dict, int]:
user = await get_auth_key_user(request)
user = await get_cli_user(request)
if not user:
return {"error": "unauthorized"}, 401

Expand All @@ -71,7 +71,7 @@ async def get_doc_ref_dict() -> Tuple[dict, int]:

@bp.route("/get_username", methods=["GET"])
async def get_username() -> Tuple[dict, int]:
user = await get_auth_key_user(request)
user = await get_cli_user(request)
if not user:
return {"error": "unauthorized"}, 401

Expand All @@ -80,7 +80,7 @@ async def get_username() -> Tuple[dict, int]:

@bp.route("/update_firestore", methods=["GET"])
async def update_firestore() -> Tuple[dict, int]:
user = await get_auth_key_user(request)
user = await get_cli_user(request)
if not user:
return {"error": "unauthorized"}, 401

Expand Down Expand Up @@ -117,7 +117,7 @@ async def update_firestore() -> Tuple[dict, int]:

@bp.route("/create_cp0", methods=["GET"])
async def create_cp0() -> Tuple[dict, int]:
user = await get_auth_key_user(request)
user = await get_cli_user(request)
if not user:
return {"error": "unauthorized"}, 401

Expand Down
4 changes: 2 additions & 2 deletions src/signaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from quart_cors import websocket_cors

from src.api_utils import get_websocket_origin
from src.auth import get_user_id, get_auth_key_user
from src.auth import get_user_id, get_cli_user
from src.utils import constants

bp = Blueprint("signaling", __name__, url_prefix="/api")
Expand Down Expand Up @@ -130,7 +130,7 @@ async def _get_user_id(ws: Websocket):
if constants.TERRA:
return await get_user_id(ws)
else:
user = await get_auth_key_user(ws)
user = await get_cli_user(ws)
if user:
return user["username"]
else:
Expand Down

0 comments on commit 9cb1997

Please sign in to comment.