11package credentials
22
33import (
4+ "context"
45 "fmt"
56 "path/filepath"
67 "regexp"
@@ -10,32 +11,38 @@ import (
1011 "github.com/gptscript-ai/gptscript/pkg/config"
1112)
1213
14+ type CredentialBuilder interface {
15+ EnsureCredentialHelpers (ctx context.Context ) error
16+ }
17+
1318type CredentialStore interface {
14- Get (toolName string ) (* Credential , bool , error )
15- Add (cred Credential ) error
16- Remove (toolName string ) error
17- List () ([]Credential , error )
19+ Get (ctx context. Context , toolName string ) (* Credential , bool , error )
20+ Add (ctx context. Context , cred Credential ) error
21+ Remove (ctx context. Context , toolName string ) error
22+ List (ctx context. Context ) ([]Credential , error )
1823}
1924
2025type Store struct {
2126 credCtx string
27+ credBuilder CredentialBuilder
2228 credHelperDirs CredentialHelperDirs
2329 cfg * config.CLIConfig
2430}
2531
26- func NewStore (cfg * config.CLIConfig , credCtx , cacheDir string ) (CredentialStore , error ) {
32+ func NewStore (cfg * config.CLIConfig , credentialBuilder CredentialBuilder , credCtx , cacheDir string ) (CredentialStore , error ) {
2733 if err := validateCredentialCtx (credCtx ); err != nil {
2834 return nil , err
2935 }
3036 return Store {
3137 credCtx : credCtx ,
38+ credBuilder : credentialBuilder ,
3239 credHelperDirs : GetCredentialHelperDirs (cacheDir ),
3340 cfg : cfg ,
3441 }, nil
3542}
3643
37- func (s Store ) Get (toolName string ) (* Credential , bool , error ) {
38- store , err := s .getStore ()
44+ func (s Store ) Get (ctx context. Context , toolName string ) (* Credential , bool , error ) {
45+ store , err := s .getStore (ctx )
3946 if err != nil {
4047 return nil , false , err
4148 }
@@ -57,9 +64,9 @@ func (s Store) Get(toolName string) (*Credential, bool, error) {
5764 return & cred , true , nil
5865}
5966
60- func (s Store ) Add (cred Credential ) error {
67+ func (s Store ) Add (ctx context. Context , cred Credential ) error {
6168 cred .Context = s .credCtx
62- store , err := s .getStore ()
69+ store , err := s .getStore (ctx )
6370 if err != nil {
6471 return err
6572 }
@@ -70,16 +77,16 @@ func (s Store) Add(cred Credential) error {
7077 return store .Store (auth )
7178}
7279
73- func (s Store ) Remove (toolName string ) error {
74- store , err := s .getStore ()
80+ func (s Store ) Remove (ctx context. Context , toolName string ) error {
81+ store , err := s .getStore (ctx )
7582 if err != nil {
7683 return err
7784 }
7885 return store .Erase (toolNameWithCtx (toolName , s .credCtx ))
7986}
8087
81- func (s Store ) List () ([]Credential , error ) {
82- store , err := s .getStore ()
88+ func (s Store ) List (ctx context. Context ) ([]Credential , error ) {
89+ store , err := s .getStore (ctx )
8390 if err != nil {
8491 return nil , err
8592 }
@@ -106,17 +113,21 @@ func (s Store) List() ([]Credential, error) {
106113 return creds , nil
107114}
108115
109- func (s * Store ) getStore () (credentials.Store , error ) {
110- return s .getStoreByHelper (config .GPTScriptHelperPrefix + s .cfg .CredentialsStore )
116+ func (s * Store ) getStore (ctx context. Context ) (credentials.Store , error ) {
117+ return s .getStoreByHelper (ctx , config .GPTScriptHelperPrefix + s .cfg .CredentialsStore )
111118}
112119
113- func (s * Store ) getStoreByHelper (helper string ) (credentials.Store , error ) {
120+ func (s * Store ) getStoreByHelper (ctx context. Context , helper string ) (credentials.Store , error ) {
114121 if helper == "" || helper == config .GPTScriptHelperPrefix + "file" {
115122 return credentials .NewFileStore (s .cfg ), nil
116123 }
117124
118125 // If the helper is referencing one of the credential helper programs, then reference the full path.
119126 if strings .HasPrefix (helper , "gptscript-credential-" ) {
127+ if err := s .credBuilder .EnsureCredentialHelpers (ctx ); err != nil {
128+ return nil , err
129+ }
130+
120131 helper = filepath .Join (s .credHelperDirs .BinDir , helper )
121132 }
122133
0 commit comments