118 lines
2.7 KiB
Go
118 lines
2.7 KiB
Go
// Package accounts keeps the IMVU accounts' passwords the importer was given: each still sealed for its key, so
|
|
// the file alone (without the key) reveals nothing. The hub drops its copy once the importer says it has one.
|
|
package accounts
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// FileName is the store's file in the data folder.
|
|
const FileName = "accounts.json"
|
|
|
|
// Account is a kept account.
|
|
type Account struct {
|
|
ID string `json:"id"` // the hub's
|
|
IMVUID int64 `json:"imvuId"`
|
|
Username string `json:"username"`
|
|
Sealed string `json:"sealed"`
|
|
// When IMVU last refused the sign-in with this password (zero: never): it isn't tried again for a while.
|
|
RefusedAt time.Time `json:"refusedAt,omitzero"`
|
|
}
|
|
|
|
// Store is the accounts file.
|
|
type Store struct {
|
|
path string
|
|
mu sync.Mutex
|
|
byID map[string]Account
|
|
}
|
|
|
|
// Open reads the store in dir (empty if there's none yet).
|
|
func Open(dir string) (*Store, error) {
|
|
s := &Store{path: filepath.Join(dir, FileName), byID: map[string]Account{}}
|
|
data, err := os.ReadFile(s.path)
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
return s, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var list []Account
|
|
if err := json.Unmarshal(data, &list); err != nil {
|
|
return nil, err
|
|
}
|
|
for _, a := range list {
|
|
s.byID[a.ID] = a
|
|
}
|
|
return s, nil
|
|
}
|
|
|
|
// Get returns the account with the hub's ID id.
|
|
func (s *Store) Get(id string) (Account, bool) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
a, ok := s.byID[id]
|
|
return a, ok
|
|
}
|
|
|
|
// Put keeps an account (replacing its password, if it had one; a new password may be tried at once).
|
|
func (s *Store) Put(a Account) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
s.byID[a.ID] = a
|
|
return s.save()
|
|
}
|
|
|
|
// Refused records that IMVU refused the account's sign-in (with its current password).
|
|
func (s *Store) Refused(id string, at time.Time) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
a, ok := s.byID[id]
|
|
if !ok {
|
|
return nil
|
|
}
|
|
a.RefusedAt = at
|
|
s.byID[id] = a
|
|
return s.save()
|
|
}
|
|
|
|
// Keep forgets every account not in ids (the hub no longer lists them) and returns how many it forgot.
|
|
func (s *Store) Keep(ids map[string]bool) (int, error) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
n := 0
|
|
for id := range s.byID {
|
|
if !ids[id] {
|
|
delete(s.byID, id)
|
|
n++
|
|
}
|
|
}
|
|
if n == 0 {
|
|
return 0, nil
|
|
}
|
|
return n, s.save()
|
|
}
|
|
|
|
func (s *Store) save() error {
|
|
list := make([]Account, 0, len(s.byID))
|
|
for _, a := range s.byID {
|
|
list = append(list, a)
|
|
}
|
|
data, err := json.MarshalIndent(list, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := os.MkdirAll(filepath.Dir(s.path), 0o700); err != nil {
|
|
return err
|
|
}
|
|
tmp := s.path + ".tmp"
|
|
if err := os.WriteFile(tmp, data, 0o600); err != nil {
|
|
return err
|
|
}
|
|
return os.Rename(tmp, s.path)
|
|
}
|