From e7971b58409790b25257e44ca4b19341cfa37234 Mon Sep 17 00:00:00 2001 From: Tryanks Date: Fri, 17 Jan 2025 13:22:35 +0800 Subject: [PATCH] enh: add github oauth2 provider support --- backend/open_webui/config.py | 46 ++++++++++++++++++++++++++++++++++++ src/routes/auth/+page.svelte | 16 +++++++++++++ 2 files changed, 62 insertions(+) diff --git a/backend/open_webui/config.py b/backend/open_webui/config.py index cf6a594ed..02c06487a 100644 --- a/backend/open_webui/config.py +++ b/backend/open_webui/config.py @@ -362,6 +362,30 @@ MICROSOFT_REDIRECT_URI = PersistentConfig( os.environ.get("MICROSOFT_REDIRECT_URI", ""), ) +GITHUB_CLIENT_ID = PersistentConfig( + "GITHUB_CLIENT_ID", + "oauth.github.client_id", + os.environ.get("GITHUB_CLIENT_ID", ""), +) + +GITHUB_CLIENT_SECRET = PersistentConfig( + "GITHUB_CLIENT_SECRET", + "oauth.github.client_secret", + os.environ.get("GITHUB_CLIENT_SECRET", ""), +) + +GITHUB_CLIENT_SCOPE = PersistentConfig( + "GITHUB_CLIENT_SCOPE", + "oauth.github.scope", + os.environ.get("GITHUB_CLIENT_SCOPE", "user:email"), +) + +GITHUB_CLIENT_REDIRECT_URI = PersistentConfig( + "GITHUB_CLIENT_REDIRECT_URI", + "oauth.github.redirect_uri", + os.environ.get("GITHUB_CLIENT_REDIRECT_URI", ""), +) + OAUTH_CLIENT_ID = PersistentConfig( "OAUTH_CLIENT_ID", "oauth.oidc.client_id", @@ -506,6 +530,27 @@ def load_oauth_providers(): "register": microsoft_oauth_register, } + if GITHUB_CLIENT_ID.value and GITHUB_CLIENT_SECRET.value: + def github_oauth_register(client): + client.register( + name="github", + client_id=GITHUB_CLIENT_ID.value, + client_secret=GITHUB_CLIENT_SECRET.value, + access_token_url="https://github.com/login/oauth/access_token", + authorize_url="https://github.com/login/oauth/authorize", + api_base_url="https://api.github.com", + userinfo_endpoint="https://api.github.com/user", + client_kwargs={ + "scope": GITHUB_CLIENT_SCOPE.value + }, + redirect_uri=GITHUB_CLIENT_REDIRECT_URI.value, + ) + OAUTH_PROVIDERS["github"] = { + "redirect_uri": GITHUB_CLIENT_REDIRECT_URI.value, + "register": github_oauth_register, + "sub_claim": "id", + } + if ( OAUTH_CLIENT_ID.value and OAUTH_CLIENT_SECRET.value @@ -524,6 +569,7 @@ def load_oauth_providers(): ) OAUTH_PROVIDERS["oidc"] = { "name": OAUTH_PROVIDER_NAME.value, + "redirect_uri": OPENID_REDIRECT_URI.value, "register": oidc_oauth_register, } diff --git a/src/routes/auth/+page.svelte b/src/routes/auth/+page.svelte index 00bbe7799..5439baa90 100644 --- a/src/routes/auth/+page.svelte +++ b/src/routes/auth/+page.svelte @@ -374,6 +374,22 @@ {$i18n.t('Continue with {{provider}}', { provider: 'Microsoft' })} {/if} + {#if $config?.oauth?.providers?.github} + + {/if} {#if $config?.oauth?.providers?.oidc}