Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/gitea/personal_access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand Down
25 changes: 25 additions & 0 deletions plugins/gt/gt.go
Original file line number Diff line number Diff line change
@@ -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,
},
},
}
}
170 changes: 170 additions & 0 deletions plugins/gt/personal_access_token.go
Original file line number Diff line number Diff line change
@@ -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"`
}
146 changes: 146 additions & 0 deletions plugins/gt/personal_access_token_test.go
Original file line number Diff line number Diff line change
@@ -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…",
},
},
},
})
}
22 changes: 22 additions & 0 deletions plugins/gt/plugin.go
Original file line number Diff line number Diff line change
@@ -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(),
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"access_token":"enjkarzu2ca5ffcnvzaczxncuczeoq9utlpqqrzs"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"access_token":"oyyfsny27bgphldmhvffxhhlmqvdkzjrfslrsj9f"}
14 changes: 14 additions & 0 deletions plugins/gt/test-fixtures/hosts.yml
Original file line number Diff line number Diff line change
@@ -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