Skip to main content

PairingGuard

Struct PairingGuard 

Source
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

Source

pub fn new(require_pairing: bool, existing_tokens: &[String]) -> PairingGuard

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
Source

pub fn pairing_code(&self) -> Option<String>

The one-time pairing code (generated only on first startup when no tokens exist).

Source

pub fn require_pairing(&self) -> bool

Whether pairing is required at all.

Source

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.

Source

pub fn is_authenticated(&self, token: &str) -> bool

Check if a bearer token is valid (compares against stored hashes).

Source

pub fn is_paired(&self) -> bool

Returns true if the gateway is already paired (has at least one token).

Source

pub fn tokens(&self) -> Vec<String>

Get all paired token hashes (for persisting to config).

Source

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.

Source

pub fn revoke_token_hash(&self, token_hash: &str) -> bool

Revoke a paired token by its SHA-256 hash. Returns true if removed.

Source

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.

Source

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.

Source

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.

Source

pub fn token_hash(token: &str) -> String

Get the token hash for a given plaintext token (for device registry lookup).

Source

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

Source§

fn clone(&self) -> PairingGuard

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PairingGuard

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,