@@ -89,6 +89,8 @@ import { inviteForAudienceGate } from './audience-gate-test-support';
8989import { bindTestSecurityCatalog } from './__tests__/security-catalog.testkit' ;
9090import { ADMIN_FULL_ACCESS_CAPABILITIES } from '@objectstack/spec' ;
9191import { relaxTenantColumns } from './pre-18-tenant-column.testkit.js' ;
92+ import { withSystemReadContext } from './objectql-adapter.js' ;
93+ import { readSsoProviderSignInOptions } from './sso-sign-in-options.js' ;
9294
9395const BASE = 'http://localhost:3000' ;
9496const AUTH = `${ BASE } /api/v1/auth` ;
@@ -458,3 +460,96 @@ describe('[#10009] the two doors onto SSO registration now answer the same org o
458460 expect ( directBody ?. code ?? directBody ?. error ?. code ) . toBe ( 'SSO_REGISTER_FORBIDDEN' ) ;
459461 } ) ;
460462} ) ;
463+
464+ // ---------------------------------------------------------------------------
465+ // The provider's sign-in toggles are written through the vendor's own
466+ // /sso/update-provider (`sso-sign-in-options.ts`), on a real SQL store.
467+ // ---------------------------------------------------------------------------
468+
469+ describe ( 'the per-provider sign-in toggles are written by the provider writer, default off' , ( ) => {
470+ async function providerOwnedBy ( engine : ObjectQL , manager : AuthManager , userId : string ) : Promise < void > {
471+ const auth : any = await manager . getAuthInstance ( ) ;
472+ const ctx = await auth . $context ;
473+ await ctx . adapter . create ( {
474+ model : 'ssoProvider' ,
475+ data : {
476+ providerId : 'acme-okta' ,
477+ issuer : 'https://idp.acme.example' ,
478+ domain : 'acme.example' ,
479+ userId,
480+ oidcConfig : JSON . stringify ( { issuer : 'https://idp.acme.example' , clientId : 'cid' , pkce : true } ) ,
481+ } ,
482+ } ) ;
483+ }
484+
485+ const postUpdate = ( send : ( r : Request ) => Promise < Response > , cookie : string , body : Record < string , unknown > ) =>
486+ send (
487+ new Request ( `${ AUTH } /sso/update-provider` , {
488+ method : 'POST' ,
489+ headers : { 'content-type' : 'application/json' , origin : BASE , cookie } ,
490+ body : JSON . stringify ( { providerId : 'acme-okta' , ...body } ) ,
491+ } ) ,
492+ ) ;
493+
494+ it ( 'a new provider stores both OFF; update-provider turns each on and off; the store reads what was written' , async ( ) => {
495+ const engine = await bootEngine ( ) ;
496+ const manager = makeManager ( engine ) ;
497+ const send = ( r : Request ) => manager . handleRequest ( r ) ;
498+ const email = 'platformadmin@example.com' ;
499+ const cookie = await signUp ( send , email , engine ) ;
500+ const userId = await userIdOf ( engine , email ) ;
501+ await grantPlatformAdmin ( engine , userId ) ;
502+ await providerOwnedBy ( engine , manager , userId ) ;
503+
504+ const reader = withSystemReadContext ( engine as never ) as never ;
505+ expect ( await readSsoProviderSignInOptions ( reader , 'acme-okta' ) ) . toEqual ( { allowIdpInitiated : false , allowJit : false } ) ;
506+
507+ const on = await postUpdate ( send , cookie , { allowIdpInitiated : true , allowJit : true } ) ;
508+ expect ( on . status , await on . clone ( ) . text ( ) ) . toBe ( 200 ) ;
509+ expect ( await readSsoProviderSignInOptions ( reader , 'acme-okta' ) ) . toEqual ( { allowIdpInitiated : true , allowJit : true } ) ;
510+
511+ const off = await postUpdate ( send , cookie , { allowJit : false } ) ;
512+ expect ( off . status , await off . clone ( ) . text ( ) ) . toBe ( 200 ) ;
513+ expect ( await readSsoProviderSignInOptions ( reader , 'acme-okta' ) ) . toEqual ( { allowIdpInitiated : true , allowJit : false } ) ;
514+ } ) ;
515+
516+ it ( 'a value that is not a boolean is refused 400, and nothing is written' , async ( ) => {
517+ const engine = await bootEngine ( ) ;
518+ const manager = makeManager ( engine ) ;
519+ const send = ( r : Request ) => manager . handleRequest ( r ) ;
520+ const email = 'platformadmin@example.com' ;
521+ const cookie = await signUp ( send , email , engine ) ;
522+ const userId = await userIdOf ( engine , email ) ;
523+ await grantPlatformAdmin ( engine , userId ) ;
524+ await providerOwnedBy ( engine , manager , userId ) ;
525+
526+ const res = await postUpdate ( send , cookie , { allowJit : 'yes' } ) ;
527+ const body = ( await res . clone ( ) . json ( ) ) as Record < string , any > ;
528+ expect ( res . status , await res . clone ( ) . text ( ) ) . toBe ( 400 ) ;
529+ expect ( body ?. code ) . toBe ( 'VALIDATION_ERROR' ) ;
530+ expect ( await readSsoProviderSignInOptions ( withSystemReadContext ( engine as never ) as never , 'acme-okta' ) ) . toEqual ( {
531+ allowIdpInitiated : false ,
532+ allowJit : false ,
533+ } ) ;
534+ } ) ;
535+
536+ it ( 'a member who does not write the provider cannot flip it (the vendor provider-access check)' , async ( ) => {
537+ const engine = await bootEngine ( ) ;
538+ const manager = makeManager ( engine ) ;
539+ const send = ( r : Request ) => manager . handleRequest ( r ) ;
540+ const adminCookie = await signUp ( send , 'platformadmin@example.com' , engine ) ;
541+ const adminId = await userIdOf ( engine , 'platformadmin@example.com' ) ;
542+ await grantPlatformAdmin ( engine , adminId ) ;
543+ await providerOwnedBy ( engine , manager , adminId ) ;
544+ expect ( adminCookie ) . toBeTruthy ( ) ;
545+
546+ const memberCookie = await signUp ( send , 'member@example.com' , engine ) ;
547+ const res = await postUpdate ( send , memberCookie , { allowJit : true } ) ;
548+ // The vendor's own refusal (403, no `code` member on @better-auth/sso 1.7.12).
549+ expect ( res . status , await res . clone ( ) . text ( ) ) . toBe ( 403 ) ;
550+ expect ( await readSsoProviderSignInOptions ( withSystemReadContext ( engine as never ) as never , 'acme-okta' ) ) . toEqual ( {
551+ allowIdpInitiated : false ,
552+ allowJit : false ,
553+ } ) ;
554+ } ) ;
555+ } ) ;
0 commit comments