Skip to main content

ImpersonationToken

Struct ImpersonationToken 

Source
pub struct ImpersonationToken { /* private fields */ }
Expand description

An owned, immutable snapshot of a Windows impersonation context.

Cloning this value shares ownership of the same captured token. The native handle is real rather than pseudo or borrowed, is not inheritable, and has only TOKEN_IMPERSONATE access. No safe API exposes that handle or permits callers to mutate the captured token. The value is safe to send to and share between threads; each call to Self::with_impersonation affects only the calling thread for the duration of its closure.

Implementations§

Source§

impl ImpersonationToken

Source

pub fn capture() -> Result<ImpersonationToken, CaptureError>

Captures the calling thread’s effective Windows security context.

If the thread is impersonating, capture preserves its identification, impersonation, or delegation level. If the thread has no token, capture snapshots the process token as a SecurityImpersonation token. The captured handle is non-inheritable and grants only TOKEN_IMPERSONATE.

§Errors

Returns a CaptureError synchronously when the effective token cannot be opened, inspected, or duplicated. Anonymous impersonation is reported as CaptureFailure::AnonymousContext.

Source

pub fn with_impersonation<F, T>(&self, operation: F) -> Result<T, ApplyError>
where F: FnOnce() -> T,

Runs operation with this token applied to the current thread.

The exact prior thread-token state is restored before this method returns, whether operation returns an ordinary value or a Result. Stack unwinding also restores the prior state. The closure’s return value is not interpreted, so a fallible closure produces Result<Result<T, E>, ApplyError>.

This method changes only the calling thread’s impersonation state. The token may be reused sequentially or concurrently on other threads.

§Errors

Returns an ApplyError before calling operation when the exact prior context cannot be saved or the captured token cannot be applied.

§Panics

Panics if restoring the exact prior thread-token state fails. Returning a shared worker thread under an unknown identity would permit unrelated later work to run with the wrong security context. If restoration fails while operation is already unwinding, Rust’s double-panic behavior aborts the process.

Trait Implementations§

Source§

impl Clone for ImpersonationToken

Source§

fn clone(&self) -> ImpersonationToken

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for ImpersonationToken

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> 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> 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 = !

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.