From 20217c64e065a806b10f2bbd39f873997c5b6559 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Fredrik=20Ki=C3=A6r?= <31612826+anders-kiaer@users.noreply.github.com> Date: Wed, 27 Sep 2023 08:53:02 +0200 Subject: [PATCH] Add support for using GitHub codespace (#62) --- README.md | 18 +++++++++++++++++- backend/src/backend/auth/auth_helper.py | 13 +++++++++---- docker-compose.yml | 1 + 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 35a7a91af..c5479873c 100644 --- a/README.md +++ b/README.md @@ -62,4 +62,20 @@ git push upstream :review --force ``` The `main` branch only accepts commits through pull requests. -NB: Note that Radix will always use the `radixconfig.yml` as it is in `main` branch (unless changed in Radix UI). \ No newline at end of file +NB: Note that Radix will always use the `radixconfig.yml` as it is in `main` branch (unless changed in Radix UI). + +### Usage in GitHub codespaces + +Using the standard GitHub codespace image, you can easily start up the application by running +the same command as locally: +``` +docker-compose up +``` +in the terminal. When using GitHub codespaces you do not have to create the `.env` file since +environment variables are automatically set up for you at startup through repository settings. +Note that you need to have at least "collaborator" role in the repository in order to have +environment variables automatically set up. + +When you start up the docker containers, GitHub codespace will automatically make a link where +you can access the application in development mode (i.e. changes you do the code will automatically +be reflected in the application). diff --git a/backend/src/backend/auth/auth_helper.py b/backend/src/backend/auth/auth_helper.py index 041e4be4a..5b1315094 100644 --- a/backend/src/backend/auth/auth_helper.py +++ b/backend/src/backend/auth/auth_helper.py @@ -1,3 +1,4 @@ +import os import base64 import time from typing import List, Optional @@ -33,11 +34,15 @@ async def _login_route(self, request: Request, redirect_url_after_login: Optiona for value in config.RESOURCE_SCOPES_DICT.values(): all_scopes_list.extend(value) + if "CODESPACE_NAME" in os.environ: + # Developer is using GitHub codespace, so we use the GitHub codespace port forward URL + redirect_uri = f"https://{os.environ['CODESPACE_NAME']}-8080.app.github.dev/api/auth-callback" + print(f"You are using GitHub codespace. Remember to allow app registration redirect URI {redirect_uri}") + else: + redirect_uri = str(request.url_for("_authorized_callback_route")) + cca = _create_msal_confidential_client_app(token_cache=None) - flow_dict = cca.initiate_auth_code_flow( - scopes=all_scopes_list, - redirect_uri=str(request.url_for("_authorized_callback_route")), - ) + flow_dict = cca.initiate_auth_code_flow(scopes=all_scopes_list, redirect_uri=redirect_uri) request.session["flow"] = flow_dict diff --git a/docker-compose.yml b/docker-compose.yml index dda1034d3..f6e4e1c56 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -31,6 +31,7 @@ services: - WEBVIZ_SMDA_RESOURCE_SCOPE - WEBVIZ_SMDA_SUBSCRIPTION_KEY - WEBVIZ_SUMO_ENV + - CODESPACE_NAME # Automatically set env. variable by GitHub codespace volumes: - ./backend/src:/home/appuser/backend/src