feat: OAuth middleware with module loaders for identity/secrets/auth config#1220
Closed
pyramation wants to merge 1 commit into
Closed
feat: OAuth middleware with module loaders for identity/secrets/auth config#1220pyramation wants to merge 1 commit into
pyramation wants to merge 1 commit into
Conversation
…config Replace manual SQL queries and hardcoded schema assumptions in OAuth middleware with module loaders from express-context: New loaders: - encryptedSecretsLoader: resolves encrypted_secrets schema from metaschema_modules_public.encrypted_secrets_module - userAuthLoader: resolves user_auth_module for sign_in/sign_up function names and schema (no longer assumes privateSchema) - identityProvidersLoader: resolves identity_providers_module for provider config table location OAuth middleware changes: - Uses req.constructive.useModule() for all schema lookups - Uses req.constructive.withPgClient() for properly scoped RLS transactions (replaces manual set_config calls) - Removes express-rate-limit (DB already handles rate limiting) - Uses getNodeEnv() instead of process.env.NODE_ENV - Extracts email_verified from raw provider profile data - Passes loaders registry to createContextMiddleware in server.ts Addresses review comments from PR #1141.
Contributor
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
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.
Summary
Refactors the OAuth/SSO middleware from PR #1141 to use the module loader architecture from
express-context, addressing the review issues Dan identified.What changed
New module loaders (
packages/express-context/src/loaders/):encryptedSecretsLoader— resolvesencrypted_secrets_modulefrommetaschema_modules_publicto find the correct schema for decrypting secrets (replaces hardcoded metaschema JOIN)userAuthLoader— resolvesuser_auth_modulefor sign_in/sign_up function names and the schema they live in (no longer assumesprivateSchema)identityProvidersLoader— resolvesidentity_providers_modulefor provider config table location (schema + table name, not hardcoded)All three are registered in
createDefaultRegistry()and added toBuiltinModuleMapfor typeduseModule()access.OAuth middleware (
graphql/server/src/middleware/oauth.ts):req.constructive.useModule()to resolve all per-database config (identity providers, encrypted secrets, user auth, auth settings) — no manual metaschema queriesreq.constructive.withPgClient()for RLS-scoped transactions with automatic pgSettings — replaces manualset_config('jwt.claims.*', ..., false)callsexpress-rate-limit— DB already handles rate limiting viaauth_rate_limits/app_settings_rate_limitgetNodeEnv()from@pgpmjs/envinstead ofprocess.env.NODE_ENVsign_in_identity/sign_up_identityare called in theuserAuth.schemaName— not assumed to be in the RLSprivateSchemaemail_verifiedfrom the raw provider profile data (OAuthProfile doesn't expose it directly)Server wiring (
graphql/server/src/server.ts):createDefaultRegistry()asloaderstocreateContextMiddleware()— enablesuseModule()onreq.constructive/authIssues addressed from #1141 review
getEncryptedSecretsSchemauses fragile metaschema JOIN withprivateSchemaencryptedSecretsLoaderqueryingencrypted_secrets_modulebydatabase_idprocess.env.NODE_ENVused directlygetNodeEnv()from@pgpmjs/envexpress-rate-limitis per-process, redundant with DB rate limitingset_config('jwt.claims.*')withfalse(session-level)withPgClient()which applies pgSettings viaSET LOCAL(transaction-scoped)sign_in_identity/sign_up_identitylive inprivateSchemauserAuthLoaderwhich finds the actual schema fromuser_auth_moduleOAUTH_STATE_MAX_AGE,requireVerifiedEmail,errorRedirectPathemail_verifiedextracted from raw profileReview & Testing Checklist for Human
This is a medium-risk PR since OAuth is a security-sensitive flow and the module loaders are querying real module tables that must exist in the tenant database.
encryptedSecretsLoader,userAuthLoader, andidentityProvidersLoadermatches the actualmetaschema_modules_publictable schemas in the deployed databases (column names, JOINs, WHERE clauses)identity_providers_modulehas aprivate_schema_idcolumn that JOINs tometaschema_public.schema— the loader assumes thiswithPgClient()correctly scopes thejwt.claims.user_agentandjwt.claims.originsettings to the transaction (thetrueflag inset_configmeans transaction-local)createDefaultRegistry()is only called once per server instance (it's called in the constructor, so it should be fine)Notes
OAuthProfiletype from@constructive-io/oauthdoesn't includeemailVerified— we extract it fromprofile.rawwhich contains the original provider response. A follow-up could addemailVerifiedto theOAuthProfiletype.session_credentialstable used ingenerateCrossOriginTokenis accessed viauserAuth.schemaName— this assumes it lives in the same schema assign_in_identity. Worth verifying.Link to Devin session: https://app.devin.ai/sessions/0796902ebeba4f6ea1900d84deab2fc5
Requested by: @pyramation