pub struct Token(/* private fields */);Expand description
A bearer token for the control API.
Deliberately neither Debug nor Display. A token that can be formatted is a token
that ends up in a log line eventually; the only way out is Token::as_str, which
reads as the deliberate act it is.
Implementations§
Source§impl Token
impl Token
pub fn generate() -> Result<Self>
Sourcepub fn from_hex(s: &str) -> Self
pub fn from_hex(s: &str) -> Self
Reconstruct a token generated elsewhere, such as one read back from disk.
pub fn as_str(&self) -> &str
Sourcepub fn matches(&self, presented: &str) -> bool
pub fn matches(&self, presented: &str) -> bool
Compare in constant time.
A short-circuiting == leaks the token one byte at a time to anything that can
time the response, and on loopback that is every process on the machine. The
length is allowed to leak because it is a compile-time constant.
Sourcepub fn write_to_disk(&self) -> Option<PathBuf>
pub fn write_to_disk(&self) -> Option<PathBuf>
Write the token where a local tool can find it, returning where it went.
Best effort. A daemon that cannot write the file still works, because the token is printed at startup as well, and refusing to start over this would be worse than the inconvenience it avoids.
Sourcepub fn load_or_generate(rotate: bool) -> Result<(Self, Source)>
pub fn load_or_generate(rotate: bool) -> Result<(Self, Source)>
The token this run will use: last run’s, or a new one written down.
Reused by default, because the alternative is what this did before and it made the extension unusable. A fresh token every restart means pasting sixty-four characters into a popup every time the daemon comes back — and the token was already being written to disk, so regenerating took the risk of keeping it there and discarded the only thing that risk buys.
rotate mints a new one anyway, which is what to reach for if the old one leaked.