Add session mode: signing in and adding to the cart.
With cart_mode = "session", dibs signs in to the shop's account and puts an item in the cart when it fires. The account's cookies are kept apart from the watcher's and stored encrypted, with the key in the OS keyring. Without a keyring, they're stored in a file only you can read. An alert now goes out right away, without a cart link, and a second one follows once the item is in the cart. If adding fails, the second alert carries the cart link instead. Amazon only accepts additions its page scripts have seen, so plugins can now press a button in headless Chrome with ctx:click. The TUI asks for one-time codes and shows the plugin's warning about account locks in a modal. 'dibs login' signs in from a terminal, or with --browser in a Chrome window, and 'dibs logout' deletes the cookies. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e2cbc1a55c
commit
15a34e1f84
39 files changed
+3529
-163
No files matched your search
+50
-3
@@ -154,6 +154,28 @@ func (j *Jar) importLocked(host string, cookies []Cookie, now time.Time) {
|
||||
}
|
||||
}
|
||||
|
||||
// Clear deletes every cookie. The deletions are saved like any other
|
||||
// change, so other processes sharing the file drop the cookies too.
|
||||
func (j *Jar) Clear() {
|
||||
j.mu.Lock()
|
||||
defer j.mu.Unlock()
|
||||
|
||||
now := j.now()
|
||||
|
||||
for host, byKey := range j.cookies {
|
||||
for _, c := range byKey {
|
||||
if c.expired(now) { continue }
|
||||
|
||||
hc := &http.Cookie{Name: c.Name, Path: c.Path, MaxAge: -1}
|
||||
if !c.HostOnly { hc.Domain = c.Domain }
|
||||
j.jar.SetCookies(&url.URL{Scheme: "https", Host: host, Path: c.Path}, []*http.Cookie{hc})
|
||||
|
||||
c.Expires, c.Set = now, now
|
||||
j.store(host, c, now)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func key(c Cookie) string { return c.Domain + "|" + c.Path + "|" + c.Name }
|
||||
|
||||
// store records c for host. Callers hold mu.
|
||||
@@ -164,14 +186,26 @@ func (j *Jar) store(host string, c Cookie, now time.Time) {
|
||||
j.cookies[host][key(c)] = c
|
||||
}
|
||||
|
||||
// Codec turns the cookie file's JSON into what is stored on disk and back,
|
||||
// such as to encrypt it.
|
||||
type Codec interface {
|
||||
Encode(data []byte) ([]byte, error)
|
||||
Decode(data []byte) ([]byte, error)
|
||||
}
|
||||
|
||||
// Save writes the cookies to path, readable only by the user. Other dibs
|
||||
// processes may share the file, so it first takes in their newer cookies.
|
||||
func (j *Jar) Save(path string) error {
|
||||
return j.SaveWith(path, nil)
|
||||
}
|
||||
|
||||
// SaveWith is Save through codec, which may be nil.
|
||||
func (j *Jar) SaveWith(path string, codec Codec) error {
|
||||
unlock, err := fsutil.Lock(path + ".lock")
|
||||
if err != nil { return err }
|
||||
defer unlock()
|
||||
|
||||
saved, err := readCookies(path)
|
||||
saved, err := readCookies(path, codec)
|
||||
if err != nil { return err }
|
||||
|
||||
j.mu.Lock()
|
||||
@@ -195,12 +229,21 @@ func (j *Jar) Save(path string) error {
|
||||
data, err := json.MarshalIndent(out, "", " ")
|
||||
if err != nil { return err }
|
||||
|
||||
if codec != nil {
|
||||
if data, err = codec.Encode(data); err != nil { return err }
|
||||
}
|
||||
|
||||
return fsutil.WriteFileAtomic(path, data, 0o600)
|
||||
}
|
||||
|
||||
// Load imports the cookies saved at path. A missing file is not an error.
|
||||
func (j *Jar) Load(path string) error {
|
||||
saved, err := readCookies(path)
|
||||
return j.LoadWith(path, nil)
|
||||
}
|
||||
|
||||
// LoadWith is Load through codec, which may be nil.
|
||||
func (j *Jar) LoadWith(path string, codec Codec) error {
|
||||
saved, err := readCookies(path, codec)
|
||||
if err != nil { return err }
|
||||
|
||||
for host, cookies := range saved { j.Import(host, cookies) }
|
||||
@@ -208,11 +251,15 @@ func (j *Jar) Load(path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func readCookies(path string) (map[string][]Cookie, error) {
|
||||
func readCookies(path string, codec Codec) (map[string][]Cookie, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
if errors.Is(err, fs.ErrNotExist) { return nil, nil }
|
||||
if err != nil { return nil, err }
|
||||
|
||||
if codec != nil {
|
||||
if data, err = codec.Decode(data); err != nil { return nil, err }
|
||||
}
|
||||
|
||||
var saved map[string][]Cookie
|
||||
if err := json.Unmarshal(data, &saved); err != nil { return nil, err }
|
||||
|
||||
|
||||
Reference in new issue
Block a user