@@ -25,6 +25,8 @@ type UpsertAuthProfileCall = {
2525 agentDir ?: string ;
2626 credential ?: {
2727 provider ?: string ;
28+ token ?: string ;
29+ tokenRef ?: unknown ;
2830 type ?: string ;
2931 } ;
3032 profileId ?: string ;
@@ -66,6 +68,18 @@ const mocks = vi.hoisted(() => ({
6668 callGateway : vi . fn ( ) ,
6769 resolvePluginSetupProviderCore : vi . fn ( ) ,
6870 resolvePluginSetupRegistry : vi . fn ( ) ,
71+ readSecretStoreValue : vi . fn ( ( ) => ( {
72+ ok : false as const ,
73+ error : { code : "SECRET_STORE_NOT_FOUND" , message : "missing" } ,
74+ } ) ) ,
75+ writeSecretStoreEntry : vi . fn ( ) ,
76+ deleteSecretStoreEntry : vi . fn ( ) ,
77+ } ) ) ;
78+
79+ vi . mock ( "../../secrets/store/secret-store.js" , ( ) => ( {
80+ readSecretStoreValue : mocks . readSecretStoreValue ,
81+ writeSecretStoreEntry : mocks . writeSecretStoreEntry ,
82+ deleteSecretStoreEntry : mocks . deleteSecretStoreEntry ,
6983} ) ) ;
7084
7185vi . mock ( "../../agents/auth-profiles/profiles.js" , ( ) => ( {
@@ -484,6 +498,85 @@ describe("modelsAuthLoginCommand", () => {
484498 } ) ;
485499 } ) ;
486500
501+ it ( "persists a provider-minted Copilot token through the protected store" , async ( ) => {
502+ const runtime = createRuntime ( ) ;
503+ runProviderAuth . mockResolvedValueOnce ( {
504+ profiles : [
505+ {
506+ profileId : "github-copilot:github" ,
507+ credential : {
508+ type : "token" ,
509+ provider : "github-copilot" ,
510+ token : "synthetic-device-token" ,
511+ } ,
512+ secretStorage : {
513+ kind : "store" ,
514+ namePrefix : "GITHUB_COPILOT_TOKEN" ,
515+ } ,
516+ } ,
517+ ] ,
518+ } ) ;
519+ mocks . resolvePluginProvidersCore . mockReturnValue ( [
520+ createProvider ( {
521+ id : "github-copilot" ,
522+ label : "GitHub Copilot" ,
523+ run : runProviderAuth as ProviderPlugin [ "auth" ] [ number ] [ "run" ] ,
524+ } ) ,
525+ ] ) ;
526+
527+ await modelsAuthLoginCommand ( { provider : "github-copilot" } , runtime ) ;
528+
529+ expect ( mocks . writeSecretStoreEntry ) . toHaveBeenCalledWith (
530+ expect . objectContaining ( { value : "synthetic-device-token" } ) ,
531+ ) ;
532+ const upsertCall = readMockCallArg (
533+ mocks . upsertAuthProfileAfterLoginWithLock ,
534+ ) as UpsertAuthProfileCall ;
535+ expect ( upsertCall . credential ) . not . toHaveProperty ( "token" ) ;
536+ expect ( upsertCall . credential ?. tokenRef ) . toEqual ( {
537+ source : "store" ,
538+ provider : "default" ,
539+ id : expect . stringMatching ( / ^ G I T H U B _ C O P I L O T _ T O K E N _ [ A - F 0 - 9 ] { 24 } $ / ) ,
540+ } ) ;
541+ } ) ;
542+
543+ it ( "keeps the prior auth profile when protected storage is unavailable" , async ( ) => {
544+ const runtime = createRuntime ( ) ;
545+ runProviderAuth . mockResolvedValueOnce ( {
546+ profiles : [
547+ {
548+ profileId : "github-copilot:github" ,
549+ credential : {
550+ type : "token" ,
551+ provider : "github-copilot" ,
552+ token : "synthetic-device-token" ,
553+ } ,
554+ secretStorage : {
555+ kind : "store" ,
556+ namePrefix : "GITHUB_COPILOT_TOKEN" ,
557+ } ,
558+ } ,
559+ ] ,
560+ } ) ;
561+ mocks . resolvePluginProvidersCore . mockReturnValue ( [
562+ createProvider ( {
563+ id : "github-copilot" ,
564+ label : "GitHub Copilot" ,
565+ run : runProviderAuth as ProviderPlugin [ "auth" ] [ number ] [ "run" ] ,
566+ } ) ,
567+ ] ) ;
568+ mocks . writeSecretStoreEntry . mockImplementationOnce ( ( ) => {
569+ throw new Error ( "read-only database" ) ;
570+ } ) ;
571+
572+ await expect ( modelsAuthLoginCommand ( { provider : "github-copilot" } , runtime ) ) . rejects . toThrow (
573+ "Could not write the protected secret store" ,
574+ ) ;
575+
576+ expect ( mocks . upsertAuthProfileAfterLoginWithLock ) . not . toHaveBeenCalled ( ) ;
577+ expect ( mocks . promoteAuthProfileInOrder ) . not . toHaveBeenCalled ( ) ;
578+ } ) ;
579+
487580 it ( "keeps login successful when the running gateway cannot refresh auth state" , async ( ) => {
488581 const runtime = createRuntime ( ) ;
489582 mocks . callGateway . mockRejectedValueOnce ( new Error ( "gateway unavailable" ) ) ;
0 commit comments