diff --git a/backend/chainlit/oauth_providers.py b/backend/chainlit/oauth_providers.py index 5ed9228f51..aa3e16269e 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", + ] 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()