diff --git a/plugins/gitea/personal_access_token.go b/plugins/gitea/personal_access_token.go index b0dfd9e78..d399b8b2b 100644 --- a/plugins/gitea/personal_access_token.go +++ b/plugins/gitea/personal_access_token.go @@ -30,7 +30,7 @@ func ConfigPath() string { func PersonalAccessToken() schema.CredentialType { return schema.CredentialType{ Name: credname.PersonalAccessToken, - DocsURL: sdk.URL("https://docs.gitea.io/en-us/api-usage/#generating-and-listing-api-tokens"), + DocsURL: sdk.URL("https://docs.gitea.com/development/api-usage/#generating-and-listing-api-tokens"), ManagementURL: sdk.URL("https://gitea.com/user/settings/applications"), Fields: []schema.CredentialField{ { diff --git a/plugins/gt/gt.go b/plugins/gt/gt.go new file mode 100644 index 000000000..77829fd2f --- /dev/null +++ b/plugins/gt/gt.go @@ -0,0 +1,25 @@ +package gt + +import ( + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/needsauth" + "github.com/1Password/shell-plugins/sdk/schema" + "github.com/1Password/shell-plugins/sdk/schema/credname" +) + +func GiteaCLI() schema.Executable { + return schema.Executable{ + Name: "Gitea CLI", + Runs: []string{"gt"}, + DocsURL: sdk.URL("https://gitea.com/gitea/cli"), + NeedsAuth: needsauth.IfAll( + needsauth.NotForHelpOrVersion(), + needsauth.NotWithoutArgs(), + ), + Uses: []schema.CredentialUsage{ + { + Name: credname.PersonalAccessToken, + }, + }, + } +} diff --git a/plugins/gt/personal_access_token.go b/plugins/gt/personal_access_token.go new file mode 100644 index 000000000..705505bc9 --- /dev/null +++ b/plugins/gt/personal_access_token.go @@ -0,0 +1,170 @@ +package gt + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "os" + "path/filepath" + + "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 PersonalAccessToken() schema.CredentialType { + return schema.CredentialType{ + Name: credname.PersonalAccessToken, + DocsURL: sdk.URL("https://docs.gitea.com/development/api-usage/#generating-and-listing-api-tokens"), + ManagementURL: sdk.URL("https://gitea.com/user/settings/applications"), + Fields: []schema.CredentialField{ + { + Name: fieldname.Token, + MarkdownDescription: "Token used to authenticate to Gitea.", + Secret: true, + Composition: &schema.ValueComposition{ + Length: 40, + Charset: schema.Charset{ + Lowercase: true, + Digits: true, + }, + }, + }, + { + Name: fieldname.HostAddress, + MarkdownDescription: "The Gitea host to connect to, should start with https://", + Secret: false, + Composition: &schema.ValueComposition{ + Charset: schema.Charset{ + Lowercase: true, + Uppercase: true, + Digits: true, + Symbols: true, + }, + Prefix: "https://", + }, + }, + { + Name: fieldname.User, + MarkdownDescription: "Your Gitea username", + Secret: false, + }, + }, + DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), + Importer: importer.TryAll( + importer.TryEnvVarPair(defaultEnvVarMapping), + importer.TryEnvVarPair(map[string]sdk.FieldName{ + "GITEA_TOKEN": fieldname.Token, + "GITEA_CLI_HOST": fieldname.HostAddress, + }), + TryGiteaHostsFile(), + )} +} + +var defaultEnvVarMapping = map[string]sdk.FieldName{ + "GITEA_TOKEN": fieldname.Token, + "GITEA_INSTANCE_URL": fieldname.HostAddress, +} + +func configDir() string { + if gitConfigCliDir := os.Getenv("GITEA_CLI_CONFIG_DIR"); gitConfigCliDir != "" { + return gitConfigCliDir + } + + // gt's config resolver honors XDG_CONFIG_HOME on macOS; os.UserConfigDir does not. + if xdgConfigHome := os.Getenv("XDG_CONFIG_HOME"); xdgConfigHome != "" { + return filepath.Join(xdgConfigHome, "gitea-cli") + } + + configDir, err := os.UserConfigDir() + if err != nil { + return "~/.config/gitea-cli" + } + return filepath.Join(configDir, "gitea-cli") +} + +func HostsPath() string { + return filepath.Join(configDir(), "hosts.yml") +} + +// Identity builds a stable, opaque key from an instance URL and account. +func identity(instance, user string) string { + sum := sha256.Sum256([]byte(instance + "\x00" + user)) + return hex.EncodeToString(sum[:]) +} + +func credentialFilename(instance string, user string) string { + return filepath.Join(configDir(), "credentials", identity(instance, user)+".json") +} + +func loadCredentialFromFile(hostname string, username string, rootDir string) (Credential, error) { + var credential Credential + // In test environments, absolute paths are prefixed with RootDir + credPath := credentialFilename(hostname, username) + if rootDir != "" && filepath.IsAbs(credPath) { + credPath = filepath.Join(rootDir, credPath) + } + credBytes, err := os.ReadFile(credPath) + if err != nil { + return Credential{}, err + } + + if err := json.Unmarshal(credBytes, &credential); err != nil { + return Credential{}, err + } + + return credential, nil +} + +func TryGiteaHostsFile() sdk.Importer { + return importer.TryFile(HostsPath(), func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) { + var hosts Hosts + if err := contents.ToYAML(&hosts); err != nil { + out.AddError(err) + return + } + + for hostname, host := range hosts.Hosts { + for username, account := range host.Users { + if account.CredentialStore == "file" && account.AuthMethod == "token" { + credential, err := loadCredentialFromFile(hostname, username, in.RootDir) + if err != nil { + out.AddError(err) + continue + } + out.AddCandidate(sdk.ImportCandidate{ + Fields: map[sdk.FieldName]string{ + fieldname.Token: credential.AccessToken, + fieldname.HostAddress: hostname, + fieldname.User: username, + }, + NameHint: importer.SanitizeNameHint(username + "@" + hostname), + }) + } + } + } + }) +} + +type Credential struct { + AccessToken string `json:"access_token"` +} + +type Account struct { + AuthMethod string `yaml:"auth_method" json:"auth_method"` + CredentialStore string `yaml:"credential_store,omitempty" json:"credential_store,omitempty"` +} + +type Host struct { + User string `yaml:"user,omitempty" json:"user"` + Users map[string]Account `yaml:"users" json:"users"` +} + +type Hosts struct { + Version string `yaml:"version" json:"version"` + Hosts map[string]Host `yaml:"hosts" json:"hosts"` +} diff --git a/plugins/gt/personal_access_token_test.go b/plugins/gt/personal_access_token_test.go new file mode 100644 index 000000000..ac5df47ba --- /dev/null +++ b/plugins/gt/personal_access_token_test.go @@ -0,0 +1,146 @@ +package gt + +import ( + "os" + "path/filepath" + "testing" + + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/plugintest" + "github.com/1Password/shell-plugins/sdk/schema/fieldname" +) + +func TestHostsPath(t *testing.T) { + t.Run("honors GITEA_CLI_CONFIG_DIR when set", func(t *testing.T) { + giteaCliConfigDir := filepath.Join(t.TempDir(), "giteaCli") + t.Setenv("GITEA_CLI_CONFIG_DIR", giteaCliConfigDir) + + xdgConfigHome := filepath.Join(t.TempDir(), "xdg") + t.Setenv("XDG_CONFIG_HOME", xdgConfigHome) + + want := filepath.Join(giteaCliConfigDir, "hosts.yml") + if got := HostsPath(); got != want { + t.Errorf("HostsPath() = %q, want %q", got, want) + } + }) + + t.Run("honors XDG_CONFIG_HOME when set", func(t *testing.T) { + // gt reads its hosts info from GITEA_CLI_CONFIG_DIR/gitea-cli/hosts.yml via + // github.com/adrg/xdg, so the plugin must write to the same place. + xdgConfigHome := filepath.Join(t.TempDir(), "xdg") + t.Setenv("XDG_CONFIG_HOME", xdgConfigHome) + + want := filepath.Join(xdgConfigHome, "gitea-cli", "hosts.yml") + if got := HostsPath(); got != want { + t.Errorf("HostsPath() = %q, want %q", got, want) + } + }) + + t.Run("falls back to os.UserConfigDir when XDG_CONFIG_HOME is unset", func(t *testing.T) { + t.Setenv("XDG_CONFIG_HOME", "") + + configDir, err := os.UserConfigDir() + if err != nil { + t.Skipf("os.UserConfigDir() unavailable: %v", err) + } + + want := filepath.Join(configDir, "gitea-cli", "hosts.yml") + if got := HostsPath(); got != want { + t.Errorf("HostsPath() = %q, want %q", got, want) + } + }) +} + +func TestPersonalAccessTokenProvisioner(t *testing.T) { + plugintest.TestProvisioner(t, PersonalAccessToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{ + "default": { + ItemFields: map[sdk.FieldName]string{ + fieldname.Token: "5obcw3wk772a2beqezslabcmivghbci4qexample", + fieldname.HostAddress: "https://git.example.com", + fieldname.User: "example", + }, + ExpectedOutput: sdk.ProvisionOutput{ + Environment: map[string]string{ + "GITEA_TOKEN": "5obcw3wk772a2beqezslabcmivghbci4qexample", + "GITEA_INSTANCE_URL": "https://git.example.com", + }, + }, + }, + }) +} + +func TestPersonalAccessTokenImporter(t *testing.T) { + xdgConfigHome := filepath.Join(t.TempDir(), "xdg") + t.Setenv("XDG_CONFIG_HOME", xdgConfigHome) + + baseConfigDir := filepath.Join(xdgConfigHome, "gitea-cli") + + credFile1 := "cf196e961d99c01a4ae839aa3c7d0ba52e67aafd7ce088d5df1c96ce5eeb5dee.json" + credFile2 := "86707cbf0f8161fb4b5ecfbaa450653003d4e0b809b06a66aac978452164d233.json" + + plugintest.TestImporter(t, PersonalAccessToken().Importer, map[string]plugintest.ImportCase{ + "GITEA_INSTANCE_URL": { + Environment: map[string]string{ + "GITEA_TOKEN": "5obcw3wk772a2beqezslabcmivghbci4qexample", + "GITEA_INSTANCE_URL": "https://git.example.com", + }, + ExpectedCandidates: []sdk.ImportCandidate{ + { + Fields: map[sdk.FieldName]string{ + fieldname.Token: "5obcw3wk772a2beqezslabcmivghbci4qexample", + fieldname.HostAddress: "https://git.example.com", + }, + }, + { + Fields: map[sdk.FieldName]string{ + fieldname.Token: "5obcw3wk772a2beqezslabcmivghbci4qexample", + }, + }, + }, + }, + "GITEA_CLI_HOST": { + Environment: map[string]string{ + "GITEA_TOKEN": "5obcw3wk772a2beqezslabcmivghbci4qexample", + "GITEA_CLI_HOST": "https://git.example.com", + }, + ExpectedCandidates: []sdk.ImportCandidate{ + { + Fields: map[sdk.FieldName]string{ + fieldname.Token: "5obcw3wk772a2beqezslabcmivghbci4qexample", + fieldname.HostAddress: "https://git.example.com", + }, + }, + { + Fields: map[sdk.FieldName]string{ + fieldname.Token: "5obcw3wk772a2beqezslabcmivghbci4qexample", + }, + }, + }, + }, + "hosts file": { + Files: map[string]string{ + HostsPath(): plugintest.LoadFixture(t, "hosts.yml"), + filepath.Join(baseConfigDir, "credentials", credFile1): plugintest.LoadFixture(t, "credentials/"+credFile1), + filepath.Join(baseConfigDir, "credentials", credFile2): plugintest.LoadFixture(t, "credentials/"+credFile2), + }, + ExpectedCandidates: []sdk.ImportCandidate{ + { + Fields: map[sdk.FieldName]string{ + fieldname.Token: "oyyfsny27bgphldmhvffxhhlmqvdkzjrfslrsj9f", + fieldname.HostAddress: "https://git.example.com", + fieldname.User: "example", + }, + NameHint: "example@https://git.exa…", + }, + { + Fields: map[sdk.FieldName]string{ + fieldname.Token: "enjkarzu2ca5ffcnvzaczxncuczeoq9utlpqqrzs", + fieldname.HostAddress: "https://gitea.com", + fieldname.User: "example@example.com", + }, + NameHint: "example@example.com@htt…", + }, + }, + }, + }) +} diff --git a/plugins/gt/plugin.go b/plugins/gt/plugin.go new file mode 100644 index 000000000..1dd818a26 --- /dev/null +++ b/plugins/gt/plugin.go @@ -0,0 +1,22 @@ +package gt + +import ( + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/schema" +) + +func New() schema.Plugin { + return schema.Plugin{ + Name: "gt", + Platform: schema.PlatformInfo{ + Name: "Gitea", + Homepage: sdk.URL("https://gitea.io/"), + }, + Credentials: []schema.CredentialType{ + PersonalAccessToken(), + }, + Executables: []schema.Executable{ + GiteaCLI(), + }, + } +} diff --git a/plugins/gt/test-fixtures/credentials/86707cbf0f8161fb4b5ecfbaa450653003d4e0b809b06a66aac978452164d233.json b/plugins/gt/test-fixtures/credentials/86707cbf0f8161fb4b5ecfbaa450653003d4e0b809b06a66aac978452164d233.json new file mode 100644 index 000000000..17f7d8cdc --- /dev/null +++ b/plugins/gt/test-fixtures/credentials/86707cbf0f8161fb4b5ecfbaa450653003d4e0b809b06a66aac978452164d233.json @@ -0,0 +1 @@ +{"access_token":"enjkarzu2ca5ffcnvzaczxncuczeoq9utlpqqrzs"} diff --git a/plugins/gt/test-fixtures/credentials/cf196e961d99c01a4ae839aa3c7d0ba52e67aafd7ce088d5df1c96ce5eeb5dee.json b/plugins/gt/test-fixtures/credentials/cf196e961d99c01a4ae839aa3c7d0ba52e67aafd7ce088d5df1c96ce5eeb5dee.json new file mode 100644 index 000000000..698f8b78d --- /dev/null +++ b/plugins/gt/test-fixtures/credentials/cf196e961d99c01a4ae839aa3c7d0ba52e67aafd7ce088d5df1c96ce5eeb5dee.json @@ -0,0 +1 @@ +{"access_token":"oyyfsny27bgphldmhvffxhhlmqvdkzjrfslrsj9f"} diff --git a/plugins/gt/test-fixtures/hosts.yml b/plugins/gt/test-fixtures/hosts.yml new file mode 100644 index 000000000..907cfe58a --- /dev/null +++ b/plugins/gt/test-fixtures/hosts.yml @@ -0,0 +1,14 @@ +version: "1" +hosts: + https://git.example.com: + user: example + users: + example: + auth_method: token + credential_store: file + https://gitea.com: + user: example@example.com + users: + example@example.com: + auth_method: token + credential_store: file