3030 * accepted;
3131 * 7. a provider listed without a credential pair is no way in, and one the
3232 * boot did not declare counts only as the settings-added Google.
33+ *
34+ * [#30300] The two doors that are not a settings save, below the first
35+ * describe, each with its own composition:
36+ * 8. deleting the last identity provider (`POST /sso/delete-provider`) while
37+ * email and password is off is refused 409 `DELETE_RESTRICTED`, in the
38+ * caller's language, nothing deleted; CONTROLS: another provider left,
39+ * email and password on, an unknown provider (the vendor's 404), no
40+ * session (the vendor's 401). Real `ObjectKernel` with `ObjectQLPlugin`
41+ * over in-memory SQLite, `SettingsServicePlugin` and `AuthPlugin` with
42+ * the SSO plugin, a real sign-up and session cookie; provider rows are
43+ * written straight into `sys_sso_provider` (registering one needs an IdP);
44+ * 9. a boot that serves no way in (a redeploy without the `GOOGLE_CLIENT_*`
45+ * pair, email and password off) reports `no_sign_in_method_at_boot` at
46+ * `error` and still boots (ruling A); CONTROLS: the deploy that still
47+ * declares the provider, and the recovery `OS_AUTH_EMAIL_PASSWORD_ENABLED=true`;
48+ * 10. the judge and the report against a fault the composition cannot make:
49+ * an unreadable provider list is 503 on the delete and silence at boot.
3350 */
3451
3552import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest' ;
36- import type { PluginContext } from '@objectstack/core' ;
53+ import { ObjectKernel , type Plugin , type PluginContext } from '@objectstack/core' ;
3754import type { IHttpRequest , IHttpResponse , IHttpServer , RouteHandler } from '@objectstack/spec/contracts' ;
38- import { assertEngineFindOnePredicate , type EngineFindOneQueryInput } from '@objectstack/objectql' ;
3955import {
56+ ObjectQLPlugin ,
57+ assertEngineFindOnePredicate ,
58+ type EngineFindOneQueryInput ,
59+ type ObjectQL ,
60+ } from '@objectstack/objectql' ;
61+ import { SqlDriver } from '@objectstack/driver-sql' ;
62+ import {
63+ InMemoryCryptoProvider ,
4064 SettingsService ,
65+ SettingsServicePlugin ,
4166 SettingsValidationError ,
4267 builtinSettingsManifests ,
4368 registerSettingsRoutes ,
@@ -46,7 +71,17 @@ import {
4671import { AuthPlugin } from './auth-plugin.js' ;
4772import type { AuthManager } from './auth-manager.js' ;
4873import { SSO_PROVIDER_OBJECT } from './sso-client-secret.js' ;
49- import { SIGN_IN_FLOOR_COPY , providerCredentialIndex , providerCredentialsFrom } from './sign-in-method-floor.js' ;
74+ import {
75+ NO_SIGN_IN_METHOD_AT_BOOT ,
76+ SIGN_IN_FLOOR_COPY ,
77+ providerCredentialIndex ,
78+ providerCredentialsFrom ,
79+ reportIfNoSignInMethodAtBoot ,
80+ resolveNoSignInMethodReport ,
81+ ssoProviderDeleteRefusal ,
82+ type SignInMethodConfigView ,
83+ type SignInMethodFloorDeps ,
84+ } from './sign-in-method-floor.js' ;
5085
5186const SECRET = 'test-secret-at-least-32-chars-long' ;
5287
@@ -425,3 +460,305 @@ describe('the auth settings save keeps a way to sign in', () => {
425460 expect ( passwordOn ( ( await boot ( { settings : recovered } ) ) . manager ) ) . toBe ( true ) ;
426461 } ) ;
427462} ) ;
463+
464+ // ── [#30300] The doors that are not a settings save ─────────────────────────
465+
466+ const BASE = 'http://localhost:3000' ;
467+ const AUTH = `${ BASE } /api/v1/auth` ;
468+ const SECRET_30300 = 'test-secret-at-least-32-chars-long-30300' ;
469+ const PASSWORD = 'S3cure!Passw0rd-30300' ;
470+ const SYS = { isSystem : true } as const ;
471+
472+ const ENV_KEYS_30300 = [
473+ 'OS_AUTH_EMAIL_PASSWORD_ENABLED' ,
474+ 'OS_AUTH_GOOGLE_ENABLED' ,
475+ 'GOOGLE_CLIENT_ID' ,
476+ 'GOOGLE_CLIENT_SECRET' ,
477+ 'OS_SSO_ENABLED' ,
478+ 'OS_AUTH_SSO_ONLY' ,
479+ 'OS_TENANCY_POSTURE' ,
480+ 'OS_MULTI_ORG_ENABLED' ,
481+ 'OS_PLATFORM_OWNER_EMAIL' ,
482+ ] as const ;
483+
484+ function sqliteDriverPlugin ( ) : Plugin {
485+ return {
486+ name : 'test.driver.sqlite' ,
487+ type : 'standard' ,
488+ version : '1.0.0' ,
489+ async init ( ctx : PluginContext ) {
490+ ctx . registerService (
491+ 'driver.default' ,
492+ new SqlDriver ( { client : 'better-sqlite3' , connection : { filename : ':memory:' } , useNullAsDefault : true } ) ,
493+ ) ;
494+ } ,
495+ } ;
496+ }
497+
498+ const cookieFrom = ( response : Response ) : string =>
499+ ( response . headers . getSetCookie ?.( ) ?? [ response . headers . get ( 'set-cookie' ) ?? '' ] )
500+ . map ( ( c ) => c . split ( ';' ) [ 0 ] )
501+ . filter ( Boolean )
502+ . join ( '; ' ) ;
503+
504+ describe ( 'the last way to sign in is kept, and a boot without one is said (#30300)' , ( ) => {
505+ let kernel : ObjectKernel | undefined ;
506+ const savedEnv : Partial < Record < ( typeof ENV_KEYS_30300 ) [ number ] , string | undefined > > = { } ;
507+
508+ beforeEach ( ( ) => {
509+ for ( const key of ENV_KEYS_30300 ) {
510+ savedEnv [ key ] = process . env [ key ] ;
511+ delete process . env [ key ] ;
512+ }
513+ } ) ;
514+
515+ afterEach ( async ( ) => {
516+ try {
517+ await kernel ?. shutdown ( ) ;
518+ } catch {
519+ /* a stopped kernel */
520+ }
521+ kernel = undefined ;
522+ for ( const key of ENV_KEYS_30300 ) {
523+ if ( savedEnv [ key ] === undefined ) delete process . env [ key ] ;
524+ else process . env [ key ] = savedEnv [ key ] ;
525+ }
526+ vi . restoreAllMocks ( ) ;
527+ } ) ;
528+
529+ /**
530+ * Boot the composition. The kernel logger's `error` is spied on the
531+ * instance every plugin's `ctx.logger` is (the seeded-boot pin's way).
532+ */
533+ async function boot ( authOptions : Record < string , unknown > = { } ) {
534+ kernel = new ObjectKernel ( { logger : { level : 'silent' } } ) ;
535+ const kernelLogger = ( kernel as unknown as { logger : { error : ( message : string , meta ?: unknown ) => void } } ) . logger ;
536+ const error = vi . spyOn ( kernelLogger , 'error' ) ;
537+ await kernel . use ( sqliteDriverPlugin ( ) ) ;
538+ await kernel . use ( new ObjectQLPlugin ( ) ) ;
539+ await kernel . use ( new AuthPlugin ( { secret : SECRET_30300 , baseUrl : BASE , ...authOptions } ) ) ;
540+ await kernel . use ( new SettingsServicePlugin ( { registerRoutes : false , cryptoProvider : new InMemoryCryptoProvider ( ) } ) ) ;
541+ await kernel . bootstrap ( ) ;
542+ const ql = kernel . getService < ObjectQL > ( 'objectql' ) ;
543+ const manager = kernel . getService < AuthManager > ( 'auth' ) ;
544+ const settings = kernel . getService < SettingsService > ( 'settings' ) ;
545+ const reports = ( ) =>
546+ error . mock . calls . map ( ( c ) => String ( c [ 0 ] ) ) . filter ( ( m ) => m . includes ( NO_SIGN_IN_METHOD_AT_BOOT ) ) ;
547+ return { ql, manager, settings, reports } ;
548+ }
549+
550+ // ── Door 1 ─────────────────────────────────────────────────────────────────
551+
552+ /** A signed-in administrator who owns `providers`, on a deployment with enterprise SSO switched on. */
553+ async function adminWithProviders ( providers : string [ ] ) {
554+ const env = await boot ( { plugins : { sso : true } } ) ;
555+ const { ql, manager } = env ;
556+ const signedUp = await manager . handleRequest (
557+ new Request ( `${ AUTH } /sign-up/email` , {
558+ method : 'POST' ,
559+ headers : { 'content-type' : 'application/json' , origin : BASE } ,
560+ body : JSON . stringify ( { email : 'admin@acme.test' , password : PASSWORD , name : 'Admin' } ) ,
561+ } ) ,
562+ ) ;
563+ expect ( signedUp . status , await signedUp . clone ( ) . text ( ) ) . toBe ( 200 ) ;
564+ const cookie = cookieFrom ( signedUp ) ;
565+ const [ user ] = ( await ql . find ( 'sys_user' , { where : { email : 'admin@acme.test' } , limit : 1 , context : SYS } ) ) as any [ ] ;
566+ for ( const providerId of providers ) {
567+ await ql . insert (
568+ SSO_PROVIDER_OBJECT ,
569+ {
570+ id : `ssop_${ providerId } ` ,
571+ provider_id : providerId ,
572+ issuer : `https://${ providerId } .idp.test` ,
573+ domain : 'acme.test' ,
574+ oidc_config : JSON . stringify ( { clientId : `${ providerId } -client` , discoveryEndpoint : `https://${ providerId } .idp.test/.well-known/openid-configuration` } ) ,
575+ user_id : String ( user . id ) ,
576+ } ,
577+ { context : SYS } ,
578+ ) ;
579+ }
580+ const providerIds = async ( ) =>
581+ ( ( await ql . find ( SSO_PROVIDER_OBJECT , { limit : 10 , context : SYS } ) ) as any [ ] ) . map ( ( r ) => r . provider_id ) . sort ( ) ;
582+ const remove = ( providerId : string , opts : { cookie ?: string ; language ?: string } = { } ) =>
583+ manager . handleRequest (
584+ new Request ( `${ AUTH } /sso/delete-provider` , {
585+ method : 'POST' ,
586+ headers : {
587+ 'content-type' : 'application/json' ,
588+ origin : BASE ,
589+ ...( opts . cookie === '' ? { } : { cookie : opts . cookie ?? cookie } ) ,
590+ ...( opts . language ? { 'accept-language' : opts . language } : { } ) ,
591+ } ,
592+ body : JSON . stringify ( { providerId } ) ,
593+ } ) ,
594+ ) ;
595+ /** Email and password off through the console, as the operator did; the floor admits it while a provider row exists. */
596+ const passwordOff = async ( ) => {
597+ await env . settings . setMany ( 'auth' , { email_password_enabled : false } ) ;
598+ await vi . waitFor ( ( ) => expect ( manager . getPublicConfig ( ) . emailPassword . enabled ) . toBe ( false ) ) ;
599+ } ;
600+ return { ...env , cookie, providerIds, remove, passwordOff } ;
601+ }
602+
603+ it ( 'refuses deleting the last identity provider while email and password is off: 409, localized, nothing deleted' , async ( ) => {
604+ const { remove, providerIds, passwordOff } = await adminWithProviders ( [ 'okta' ] ) ;
605+ await passwordOff ( ) ;
606+
607+ const res = await remove ( 'okta' , { language : 'zh-CN,zh;q=0.9' } ) ;
608+ const body = await res . json ( ) ;
609+ expect ( res . status , JSON . stringify ( body ) ) . toBe ( 409 ) ;
610+ expect ( body ) . toMatchObject ( {
611+ code : 'DELETE_RESTRICTED' ,
612+ message : SIGN_IN_FLOOR_COPY [ 'zh-CN' ] . enterpriseSso ,
613+ lastSignInMethod : 'enterprise_sso' ,
614+ } ) ;
615+ expect ( await providerIds ( ) ) . toEqual ( [ 'okta' ] ) ;
616+
617+ // Same refusal in the default language.
618+ const en = await remove ( 'okta' ) ;
619+ expect ( en . status ) . toBe ( 409 ) ;
620+ expect ( ( await en . json ( ) ) . message ) . toBe ( SIGN_IN_FLOOR_COPY . en . enterpriseSso ) ;
621+ expect ( await providerIds ( ) ) . toEqual ( [ 'okta' ] ) ;
622+ } , 30_000 ) ;
623+
624+ it ( 'control: while another provider remains the delete goes through; the one left is then the last and is refused' , async ( ) => {
625+ const { remove, providerIds, passwordOff } = await adminWithProviders ( [ 'okta' , 'entra' ] ) ;
626+ await passwordOff ( ) ;
627+
628+ const first = await remove ( 'okta' ) ;
629+ expect ( first . status , await first . clone ( ) . text ( ) ) . toBe ( 200 ) ;
630+ expect ( await providerIds ( ) ) . toEqual ( [ 'entra' ] ) ;
631+
632+ const last = await remove ( 'entra' ) ;
633+ expect ( last . status , await last . clone ( ) . text ( ) ) . toBe ( 409 ) ;
634+ expect ( await providerIds ( ) ) . toEqual ( [ 'entra' ] ) ;
635+ } , 30_000 ) ;
636+
637+ it ( 'control: while email and password is on, deleting the last provider goes through' , async ( ) => {
638+ const { remove, providerIds } = await adminWithProviders ( [ 'okta' ] ) ;
639+
640+ const res = await remove ( 'okta' ) ;
641+ expect ( res . status , await res . clone ( ) . text ( ) ) . toBe ( 200 ) ;
642+ expect ( await providerIds ( ) ) . toEqual ( [ ] ) ;
643+ } , 30_000 ) ;
644+
645+ it ( 'control: an unknown provider is still the vendor 404, and no session is still the vendor 401' , async ( ) => {
646+ const { remove, providerIds, passwordOff } = await adminWithProviders ( [ 'okta' ] ) ;
647+ await passwordOff ( ) ;
648+
649+ expect ( ( await remove ( 'nobody' ) ) . status ) . toBe ( 404 ) ;
650+ expect ( ( await remove ( 'okta' , { cookie : '' } ) ) . status ) . toBe ( 401 ) ;
651+ expect ( await providerIds ( ) ) . toEqual ( [ 'okta' ] ) ;
652+ } , 30_000 ) ;
653+
654+ // ── Door 2 ─────────────────────────────────────────────────────────────────
655+
656+ it ( 'a redeploy that drops the last provider while email and password is off is said at error, and still boots' , async ( ) => {
657+ const { manager, reports } = await boot ( { emailAndPassword : { enabled : false } } ) ;
658+
659+ // The boot completed (bootstrap resolved, the manager serves) and serves no way in.
660+ expect ( manager . getPublicConfig ( ) . emailPassword . enabled ) . toBe ( false ) ;
661+ expect ( manager . getPublicConfig ( ) . socialProviders ) . toEqual ( [ ] ) ;
662+ expect ( reports ( ) ) . toHaveLength ( 1 ) ;
663+ expect ( reports ( ) [ 0 ] ) . toContain ( 'nobody can sign in' ) ;
664+ expect ( reports ( ) [ 0 ] ) . toContain ( 'OS_AUTH_EMAIL_PASSWORD_ENABLED=true' ) ;
665+ } , 30_000 ) ;
666+
667+ it ( 'the same with enterprise SSO switched on but no identity provider row' , async ( ) => {
668+ const { reports } = await boot ( { emailAndPassword : { enabled : false } , plugins : { sso : true } } ) ;
669+ expect ( reports ( ) ) . toHaveLength ( 1 ) ;
670+ } , 30_000 ) ;
671+
672+ it ( 'control: the deploy that still declares the provider says nothing' , async ( ) => {
673+ process . env . GOOGLE_CLIENT_ID = 'google-env-client-id' ;
674+ process . env . GOOGLE_CLIENT_SECRET = 'google-env-client-secret' ;
675+ const { manager, reports } = await boot ( { emailAndPassword : { enabled : false } } ) ;
676+
677+ expect ( manager . getPublicConfig ( ) . socialProviders . map ( ( p : { id : string } ) => p . id ) ) . toContain ( 'google' ) ;
678+ expect ( reports ( ) ) . toEqual ( [ ] ) ;
679+ } , 30_000 ) ;
680+
681+ it ( 'recovery: OS_AUTH_EMAIL_PASSWORD_ENABLED=true turns email and password back on over the stack config, and nothing is said' , async ( ) => {
682+ process . env . OS_AUTH_EMAIL_PASSWORD_ENABLED = 'true' ;
683+ const { manager, reports } = await boot ( { emailAndPassword : { enabled : false } } ) ;
684+
685+ expect ( manager . getPublicConfig ( ) . emailPassword . enabled ) . toBe ( true ) ;
686+ expect ( reports ( ) ) . toEqual ( [ ] ) ;
687+ } , 30_000 ) ;
688+ } ) ;
689+
690+ // ── The judge and the report, against a fault the composition cannot make ───
691+
692+ describe ( 'the floor judges a provider delete and a boot by what it can read (#30300)' , ( ) => {
693+ const OFF : SignInMethodConfigView = { emailPassword : { enabled : false } , socialProviders : [ ] , features : { sso : true } } ;
694+
695+ const deps = (
696+ over : { config ?: SignInMethodConfigView ; rows ?: unknown [ ] | 'throws' ; hostSignInHandoff ?: boolean } = { } ,
697+ ) : SignInMethodFloorDeps => ( {
698+ publicConfig : ( ) => over . config ?? OFF ,
699+ engine : ( ) => ( {
700+ find : async ( _object : string , query ?: { limit ?: number } ) => {
701+ if ( over . rows === 'throws' ) throw new Error ( 'provider table unreadable' ) ;
702+ const rows = ( over . rows ?? [ ] ) as any [ ] ;
703+ return query && 'limit' in query && typeof query . limit === 'number' ? rows . slice ( 0 , query . limit ) : rows ;
704+ } ,
705+ } ) as any ,
706+ hostSignInHandoff : over . hostSignInHandoff ?? false ,
707+ stackConfiguredGoogle : ( ) => false ,
708+ providerHasCredentials : ( ) => true ,
709+ env : ( ) => ( { } ) ,
710+ } ) ;
711+
712+ it ( 'the last provider: 409 DELETE_RESTRICTED; an unreadable provider list: 503 SERVICE_UNAVAILABLE, never a pass' , async ( ) => {
713+ await expect ( ssoProviderDeleteRefusal ( deps ( { rows : [ { provider_id : 'okta' } ] } ) , 'okta' , 'ja-JP' ) ) . resolves . toEqual ( {
714+ status : 'CONFLICT' ,
715+ code : 'DELETE_RESTRICTED' ,
716+ message : SIGN_IN_FLOOR_COPY [ 'ja-JP' ] . enterpriseSso ,
717+ lastSignInMethod : 'enterprise_sso' ,
718+ } ) ;
719+ await expect ( ssoProviderDeleteRefusal ( deps ( { rows : 'throws' } ) , 'okta' , 'es-ES' ) ) . resolves . toEqual ( {
720+ status : 'SERVICE_UNAVAILABLE' ,
721+ code : 'SERVICE_UNAVAILABLE' ,
722+ message : SIGN_IN_FLOOR_COPY [ 'es-ES' ] . enterpriseSsoUnverified ,
723+ } ) ;
724+ } ) ;
725+
726+ it . each ( [
727+ { name : 'email and password on' , d : ( ) => deps ( { config : { ...OFF , emailPassword : { enabled : true } } , rows : [ { provider_id : 'okta' } ] } ) } ,
728+ { name : 'enterprise SSO not switched on' , d : ( ) => deps ( { config : { ...OFF , features : { sso : false } } , rows : [ { provider_id : 'okta' } ] } ) } ,
729+ { name : 'another provider row' , d : ( ) => deps ( { rows : [ { provider_id : 'okta' } , { provider_id : 'entra' } ] } ) } ,
730+ { name : 'no row at all (the vendor answers 404)' , d : ( ) => deps ( { rows : [ ] } ) } ,
731+ { name : 'a social provider with credentials' , d : ( ) => deps ( { config : { ...OFF , socialProviders : [ { id : 'github' , type : 'social' } ] } , rows : [ { provider_id : 'okta' } ] } ) } ,
732+ { name : 'phone-number sign-in' , d : ( ) => deps ( { config : { ...OFF , features : { sso : true , phoneNumber : true } } , rows : [ { provider_id : 'okta' } ] } ) } ,
733+ { name : 'a host sign-in handoff' , d : ( ) => deps ( { hostSignInHandoff : true , rows : [ { provider_id : 'okta' } ] } ) } ,
734+ ] ) ( 'passes the delete with $name' , async ( { d } ) => {
735+ await expect ( ssoProviderDeleteRefusal ( d ( ) , 'okta' ) ) . resolves . toBeUndefined ( ) ;
736+ } ) ;
737+
738+ it ( 'the boot report is a positive claim: an unreadable provider table says nothing, an empty one says it' , async ( ) => {
739+ expect ( await resolveNoSignInMethodReport ( deps ( { rows : 'throws' } ) ) ) . toBeNull ( ) ;
740+ expect ( await resolveNoSignInMethodReport ( deps ( { rows : [ { provider_id : 'okta' } ] } ) ) ) . toBeNull ( ) ;
741+ expect ( await resolveNoSignInMethodReport ( deps ( { rows : [ ] } ) ) ) . toContain ( NO_SIGN_IN_METHOD_AT_BOOT ) ;
742+ // Enterprise SSO not switched on: its table is not read, and nothing else is in force.
743+ expect ( await resolveNoSignInMethodReport ( deps ( { config : { ...OFF , features : { sso : false } } , rows : 'throws' } ) ) ) . toContain (
744+ NO_SIGN_IN_METHOD_AT_BOOT ,
745+ ) ;
746+ expect ( await resolveNoSignInMethodReport ( deps ( { hostSignInHandoff : true } ) ) ) . toBeNull ( ) ;
747+ } ) ;
748+
749+ it ( 'says it once at error (warn on a sink without error), and never throws' , async ( ) => {
750+ const error = vi . fn ( ) ;
751+ const warn = vi . fn ( ) ;
752+ const said = await reportIfNoSignInMethodAtBoot ( deps ( ) , { error, warn } as any ) ;
753+ expect ( error ) . toHaveBeenCalledTimes ( 1 ) ;
754+ expect ( error ) . toHaveBeenCalledWith ( said ) ;
755+ expect ( warn ) . not . toHaveBeenCalled ( ) ;
756+
757+ const warnOnly = vi . fn ( ) ;
758+ await reportIfNoSignInMethodAtBoot ( deps ( ) , { warn : warnOnly } as any ) ;
759+ expect ( warnOnly ) . toHaveBeenCalledTimes ( 1 ) ;
760+
761+ const throwing = { error : ( ) => { throw new Error ( 'sink down' ) ; } , warn : vi . fn ( ) } ;
762+ await expect ( reportIfNoSignInMethodAtBoot ( deps ( ) , throwing as any ) ) . resolves . toContain ( NO_SIGN_IN_METHOD_AT_BOOT ) ;
763+ } ) ;
764+ } ) ;
0 commit comments