// Package storage keeps the connection settings in the app's store and the // device token in the platform's secure storage. package storage import ( "crypto/rand" "crypto/sha256" "encoding/hex" "encoding/json" "github.com/egoist/asuka/sdk/secure" "github.com/egoist/asuka/sdk/store" "github.com/egoist/asuka/signal" ) // Connection is the last server the app connected to. type Connection struct { URL string `json:"url"` Proxy string `json:"proxy,omitempty"` Insecure bool `json:"insecure,omitempty"` Fingerprint string `json:"fingerprint,omitempty"` ServerID string `json:"serverId,omitempty"` // Passwordless servers need no token; remembering that avoids a // pointless password prompt on the next launch. Passwordless bool `json:"passwordless,omitempty"` } // Store is the persistence the UI needs. Tests use a memory store. type Store interface { Connection() (Connection, bool) SaveConnection(Connection) ClearConnection() InstallID() string LoadToken(server string) (string, bool, error) SaveToken(server, token string) error ClearToken(server string) error } // Persistent is the app's store: settings in store.json, tokens in the // Android Keystore / Keychain. type Persistent struct { conn *signal.Signal[Connection] hasConn *signal.Signal[bool] installID *signal.Signal[string] } // New makes the persistent store. Call it on the main thread, while a // component builds, as any signal. func New() *Persistent { raw := store.Signal("openchamber.connection", "") var c Connection ok := raw.Get() != "" && json.Unmarshal([]byte(raw.Get()), &c) == nil && c.URL != "" p := &Persistent{ conn: signal.NewFunc(c, nil), hasConn: signal.New(ok), installID: store.Signal("openchamber.installId", ""), } return p } func (p *Persistent) Connection() (Connection, bool) { return p.conn.Get(), p.hasConn.Get() } func (p *Persistent) SaveConnection(c Connection) { p.conn.Set(c) p.hasConn.Set(c.URL != "") buf, err := json.Marshal(c) if err != nil { return } // Keep it in the store as JSON; the signal above stays the source of // truth for the UI. store.Set("openchamber.connection", string(buf)) } func (p *Persistent) ClearConnection() { p.conn.Set(Connection{}) p.hasConn.Set(false) store.Set("openchamber.connection", "") } // InstallID is a stable random id for this installation. It names the // device when logging in, so repeated logins reuse one device record. func (p *Persistent) InstallID() string { if id := p.installID.Get(); id != "" { return id } var b [16]byte if _, err := rand.Read(b[:]); err != nil { return "openchamber-" + hex.EncodeToString(b[:8]) } id := hex.EncodeToString(b[:]) p.installID.Set(id) return id } func (p *Persistent) tokenKey(server string) string { sum := sha256.Sum256([]byte(server)) return "openchamber.token." + hex.EncodeToString(sum[:12]) } func (p *Persistent) LoadToken(server string) (string, bool, error) { return secure.Get(p.tokenKey(server)) } func (p *Persistent) SaveToken(server, token string) error { return secure.Set(p.tokenKey(server), token) } func (p *Persistent) ClearToken(server string) error { return secure.Delete(p.tokenKey(server)) }