| index adb9700ed..1e90747f8 100644 |
| --- a/authpending.go |
| +++ b/authpending.go |
| @@ -40,8 +40,7 @@ func pendingLoginFile(deviceCode string) string { |
| } |
| |
| // savePendingLogin writes this attempt's own file, 0600 through a temp file |
| -// in the same directory, then removes every other attempt's file: the newest |
| -// --no-wait wins. The device code is a secret until the login is claimed. |
| +// in the same directory, and deletes no other live attempt: several can sit side by side. The device code is a secret until the login is claimed. |
| func savePendingLogin(p pendingLogin) error { |
| dir := pendingLoginPath() |
| if err := os.MkdirAll(dir, 0o700); err != nil { |
| @@ -68,43 +67,34 @@ func savePendingLogin(p pendingLogin) error { |
| if err := tmp.Close(); err != nil { |
| return err |
| } |
| - own := pendingLoginFile(p.DeviceCode) |
| - if err := os.Rename(tmp.Name(), own); err != nil { |
| + if err := os.Rename(tmp.Name(), pendingLoginFile(p.DeviceCode)); err != nil { |
| return err |
| } |
| - entries, err := os.ReadDir(dir) |
| - if err != nil { |
| - return err |
| - } |
| - for _, e := range entries { |
| - path := filepath.Join(dir, e.Name()) |
| - if path == own || !strings.HasSuffix(e.Name(), ".json") { |
| - continue |
| - } |
| - if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) { |
| - return err |
| - } |
| - } |
| - return nil |
| + // Nothing else is touched except attempts that have already expired. |
| + _, err = readPendingLogins(true) |
| + return err |
| } |
| |
| -// loadPendingLogin returns the newest attempt (latest ExpiresAt), or nil, nil |
| -// when none is waiting. Expired attempts are still returned: --wait turns |
| -// one into the "expired" answer and removes it by its exact path. |
| -func loadPendingLogin() (*pendingLogin, error) { |
| - entries, err := os.ReadDir(pendingLoginPath()) |
| +// readPendingLogins lists every attempt file. With sweep set it also removes |
| +// the expired ones, by exact path: an expired attempt is dead, so anyone may. |
| +// It never removes an unexpired file. The result holds unexpired attempts only. |
| +func readPendingLogins(sweep bool) ([]pendingLogin, error) { |
| + dir := pendingLoginPath() |
| + entries, err := os.ReadDir(dir) |
| if errors.Is(err, os.ErrNotExist) { |
| return nil, nil |
| } |
| if err != nil { |
| return nil, err |
| } |
| - var best *pendingLogin |
| + now := time.Now() |
| + var live []pendingLogin |
| for _, e := range entries { |
| if !strings.HasSuffix(e.Name(), ".json") { |
| continue |
| } |
| - raw, err := os.ReadFile(filepath.Join(pendingLoginPath(), e.Name())) |
| + path := filepath.Join(dir, e.Name()) |
| + raw, err := os.ReadFile(path) |
| if errors.Is(err, os.ErrNotExist) { |
| continue |
| } |
| @@ -113,20 +103,41 @@ func loadPendingLogin() (*pendingLogin, error) { |
| } |
| var p pendingLogin |
| if err := json.Unmarshal(raw, &p); err != nil { |
| - return nil, errors.New(filepath.Join(pendingLoginPath(), e.Name()) + " is not readable as a pending login; delete it and run carlos auth login --no-wait again") |
| + return nil, errors.New(path + " is not readable as a pending login; delete it and run carlos auth login --no-wait again") |
| } |
| - if best == nil || p.ExpiresAt.After(best.ExpiresAt) { |
| - cp := p |
| - best = &cp |
| + if !p.ExpiresAt.IsZero() && !now.Before(p.ExpiresAt) { |
| + if sweep { |
| + if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) { |
| + return nil, err |
| + } |
| + } |
| + continue |
| + } |
| + live = append(live, p) |
| + } |
| + return live, nil |
| +} |
| + |
| +// loadPendingLogin returns the newest unexpired attempt (latest ExpiresAt), |
| +// or nil, nil when none is waiting. Expired files met on the way are removed. |
| +func loadPendingLogin() (*pendingLogin, error) { |
| + live, err := readPendingLogins(true) |
| + if err != nil || len(live) == 0 { |
| + return nil, err |
| + } |
| + best := live[0] |
| + for _, p := range live[1:] { |
| + if p.ExpiresAt.After(best.ExpiresAt) { |
| + best = p |
| } |
| } |
| - return best, nil |
| + return &best, nil |
| } |
| |
| // clearPendingLoginIf removes the file for exactly this attempt, by its own |
| // path. It never reads or removes another attempt's file, so a --wait that |
| -// finishes late cannot delete the login a newer --no-wait started. The name |
| -// is kept from the single-file version; the "if" is now the path itself. |
| +// finishes late cannot delete a login a newer --no-wait started. The name is |
| +// kept from the single-file version; the "if" is now the path itself. |
| func clearPendingLoginIf(deviceCode string) error { |
| if err := os.Remove(pendingLoginFile(deviceCode)); err != nil && !errors.Is(err, os.ErrNotExist) { |
| return err |