Skip to main content

SessionToken

Struct SessionToken 

Source
pub struct SessionToken(/* private fields */);
Expand description

A 256-bit random session token for socket authentication.

Implementations§

Source§

impl SessionToken

Source

pub fn generate() -> Self

Generate a new random session token from the OS CSPRNG.

Source

pub fn to_hex(&self) -> String

Hex-encode the token for file storage and wire transmission.

Source

pub fn from_hex(hex: &str) -> Result<Self, Error>

Decode a hex-encoded token string.

§Errors

Returns an error if the hex string is not exactly 64 characters or contains invalid hex digits.

Source

pub fn write_to_file(&self, path: &Path) -> Result<()>

Write the token to a file with owner-only permissions (0o600).

On Unix, this uses write-then-rename atomicity: writes to a temporary file at 0o600 (via OpenOptions::mode to avoid a TOCTOU permissions window), then atomically renames it to the target path. This prevents a racing read_from_file from seeing a truncated/empty file during daemon restarts.

§Errors

Returns an error if the file cannot be written or permissions cannot be set.

Source

pub fn read_from_file(path: &Path) -> Result<Self>

Read and decode a token from a file.

§Errors

Returns an error if the file cannot be read or contains invalid hex.

Source

pub fn ct_eq(&self, other: &Self) -> bool

Constant-time comparison. Returns true if the tokens are equal.

Trait Implementations§

Source§

impl Debug for SessionToken

Source§

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

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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.
Source§

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

Source§

fn vzip(self) -> V