-
Notifications
You must be signed in to change notification settings - Fork 157
/
api_key.go
77 lines (73 loc) · 2.2 KB
/
api_key.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package heroku
import (
"bufio"
"context"
"strings"
"github.com/1Password/shell-plugins/sdk"
"github.com/1Password/shell-plugins/sdk/importer"
"github.com/1Password/shell-plugins/sdk/provision"
"github.com/1Password/shell-plugins/sdk/schema"
"github.com/1Password/shell-plugins/sdk/schema/credname"
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
)
func APIKey() schema.CredentialType {
return schema.CredentialType{
Name: credname.APIKey,
DocsURL: sdk.URL("https://devcenter.heroku.com/articles/authentication"),
ManagementURL: sdk.URL("https://dashboard.heroku.com/account"),
Fields: []schema.CredentialField{
{
Name: fieldname.APIKey,
MarkdownDescription: "API Key used to authenticate to Heroku.",
Secret: true,
Composition: &schema.ValueComposition{
Length: 36,
Charset: schema.Charset{
Lowercase: true,
Digits: true,
},
},
},
},
DefaultProvisioner: provision.EnvVars(map[string]sdk.FieldName{
"HEROKU_API_KEY": fieldname.APIKey,
}),
Importer: importer.TryAll(
importer.TryAllEnvVars(fieldname.APIKey, "HEROKU_API_KEY"),
TryNetrcFile(),
),
}
}
// TryNetrcFile tries to find Heroku API keys in the ~/.netrc file
func TryNetrcFile() sdk.Importer {
return importer.TryFile("~/.netrc", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) {
s := bufio.NewScanner(strings.NewReader(string(contents)))
var machine, login, password string
for s.Scan() {
if words := strings.Split(strings.Trim(s.Text(), " "), " "); len(words) >= 2 {
switch words[0] {
case "machine":
if machine != "" {
login, password = "", ""
}
machine = words[1]
case "login":
login = words[1]
case "password":
password = words[1]
}
if login != "" && password != "" && machine != "" {
if machine == "api.heroku.com" || machine == "git.heroku.com" {
out.AddCandidate(sdk.ImportCandidate{
NameHint: importer.SanitizeNameHint(login),
Fields: map[sdk.FieldName]string{
fieldname.APIKey: password,
},
})
}
machine, login, password = "", "", ""
}
}
}
})
}