Skip to content

Commit 1922256

Browse files
committed
WIP: Add keyring support to app.state for credentials
1 parent b030f61 commit 1922256

3 files changed

Lines changed: 52 additions & 2 deletions

File tree

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/cenkalti/backoff/v4 v4.3.0
99
github.com/coder/websocket v1.8.14
1010
github.com/devgianlu/shannon v0.0.0-20230613115856-82ec90b7fa7e
11-
github.com/godbus/dbus/v5 v5.2.0
11+
github.com/godbus/dbus/v5 v5.2.2
1212
github.com/gofrs/flock v0.13.0
1313
github.com/grandcat/zeroconf v1.0.0
1414
github.com/jfreymuth/pulse v0.1.2-0.20241102120944-4ffb35054b53
@@ -33,6 +33,7 @@ require (
3333

3434
require (
3535
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
36+
github.com/danieljoos/wincred v1.2.3 // indirect
3637
github.com/davecgh/go-spew v1.1.1 // indirect
3738
github.com/fsnotify/fsnotify v1.9.0 // indirect
3839
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
@@ -43,6 +44,7 @@ require (
4344
github.com/pmezard/go-difflib v1.0.0 // indirect
4445
github.com/rogpeppe/go-internal v1.14.1 // indirect
4546
github.com/stretchr/objx v0.5.3 // indirect
47+
github.com/zalando/go-keyring v0.2.7 // indirect
4648
go.yaml.in/yaml/v3 v3.0.4 // indirect
4749
golang.org/x/mod v0.31.0 // indirect
4850
golang.org/x/sync v0.19.0 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK3
44
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
55
github.com/coder/websocket v1.8.14 h1:9L0p0iKiNOibykf283eHkKUHHrpG7f65OE3BhhO7v9g=
66
github.com/coder/websocket v1.8.14/go.mod h1:NX3SzP+inril6yawo5CQXx8+fk145lPDC6pumgx0mVg=
7+
github.com/danieljoos/wincred v1.2.3 h1:v7dZC2x32Ut3nEfRH+vhoZGvN72+dQ/snVXo/vMFLdQ=
8+
github.com/danieljoos/wincred v1.2.3/go.mod h1:6qqX0WNrS4RzPZ1tnroDzq9kY3fu1KwE7MRLQK4X0bs=
79
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
810
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
911
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -15,6 +17,8 @@ github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9L
1517
github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
1618
github.com/godbus/dbus/v5 v5.2.0 h1:3WexO+U+yg9T70v9FdHr9kCxYlazaAXUhx2VMkbfax8=
1719
github.com/godbus/dbus/v5 v5.2.0/go.mod h1:3AAv2+hPq5rdnr5txxxRwiGjPXamgoIHgz9FPBfOp3c=
20+
github.com/godbus/dbus/v5 v5.2.2 h1:TUR3TgtSVDmjiXOgAAyaZbYmIeP3DPkld3jgKGV8mXQ=
21+
github.com/godbus/dbus/v5 v5.2.2/go.mod h1:3AAv2+hPq5rdnr5txxxRwiGjPXamgoIHgz9FPBfOp3c=
1822
github.com/gofrs/flock v0.13.0 h1:95JolYOvGMqeH31+FC7D2+uULf6mG61mEZ/A8dRYMzw=
1923
github.com/gofrs/flock v0.13.0/go.mod h1:jxeyy9R1auM5S6JYDBhDt+E2TCo7DkratH4Pgi8P+Z0=
2024
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
@@ -66,6 +70,8 @@ github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu
6670
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
6771
github.com/xlab/vorbis-go v0.0.0-20210911202351-b5b85f1ec645 h1:lYg/+vV/Fd5WM1+Ptg54Am3y4mDXaMSrT+mKUHV5uVc=
6872
github.com/xlab/vorbis-go v0.0.0-20210911202351-b5b85f1ec645/go.mod h1:AMqfx3jFwPqem3u8mF2lsRodZs30jG/Mag5HZ3mB3sA=
73+
github.com/zalando/go-keyring v0.2.7 h1:YbqBw40+g4g69UNk4WsRM/fV9YErfVWwozE2+7Bn+7g=
74+
github.com/zalando/go-keyring v0.2.7/go.mod h1:tsMo+VpRq5NGyKfxoBVjCuMrG47yj8cmakZDO5QGii0=
6975
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
7076
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
7177
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=

state.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package go_librespot
33
import (
44
"encoding/json"
55
"fmt"
6+
"github.com/zalando/go-keyring"
67
"os"
78
"path/filepath"
89
"sync"
@@ -51,6 +52,10 @@ func (s *AppState) Read(configDir string) error {
5152
} else {
5253
s.log.Debugf("stored credentials not found")
5354
}
55+
56+
// Credentials might be in the OS keyring.
57+
s.getCredsFromKeyring()
58+
5459
}
5560

5661
return nil
@@ -67,14 +72,51 @@ func (s *AppState) Write() error {
6772
if err != nil {
6873
return fmt.Errorf("failed creating temporary file for app state: %w", err)
6974
}
75+
if err := s.writeCredsToKeyring(); err != nil {
76+
if err := json.NewEncoder(tmpFile).Encode(s); err != nil {
77+
return fmt.Errorf("failed writing marshalled app state: %w", err)
78+
}
79+
}
80+
persistedState := struct {
81+
DeviceId string `json:"device_id"`
82+
EventManager json.RawMessage `json:"event_manager"`
83+
LastVolume *uint32 `json:"last_volume"`
84+
}{
85+
DeviceId: s.DeviceId,
86+
EventManager: s.EventManager,
87+
LastVolume: s.LastVolume,
88+
}
7089

71-
if err := json.NewEncoder(tmpFile).Encode(&s); err != nil {
90+
if err := json.NewEncoder(tmpFile).Encode(&persistedState); err != nil {
7291
return fmt.Errorf("failed writing marshalled app state: %w", err)
7392
}
7493

94+
7595
if err := os.Rename(tmpFile.Name(), s.path); err != nil {
7696
return fmt.Errorf("failed replacing app state file: %w", err)
7797
}
7898

7999
return nil
80100
}
101+
102+
func (s *AppState) getCredsFromKeyring() {
103+
storedCredsRaw, err := keyring.Get("librespot", "credentials")
104+
if err != nil {
105+
return
106+
}
107+
if err := json.Unmarshal([]byte(storedCredsRaw), &s.Credentials); err != nil {
108+
return
109+
}
110+
}
111+
112+
func (s *AppState) writeCredsToKeyring() error {
113+
creds, err := json.Marshal(s.Credentials)
114+
if err != nil {
115+
return err
116+
}
117+
err = keyring.Set("librespot", "credentials", string(creds))
118+
if err != nil {
119+
return err
120+
}
121+
return nil
122+
}

0 commit comments

Comments
 (0)