pub struct PairingGuard { /* private fields */ }Expand description
Manages pairing state for the gateway.
Bearer tokens are stored as SHA-256 hashes to prevent plaintext exposure in config files. When a new token is generated, the plaintext is returned to the client once, and only the hash is retained.
Implementations§
Source§impl PairingGuard
impl PairingGuard
Sourcepub fn new(require_pairing: bool, existing_tokens: &[String]) -> Self
pub fn new(require_pairing: bool, existing_tokens: &[String]) -> Self
Create a new pairing guard.
If require_pairing is true and no tokens exist yet, a fresh
pairing code is generated and printed to the terminal. Once
paired, no code is generated on restart — operators can use
generate_new_pairing_code() or the CLI to create one on demand.
Existing tokens are accepted in both forms:
- Plaintext (
zc_...): hashed on load for backward compatibility - Already hashed (64-char hex): stored as-is
Sourcepub fn pairing_code(&self) -> Option<String>
pub fn pairing_code(&self) -> Option<String>
The one-time pairing code (generated only on first startup when no tokens exist).
Sourcepub fn require_pairing(&self) -> bool
pub fn require_pairing(&self) -> bool
Whether pairing is required at all.
Sourcepub async fn try_pair(
&self,
code: &str,
client_id: &str,
) -> Result<Option<String>, u64>
pub async fn try_pair( &self, code: &str, client_id: &str, ) -> Result<Option<String>, u64>
Attempt to pair with the given code. Returns a bearer token on success.
Returns Err(lockout_seconds) if locked out due to brute force.
client_id identifies the client for per-client lockout accounting.
Sourcepub fn is_authenticated(&self, token: &str) -> bool
pub fn is_authenticated(&self, token: &str) -> bool
Check if a bearer token is valid (compares against stored hashes).
Sourcepub fn is_paired(&self) -> bool
pub fn is_paired(&self) -> bool
Returns true if the gateway is already paired (has at least one token).
Sourcepub fn revoke_token(&self, token: &str) -> bool
pub fn revoke_token(&self, token: &str) -> bool
Revoke a paired token by plaintext. Returns true if removed.
Test/convenience wrapper that hashes the plaintext, then defers to
revoke_token_hash. Production revoke paths
already hold the hash (the device registry stores it) and should call
revoke_token_hash directly rather than re-hashing the plaintext.
In-memory only; the caller must persist tokens() to config or a
restart will resurrect the token from disk.
Sourcepub fn revoke_token_hash(&self, token_hash: &str) -> bool
pub fn revoke_token_hash(&self, token_hash: &str) -> bool
Revoke a paired token by its SHA-256 hash. Returns true if removed.
Sourcepub fn revoke_all_tokens(&self) -> usize
pub fn revoke_all_tokens(&self) -> usize
Revoke every paired token at once. Returns the number of tokens
invalidated. This is the “rotate after compromise — nuke everything”
path: when an operator does not know which token leaked, the only safe
action is to invalidate all of them and force every client to re-pair.
The caller must persist tokens() to config so a daemon restart does
not resurrect the revoked set.
Sourcepub fn generate_new_pairing_code(&self) -> Option<String>
pub fn generate_new_pairing_code(&self) -> Option<String>
Generate a new pairing code that pairs an additional client.
Does not revoke existing tokens. To rotate a compromised token,
pair with revoke_token/revoke_token_hash + a config persist pass.
Sourcepub fn generate_pairing_code_if_vacant(
&self,
) -> Result<String, GeneratePairingCodeError>
pub fn generate_pairing_code_if_vacant( &self, ) -> Result<String, GeneratePairingCodeError>
Generate a new pairing code only when no code is already pending.
Returns Ok(code) on success, Err(GeneratePairingCodeError::Pending)
when the slot is already occupied, and
Err(GeneratePairingCodeError::PairingDisabled) when pairing is off.
The check + write is atomic — concurrent callers cannot both observe
the slot vacant and then both write into it.
Sourcepub fn token_hash(token: &str) -> String
pub fn token_hash(token: &str) -> String
Get the token hash for a given plaintext token (for device registry lookup).
Sourcepub fn authenticate_and_hash(&self, token: &str) -> Option<String>
pub fn authenticate_and_hash(&self, token: &str) -> Option<String>
Check if a token is paired and return its hash.
Trait Implementations§
Source§impl Clone for PairingGuard
impl Clone for PairingGuard
Source§fn clone(&self) -> PairingGuard
fn clone(&self) -> PairingGuard
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for PairingGuard
impl !RefUnwindSafe for PairingGuard
impl Send for PairingGuard
impl Sync for PairingGuard
impl Unpin for PairingGuard
impl UnsafeUnpin for PairingGuard
impl !UnwindSafe for PairingGuard
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more