]> khamidou — GitHub mirrors - prguy.git/commitdiff
Save credentials in keyring
authorKarim Hamidou <karim@khamidou.com>
Tue, 13 Aug 2024 14:16:03 +0000 (07:16 -0700)
committerKarim Hamidou <karim@khamidou.com>
Tue, 13 Aug 2024 14:16:03 +0000 (07:16 -0700)
cmd/prguy/config.go
cmd/prguy/main.go
cmd/prguy/repos.go
go.mod
go.sum

index 46566efd97da51d3fff3cf1114fd60e35968804b..3ee13fe6ed20ab623933f07335619db0317db147 100644 (file)
@@ -1,49 +1,37 @@
 package main
 
 import (
-       "encoding/json"
-       "io/ioutil"
-       "os"
-       "path/filepath"
-       "time"
+       "github.com/zalando/go-keyring"
+       "os/user"
 )
 
+const keychainService = "prguy"
+
 type Config struct {
-       OAuthToken      string
-       Scope           string
-       LastUpdatedTime string
+       OAuthToken string
 }
 
-func (c *Config) getConfigPath() (string, error) {
-       dirname, err := os.UserHomeDir()
+func (c *Config) exists() bool {
+       username, err := user.Current()
        if err != nil {
-               return "", err
+               return false
        }
 
-       return filepath.Join(dirname, ".prguy"), nil
-}
+       _, err = keyring.Get(keychainService, username.Username)
+       if err != nil {
+               return false
+       }
 
-func (c *Config) exists() bool {
-       configPath, err := c.getConfigPath()
-       _, err = os.Stat(configPath)
-       return err == nil
+       return true
 }
 
 func (c *Config) save() error {
-       configPath, err := c.getConfigPath()
-       file, err := os.Create(configPath)
+       username, err := user.Current()
        if err != nil {
                return err
        }
 
-       defer file.Close()
-       c.LastUpdatedTime = time.Now().String()
-       jsonData, err := json.MarshalIndent(c, "", "  ")
-       if err != nil {
-               return err
-       }
-
-       _, err = file.Write(jsonData)
+       err = keyring.Set(keychainService, username.Username, c.OAuthToken)
        if err != nil {
                return err
        }
@@ -52,17 +40,16 @@ func (c *Config) save() error {
 }
 
 func (c *Config) load() error {
-       configPath, err := c.getConfigPath()
-       file, err := os.Open(configPath)
+       username, err := user.Current()
        if err != nil {
                return err
        }
 
-       defer file.Close()
-       jsonData, err := ioutil.ReadAll(file)
+       secret, err := keyring.Get(keychainService, username.Username)
        if err != nil {
                return err
        }
 
-       return json.Unmarshal(jsonData, c)
+       c.OAuthToken = secret
+       return nil
 }
index 474ada48c8acb3b410fd6318c1606c82ea25f11f..54c3b3773ab0c1cf9711d60614355fd1418f9b62 100644 (file)
@@ -318,7 +318,7 @@ func pollGithubDeviceAuth(deviceCode string, cancel context.CancelFunc) error {
                        continue
                }
 
-               scope, ok := parsedResponse["scope"]
+               _, ok = parsedResponse["scope"]
                if !ok {
                        errorOut("Missing scopes!",
                                "The Github API did not return us the scopes, please retry in a bit.")
@@ -327,7 +327,7 @@ func pollGithubDeviceAuth(deviceCode string, cancel context.CancelFunc) error {
 
                // Save the access token and scope
                fmt.Println("Saving access token and scope")
-               c := Config{OAuthToken: access_token[0], Scope: scope[0]}
+               c := Config{OAuthToken: access_token[0]}
                c.save()
                cancel()
                return nil
index 9417be4f2611ad8c4d9782c05df6a21b6d15f931..9fff3f67c6b0de4655d0b9cebdd7f58d90a120f9 100644 (file)
@@ -58,7 +58,7 @@ func listUserPRs(token string, isDemo bool) (*orderedmap.OrderedMap, *orderedmap
        }
 
        if resp.StatusCode != 200 {
-               msg := fmt.Sprintf("Got a '%s'error from the Github API. Please retry in a bit.",
+               msg := fmt.Sprintf("Got a '%s' error from the Github API. Please retry in a bit.",
                        resp.Status)
                return nil, nil, errors.New(msg)
        }
diff --git a/go.mod b/go.mod
index 53a3c52d1d9679d22c979b791bcef5e6fcfde83d..0dc038b997ed373444cf21b1ae353a1bcd73e433 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -7,12 +7,15 @@ require (
        github.com/iancoleman/orderedmap v0.3.0
        github.com/ncruces/zenity v0.10.12
        github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
+       github.com/zalando/go-keyring v0.2.5
 )
 
 require (
        github.com/akavel/rsrc v0.10.2 // indirect
+       github.com/alessio/shellescape v1.4.1 // indirect
+       github.com/danieljoos/wincred v1.2.0 // indirect
        github.com/dchest/jsmin v0.0.0-20220218165748-59f39799265f // indirect
-       github.com/godbus/dbus/v5 v5.0.4 // indirect
+       github.com/godbus/dbus/v5 v5.1.0 // indirect
        github.com/josephspurrier/goversioninfo v1.4.0 // indirect
        github.com/randall77/makefat v0.0.0-20210315173500-7ddd0e42c844 // indirect
        github.com/tevino/abool v1.2.0 // indirect
diff --git a/go.sum b/go.sum
index ebf6b0141c2e5e19eeaac89d4cde96443f392ac0..00bac394e5347fb8ea388e48b7e09ed1549934c0 100644 (file)
--- a/go.sum
+++ b/go.sum
@@ -2,12 +2,18 @@ fyne.io/systray v1.10.0 h1:Yr1D9Lxeiw3+vSuZWPlaHC8BMjIHZXJKkek706AfYQk=
 fyne.io/systray v1.10.0/go.mod h1:oM2AQqGJ1AMo4nNqZFYU8xYygSBZkW2hmdJ7n4yjedE=
 github.com/akavel/rsrc v0.10.2 h1:Zxm8V5eI1hW4gGaYsJQUhxpjkENuG91ki8B4zCrvEsw=
 github.com/akavel/rsrc v0.10.2/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
-github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
+github.com/alessio/shellescape v1.4.1 h1:V7yhSDDn8LP4lc4jS8pFkt0zCnzVJlG5JXy9BVKJUX0=
+github.com/alessio/shellescape v1.4.1/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30=
+github.com/danieljoos/wincred v1.2.0 h1:ozqKHaLK0W/ii4KVbbvluM91W2H3Sh0BncbUNPS7jLE=
+github.com/danieljoos/wincred v1.2.0/go.mod h1:FzQLLMKBFdvu+osBrnFODiv32YGwCfx0SkRa/eYHgec=
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
+github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/dchest/jsmin v0.0.0-20220218165748-59f39799265f h1:OGqDDftRTwrvUoL6pOG7rYTmWsTCvyEWFsMjg+HcOaA=
 github.com/dchest/jsmin v0.0.0-20220218165748-59f39799265f/go.mod h1:Dv9D0NUlAsaQcGQZa5kc5mqR9ua72SmA8VXi4cd+cBw=
-github.com/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA=
 github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
+github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
+github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
 github.com/iancoleman/orderedmap v0.3.0 h1:5cbR2grmZR/DiVt+VJopEhtVs9YGInGIxAoMJn+Ichc=
 github.com/iancoleman/orderedmap v0.3.0/go.mod h1:XuLcCUkdL5owUCQeF2Ue9uuw1EptkJDkXXS7VoV7XGE=
 github.com/josephspurrier/goversioninfo v1.4.0 h1:Puhl12NSHUSALHSuzYwPYQkqa2E1+7SrtAPJorKK0C8=
@@ -21,10 +27,15 @@ github.com/randall77/makefat v0.0.0-20210315173500-7ddd0e42c844/go.mod h1:T1TLSf
 github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA=
 github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog=
 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
-github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
+github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
+github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
 github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
+github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
 github.com/tevino/abool v1.2.0 h1:heAkClL8H6w+mK5md9dzsuohKeXHUpY7Vw0ZCKW+huA=
 github.com/tevino/abool v1.2.0/go.mod h1:qc66Pna1RiIsPa7O4Egxxs9OqkuxDX55zznh9K07Tzg=
+github.com/zalando/go-keyring v0.2.5 h1:Bc2HHpjALryKD62ppdEzaFG6VxL6Bc+5v0LYpN8Lba8=
+github.com/zalando/go-keyring v0.2.5/go.mod h1:HL4k+OXQfJUWaMnqyuSOc0drfGPX2b51Du6K+MRgZMk=
 go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
 go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
 golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ=
@@ -33,5 +44,6 @@ golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9/go.mod h1:h1NjWce9XRLGQEsW7w
 golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
 golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
-gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
+gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=