1
1
package credentials
2
2
3
3
import (
4
+ "context"
4
5
"fmt"
5
6
"path/filepath"
6
7
"regexp"
@@ -10,32 +11,38 @@ import (
10
11
"github.com/gptscript-ai/gptscript/pkg/config"
11
12
)
12
13
14
+ type CredentialBuilder interface {
15
+ EnsureCredentialHelpers (ctx context.Context ) error
16
+ }
17
+
13
18
type 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 )
18
23
}
19
24
20
25
type Store struct {
21
26
credCtx string
27
+ credBuilder CredentialBuilder
22
28
credHelperDirs CredentialHelperDirs
23
29
cfg * config.CLIConfig
24
30
}
25
31
26
- func NewStore (cfg * config.CLIConfig , credCtx , cacheDir string ) (CredentialStore , error ) {
32
+ func NewStore (cfg * config.CLIConfig , credentialBuilder CredentialBuilder , credCtx , cacheDir string ) (CredentialStore , error ) {
27
33
if err := validateCredentialCtx (credCtx ); err != nil {
28
34
return nil , err
29
35
}
30
36
return Store {
31
37
credCtx : credCtx ,
38
+ credBuilder : credentialBuilder ,
32
39
credHelperDirs : GetCredentialHelperDirs (cacheDir ),
33
40
cfg : cfg ,
34
41
}, nil
35
42
}
36
43
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 )
39
46
if err != nil {
40
47
return nil , false , err
41
48
}
@@ -57,9 +64,9 @@ func (s Store) Get(toolName string) (*Credential, bool, error) {
57
64
return & cred , true , nil
58
65
}
59
66
60
- func (s Store ) Add (cred Credential ) error {
67
+ func (s Store ) Add (ctx context. Context , cred Credential ) error {
61
68
cred .Context = s .credCtx
62
- store , err := s .getStore ()
69
+ store , err := s .getStore (ctx )
63
70
if err != nil {
64
71
return err
65
72
}
@@ -70,16 +77,16 @@ func (s Store) Add(cred Credential) error {
70
77
return store .Store (auth )
71
78
}
72
79
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 )
75
82
if err != nil {
76
83
return err
77
84
}
78
85
return store .Erase (toolNameWithCtx (toolName , s .credCtx ))
79
86
}
80
87
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 )
83
90
if err != nil {
84
91
return nil , err
85
92
}
@@ -106,17 +113,21 @@ func (s Store) List() ([]Credential, error) {
106
113
return creds , nil
107
114
}
108
115
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 )
111
118
}
112
119
113
- func (s * Store ) getStoreByHelper (helper string ) (credentials.Store , error ) {
120
+ func (s * Store ) getStoreByHelper (ctx context. Context , helper string ) (credentials.Store , error ) {
114
121
if helper == "" || helper == config .GPTScriptHelperPrefix + "file" {
115
122
return credentials .NewFileStore (s .cfg ), nil
116
123
}
117
124
118
125
// If the helper is referencing one of the credential helper programs, then reference the full path.
119
126
if strings .HasPrefix (helper , "gptscript-credential-" ) {
127
+ if err := s .credBuilder .EnsureCredentialHelpers (ctx ); err != nil {
128
+ return nil , err
129
+ }
130
+
120
131
helper = filepath .Join (s .credHelperDirs .BinDir , helper )
121
132
}
122
133
0 commit comments