Skip to content

Commit

Permalink
Merge pull request #322 from thaJeztah/pass_dry
Browse files Browse the repository at this point in the history
pass: add utilities for encoding/decoding serverURL
  • Loading branch information
thaJeztah committed May 10, 2024
2 parents f64d6b1 + d3ef442 commit 74840b3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
27 changes: 21 additions & 6 deletions pass/pass.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ func (p Pass) Add(creds *credentials.Credentials) error {
return errors.New("missing credentials")
}

encoded := base64.URLEncoding.EncodeToString([]byte(creds.ServerURL))

encoded := encodeServerURL(creds.ServerURL)
_, err := p.runPass(creds.Secret, "insert", "-f", "-m", path.Join(PASS_FOLDER, encoded, creds.Username))
return err
}
Expand All @@ -99,7 +98,7 @@ func (p Pass) Delete(serverURL string) error {
return errors.New("missing server url")
}

encoded := base64.URLEncoding.EncodeToString([]byte(serverURL))
encoded := encodeServerURL(serverURL)
_, err := p.runPass("", "rm", "-rf", path.Join(PASS_FOLDER, encoded))
return err
}
Expand Down Expand Up @@ -142,7 +141,7 @@ func (p Pass) Get(serverURL string) (string, string, error) {
return "", "", errors.New("missing server url")
}

encoded := base64.URLEncoding.EncodeToString([]byte(serverURL))
encoded := encodeServerURL(serverURL)

if _, err := os.Stat(path.Join(getPassDir(), PASS_FOLDER, encoded)); err != nil {
if os.IsNotExist(err) {
Expand Down Expand Up @@ -180,7 +179,7 @@ func (p Pass) List() (map[string]string, error) {
continue
}

serverURL, err := base64.URLEncoding.DecodeString(server.Name())
serverURL, err := decodeServerURL(server.Name())
if err != nil {
return nil, err
}
Expand All @@ -194,8 +193,24 @@ func (p Pass) List() (map[string]string, error) {
continue
}

resp[string(serverURL)] = strings.TrimSuffix(usernames[0].Name(), ".gpg")
resp[serverURL] = strings.TrimSuffix(usernames[0].Name(), ".gpg")
}

return resp, nil
}

// encodeServerURL returns the serverURL in base64-URL encoding to use
// as directory-name in pass storage.
func encodeServerURL(serverURL string) string {
return base64.URLEncoding.EncodeToString([]byte(serverURL))
}

// decodeServerURL decodes base64-URL encoded serverURL. ServerURLs are
// used in encoded format for directory-names in pass storage.
func decodeServerURL(encodedServerURL string) (string, error) {
serverURL, err := base64.URLEncoding.DecodeString(encodedServerURL)
if err != nil {
return "", err
}
return string(serverURL), nil
}
3 changes: 1 addition & 2 deletions pass/pass_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package pass

import (
"encoding/base64"
"os"
"path"
"strings"
Expand Down Expand Up @@ -152,7 +151,7 @@ func TestPassHelperWithEmptyServer(t *testing.T) {
}
} else {
// No credentials; create an empty directory for this server.
serverURL := base64.URLEncoding.EncodeToString([]byte(cred.ServerURL))
serverURL := encodeServerURL(cred.ServerURL)
p := path.Join(getPassDir(), PASS_FOLDER, serverURL)
if err := os.Mkdir(p, 0o755); err != nil {
t.Error(err)
Expand Down

0 comments on commit 74840b3

Please sign in to comment.