@@ -614,22 +614,21 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
614614 eq ( ssoProvider . userId , session . user . id ) ,
615615 isNull ( ssoProvider . organizationId )
616616 )
617+ // Config columns are captured, not just the id: an update whose trust grant is
618+ // refused has to be undone, or the rejected config stays stored and goes live
619+ // the moment the domain is verified again.
617620 const [ existingOwnedProvider ] = await db
618- . select ( { id : ssoProvider . id } )
621+ . select ( {
622+ id : ssoProvider . id ,
623+ issuer : ssoProvider . issuer ,
624+ domain : ssoProvider . domain ,
625+ oidcConfig : ssoProvider . oidcConfig ,
626+ samlConfig : ssoProvider . samlConfig ,
627+ } )
619628 . from ( ssoProvider )
620629 . where ( ownerClause )
621630 . limit ( 1 )
622631
623- /**
624- * Unconditional write of Better Auth's `domainVerified` flag, which Sim
625- * mirrors from its own DNS proof. Used to withdraw trust; granting on an
626- * org-scoped provider goes through {@link grantProviderDomainTrust}, which
627- * re-tests ownership in the write itself.
628- */
629- const setProviderDomainVerified = async ( verified : boolean ) => {
630- await db . update ( ssoProvider ) . set ( { domainVerified : verified } ) . where ( ownerClause )
631- }
632-
633632 /**
634633 * Grants domain trust only while the proof is held under a row lock.
635634 *
@@ -644,7 +643,7 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
644643 */
645644 const grantProviderDomainTrust = async ( ) : Promise < boolean > => {
646645 if ( ! orgId ) {
647- await setProviderDomainVerified ( ! isHosted )
646+ await db . update ( ssoProvider ) . set ( { domainVerified : ! isHosted } ) . where ( ownerClause )
648647 return true
649648 }
650649 return db . transaction ( async ( tx ) => {
@@ -683,11 +682,21 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
683682 headers,
684683 } )
685684
686- // Nothing to roll back on update, so clear the flag: `updateSSOProvider`
687- // resets it only when the domain changes, leaving same-domain edits stale.
685+ // Restore the pre-update config and clear the flag together. Clearing alone
686+ // is not enough: re-verifying the domain now regrants trust automatically,
687+ // which would activate the very config this request reported as rejected.
688688 if ( ! ( await grantProviderDomainTrust ( ) ) ) {
689- await setProviderDomainVerified ( false )
690- logger . warn ( 'Revoked SSO domain trust: verification was removed mid-update' , {
689+ await db
690+ . update ( ssoProvider )
691+ . set ( {
692+ issuer : existingOwnedProvider . issuer ,
693+ domain : existingOwnedProvider . domain ,
694+ oidcConfig : existingOwnedProvider . oidcConfig ,
695+ samlConfig : existingOwnedProvider . samlConfig ,
696+ domainVerified : false ,
697+ } )
698+ . where ( eq ( ssoProvider . id , existingOwnedProvider . id ) )
699+ logger . warn ( 'Reverted SSO update: domain verification was removed mid-write' , {
691700 domain,
692701 orgId,
693702 providerId,
0 commit comments