Skip to content

Commit 6abb2f9

Browse files
committed
netlify: import token from netlify-cli config.json
netlify login stores the token in config.json under users.<userId>.auth.token (~/Library/Preferences/netlify on macOS, ~/.config/netlify on Linux), per https://docs.netlify.com/api-and-cli-guides/cli-guides/get-started-with-cli/#configjson-location. Keep the NETLIFY_AUTH_TOKEN importer and also try the config file.
1 parent e76d1b1 commit 6abb2f9

3 files changed

Lines changed: 90 additions & 1 deletion

File tree

‎plugins/netlify/personal_access_token.go‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package netlify
22

33
import (
4+
"context"
5+
46
"github.com/1Password/shell-plugins/sdk"
57
"github.com/1Password/shell-plugins/sdk/importer"
68
"github.com/1Password/shell-plugins/sdk/provision"
@@ -22,10 +24,48 @@ func PersonalAccessToken() schema.CredentialType {
2224
},
2325
},
2426
DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping),
25-
Importer: importer.TryEnvVarPair(defaultEnvVarMapping),
27+
Importer: importer.TryAll(
28+
importer.TryEnvVarPair(defaultEnvVarMapping),
29+
importer.MacOnly(TryNetlifyConfigFile("~/Library/Preferences/netlify/config.json")),
30+
importer.LinuxOnly(TryNetlifyConfigFile("~/.config/netlify/config.json")),
31+
),
2632
}
2733
}
2834

2935
var defaultEnvVarMapping = map[string]sdk.FieldName{
3036
"NETLIFY_AUTH_TOKEN": fieldname.Token,
3137
}
38+
39+
// TryNetlifyConfigFile imports the token that `netlify login` stores in the global config.json,
40+
// under users.<userId>.auth.token for the active userId.
41+
func TryNetlifyConfigFile(path string) sdk.Importer {
42+
return importer.TryFile(path, func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) {
43+
var config Config
44+
if err := contents.ToJSON(&config); err != nil {
45+
out.AddError(err)
46+
return
47+
}
48+
49+
user, ok := config.Users[config.UserID]
50+
if !ok || user.Auth.Token == "" {
51+
return
52+
}
53+
54+
out.AddCandidate(sdk.ImportCandidate{
55+
Fields: map[sdk.FieldName]string{
56+
fieldname.Token: user.Auth.Token,
57+
},
58+
})
59+
})
60+
}
61+
62+
type Config struct {
63+
UserID string `json:"userId"`
64+
Users map[string]ConfigUser `json:"users"`
65+
}
66+
67+
type ConfigUser struct {
68+
Auth struct {
69+
Token string `json:"token"`
70+
} `json:"auth"`
71+
}

‎plugins/netlify/personal_access_token_test.go‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,38 @@ func TestPersonalAccessTokenImporter(t *testing.T) {
3737
},
3838
},
3939
},
40+
"config file (macOS)": {
41+
OS: "darwin",
42+
Files: map[string]string{
43+
"~/Library/Preferences/netlify/config.json": plugintest.LoadFixture(t, "config.json"),
44+
},
45+
ExpectedCandidates: []sdk.ImportCandidate{
46+
{
47+
Fields: map[sdk.FieldName]string{
48+
fieldname.Token: "nfp_kV9wTq3ZtN0aB4cD7eF1gH5iJ8kL2mN6EXAMPLE",
49+
},
50+
},
51+
},
52+
},
53+
"config file (Linux)": {
54+
OS: "linux",
55+
Files: map[string]string{
56+
"~/.config/netlify/config.json": plugintest.LoadFixture(t, "config.json"),
57+
},
58+
ExpectedCandidates: []sdk.ImportCandidate{
59+
{
60+
Fields: map[sdk.FieldName]string{
61+
fieldname.Token: "nfp_kV9wTq3ZtN0aB4cD7eF1gH5iJ8kL2mN6EXAMPLE",
62+
},
63+
},
64+
},
65+
},
66+
"config file (logged out)": {
67+
OS: "darwin",
68+
Files: map[string]string{
69+
"~/Library/Preferences/netlify/config.json": `{"telemetryDisabled": false, "cliId": "55f2daf5-ee38-4fa0-8fd8-e67892a123d6", "userId": null}`,
70+
},
71+
ExpectedCandidates: nil,
72+
},
4073
})
4174
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"telemetryDisabled": false,
3+
"cliId": "55f2daf5-ee38-4fa0-8fd8-e67892a123d6",
4+
"userId": "5f3a1b2c4d5e6f7a8b9c0d1e",
5+
"users": {
6+
"5f3a1b2c4d5e6f7a8b9c0d1e": {
7+
"id": "5f3a1b2c4d5e6f7a8b9c0d1e",
8+
"name": "Wendy Appleseed",
9+
"email": "wendy.appleseed@example.com",
10+
"auth": {
11+
"token": "nfp_kV9wTq3ZtN0aB4cD7eF1gH5iJ8kL2mN6EXAMPLE",
12+
"github": {}
13+
}
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)