Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - Extend GitHub OAuth to Support GitHub Enterprise #1486

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
23 changes: 18 additions & 5 deletions backend/chainlit/oauth_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,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",
}
Expand All @@ -67,7 +80,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()
Expand All @@ -82,14 +95,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()
Expand Down
Loading