Skip to content
Repos

DevPlaceApp

C++ 1.4 KB37 lines Opens in site

Directory Raw Download

#pragma once
// Spec: shell.md section 5 (UserSessionStore). DIFF: Keychain (whenUnlockedThisDeviceOnly) -> session.json in the
// SDL pref path, mode 0600, atomic write, max 16 KiB; the secret is plaintext on disk, protected by file mode only.
// Setting nullopt removes the key. The password is kept so the token can be silently re-obtained.
#include <optional>
#include <string>

#include "core/Observable.hpp"

namespace dp {

class SessionStore : public Observable {
 public:
  static SessionStore& instance();  // pref path
  explicit SessionStore(std::string directory);

  std::optional<std::string> email() const { return email_; }
  std::optional<std::string> password() const { return password_; }
  // false (and a log line "Error: failed to store <key> in session file") when persisting failed.
  bool setEmail(const std::optional<std::string>& value);
  bool setPassword(const std::optional<std::string>& value);

  // Switches the backing directory (tests, profiles) and reloads from it. Empty = memory only.
  void rebind(std::string directory);
  void reload();
  const std::string& filePath() const { return path_; }

 private:
  bool set(std::optional<std::string>& slot, const std::optional<std::string>& value, const char* key);
  bool save();

  std::string path_;
  std::optional<std::string> email_;
  std::optional<std::string> password_;
};

}  // namespace dp
45.2ms