forked from gptscript-ai/gptscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecurity.go
56 lines (49 loc) · 1.73 KB
/
security.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package openapi
import (
"fmt"
"strings"
"github.com/gptscript-ai/gptscript/pkg/env"
)
// A SecurityInfo represents a security scheme in OpenAPI.
type SecurityInfo struct {
Name string `json:"name"` // name as defined in the security schemes
Type string `json:"type"` // http or apiKey
Scheme string `json:"scheme"` // bearer or basic, for type==http
APIKeyName string `json:"apiKeyName"` // name of the API key, for type==apiKey
In string `json:"in"` // header, query, or cookie, for type==apiKey
}
func (i SecurityInfo) GetCredentialToolStrings(hostname string) []string {
vars := i.getCredentialNamesAndEnvVars(hostname)
var tools []string
for cred, v := range vars {
field := "value"
switch i.Type {
case "apiKey":
field = i.APIKeyName
case "http":
if i.Scheme == "bearer" {
field = "bearer token"
} else {
if strings.Contains(v, "PASSWORD") {
field = "password"
} else {
field = "username"
}
}
}
tools = append(tools, fmt.Sprintf("github.com/gptscript-ai/credential as %s with %s as env and %q as message and %q as field",
cred, v, "Please provide a value for the "+v+" environment variable", field))
}
return tools
}
func (i SecurityInfo) getCredentialNamesAndEnvVars(hostname string) map[string]string {
if i.Type == "http" && i.Scheme == "basic" {
return map[string]string{
hostname + i.Name + "Username": "GPTSCRIPT_" + env.ToEnvLike(hostname) + "_" + env.ToEnvLike(i.Name) + "_USERNAME",
hostname + i.Name + "Password": "GPTSCRIPT_" + env.ToEnvLike(hostname) + "_" + env.ToEnvLike(i.Name) + "_PASSWORD",
}
}
return map[string]string{
hostname + i.Name: "GPTSCRIPT_" + env.ToEnvLike(hostname) + "_" + env.ToEnvLike(i.Name),
}
}