From 2f427f7cf3a4a036c13d9050c61e109c9a55e68f Mon Sep 17 00:00:00 2001 From: devangtomar Date: Sat, 26 Oct 2024 18:50:42 +0530 Subject: [PATCH 1/2] Adding support for `OAUTH_GITHUB_CUSTOM_URL` and `OAUTH_GITHUB_CUSTOM_API_URL` for GHE --- backend/chainlit/oauth_providers.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/backend/chainlit/oauth_providers.py b/backend/chainlit/oauth_providers.py index 5ed9228f51..2684bc3567 100644 --- a/backend/chainlit/oauth_providers.py +++ b/backend/chainlit/oauth_providers.py @@ -45,12 +45,25 @@ def get_prompt(self) -> Optional[str]: class GithubOAuthProvider(OAuthProvider): id = "github" - env = ["OAUTH_GITHUB_CLIENT_ID", "OAUTH_GITHUB_CLIENT_SECRET"] - authorize_url = "https://github.com/login/oauth/authorize" + env = [ + "OAUTH_GITHUB_CLIENT_ID", + "OAUTH_GITHUB_CLIENT_SECRET", + "OAUTH_GITHUB_CUSTOM_URL", + "OAUTH_GITHUB_CUSTOM_API_URL", # New environment variable for custom API URL + ] def __init__(self): self.client_id = os.environ.get("OAUTH_GITHUB_CLIENT_ID") self.client_secret = os.environ.get("OAUTH_GITHUB_CLIENT_SECRET") + self.github_domain = os.environ.get("OAUTH_GITHUB_CUSTOM_URL", "github.com") + self.api_url = os.environ.get( + "OAUTH_GITHUB_CUSTOM_API_URL", + f"https://api.{self.github_domain}", + ) + + self.authorize_url = f"https://{self.github_domain}/login/oauth/authorize" + self.access_token_url = f"https://{self.github_domain}/login/oauth/access_token" + self.authorize_params = { "scope": "user:email", } @@ -66,7 +79,7 @@ async def get_token(self, code: str, url: str): } async with httpx.AsyncClient() as client: response = await client.post( - "https://github.com/login/oauth/access_token", + self.access_token_url, data=payload, ) response.raise_for_status() @@ -81,14 +94,14 @@ async def get_token(self, code: str, url: str): async def get_user_info(self, token: str): async with httpx.AsyncClient() as client: user_response = await client.get( - "https://api.github.com/user", + f"{self.api_url}/user", headers={"Authorization": f"token {token}"}, ) user_response.raise_for_status() github_user = user_response.json() emails_response = await client.get( - "https://api.github.com/user/emails", + f"{self.api_url}/user/emails", headers={"Authorization": f"token {token}"}, ) emails_response.raise_for_status() From 4759942fe569c590d31f2767505184e52e96dc64 Mon Sep 17 00:00:00 2001 From: devangtomar Date: Sat, 26 Oct 2024 19:04:42 +0530 Subject: [PATCH 2/2] Refactor GithubOAuthProvider to clean up comments on environment variables --- backend/chainlit/oauth_providers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/chainlit/oauth_providers.py b/backend/chainlit/oauth_providers.py index 2684bc3567..aa3e16269e 100644 --- a/backend/chainlit/oauth_providers.py +++ b/backend/chainlit/oauth_providers.py @@ -49,7 +49,7 @@ class GithubOAuthProvider(OAuthProvider): "OAUTH_GITHUB_CLIENT_ID", "OAUTH_GITHUB_CLIENT_SECRET", "OAUTH_GITHUB_CUSTOM_URL", - "OAUTH_GITHUB_CUSTOM_API_URL", # New environment variable for custom API URL + "OAUTH_GITHUB_CUSTOM_API_URL", ] def __init__(self):