Replace Google OAuth with generic OAuth/OIDC provider support#1283
Open
tomaszzmuda wants to merge 2 commits intokhoj-ai:masterfrom
Open
Replace Google OAuth with generic OAuth/OIDC provider support#1283tomaszzmuda wants to merge 2 commits intokhoj-ai:masterfrom
tomaszzmuda wants to merge 2 commits intokhoj-ai:masterfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Khoj authentication is hardcoded to Google Sign-In only. Self-hosted users who use other identity providers (Authentik, Auth0, Keycloak, etc.) cannot use OAuth at all and are forced to use magic links or anonymous mode.
Solution
Replace the Google-specific OAuth implementation with a generic OAuth 2.0 / OpenID Connect system configurable via environment variables. Any OIDC-compliant provider works out of the box.
Migration
0102_remove_googleuserdrops theGoogleUsertable. Once applied, you cannot roll back to a version that usesGoogleUser.Migration
0101copies all existing Google auth data into the newOAuthAccountmodel before the table is removed, so no user data is lost during upgrade.Back up your database before running migrations.
Configuration
Required:
GENERIC_OAUTH_ENABLEDTrueto enableGENERIC_OAUTH_CLIENT_IDGENERIC_OAUTH_CLIENT_SECRETEndpoints (one of the two options is required):
GENERIC_OAUTH_ISSUERGENERIC_OAUTH_AUTHORIZATION_ENDPOINTGENERIC_OAUTH_TOKEN_ENDPOINTWithout at least one endpoint option, OAuth login will not work.
Optional:
GENERIC_OAUTH_PROVIDER_NAMEOAuthGENERIC_OAUTH_BUTTON_LABELContinue with {name}GENERIC_OAUTH_SCOPEopenid profile emailGENERIC_OAUTH_USERINFO_ENDPOINTGENERIC_OAUTH_JWKS_URISet redirect URI in your provider to:
https://your-khoj-domain/auth/oauth/oauthChanges
Backend
OAuthAccountmodel (replacesGoogleUser) with provider-agnostic fields andraw_infoJSON for full userinfo storageoauth_config.py— readsGENERIC_OAUTH_*env vars, supports OIDC discovery and manual endpoint modesauth.py— unified/auth/login/{provider}and/auth/oauth/{provider}endpoints, uses Authlib's built-inuserinfo()instead of manual HTTP callsget_or_create_user_oauth,get_user_by_oauth,create_user_by_oauthOAuthAccountin Django adminupdate_or_create→ asyncaupdate_or_createin OAuth account creationhttpxdependency from auth moduleFrontend
GoogleSignIn.tsxcomponent andloginPrompt.module.cssloginPrompt.tsxnow dynamically renders login buttons for all configured providersGENERIC_OAUTH_BUTTON_LABELfor custom button textaccounts.google.comentries from CSP headersDocumentation
authentication.mdxwith generic OAuth/OIDC setup guide, examples for Google and AuthentikTests
tests/test_oauth.py— 28 tests covering config helpers, database adapters, userinfo extraction, and user name/photo fallbacksBackward Compatibility
GENERIC_OAUTH_ISSUER=https://accounts.google.comGOOGLE_CLIENT_ID/GOOGLE_CLIENT_SECRETenv vars are no longer used — must switch toGENERIC_OAUTH_*varsGoogleUsertable is droppedHow it works:
I set it as draft for now cause I haven't tested all paths - it works with Authentik but I need to check Google and if migration from Google works properly.
---EDIT---
I've tested Google and Authentik integration and migration from Google to Authentik and everything is good.
This is ready to be merged.