Skip to main content

ApiKeyAuthenticator

Struct ApiKeyAuthenticator 

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

An opaque API-key → Principal map with revocation: the X-API-Key counterpart to TokenAuthenticator, sitting behind the same Authenticator trait so a transport treats it like any other bearer path.

An API key is a long-lived opaque secret a service presents instead of an interactive OIDC/JWT login — never a JWT (it carries no claims; the Principal and its scopes are what the key was registered with). The two mechanisms run side by side: seiva-http’s middleware reads X-API-Key against this, and Authorization: Bearer against the JWT path.

§Hash at rest, revocation

A key is stored only as its SHA-256 digest, exactly like TokenAuthenticator — the clear secret never sits in process memory, and the digest-keyed lookup leaks only pre-image-resistant bytes under a timing side-channel. revoke drops a key by its digest; a revoked or never-registered key authenticates to nothing (InvalidToken), so rejection is the default. An empty map denies every request — zero trust out of the box.

Implementations§

Source§

impl ApiKeyAuthenticator

Source

pub fn new() -> ApiKeyAuthenticator

A new, empty authenticator. On its own it rejects every key.

Source

pub fn with_key( self, key: impl AsRef<str>, principal: Principal, ) -> ApiKeyAuthenticator

Register one key → principal (the scopes the key grants), builder-style. The key is stored as its SHA-256 digest, never in clear.

Source

pub fn insert(&mut self, key: impl AsRef<str>, principal: Principal)

Register one key → principal in place (stored as a SHA-256 digest).

Source

pub fn revoke(&mut self, key: impl AsRef<str>) -> bool

Revoke key: after this it authenticates to nothing (InvalidToken). Returns true if the key was registered (and is now gone), false if it was not known. Keyed on the digest, so the clear key is never compared.

Source

pub fn len(&self) -> usize

How many keys are registered. Mostly for tests and health checks.

Source

pub fn is_empty(&self) -> bool

Whether no keys are registered (so every request is denied).

Trait Implementations§

Source§

impl Authenticator for ApiKeyAuthenticator

Source§

fn authenticate(&self, key: &str) -> Result<Principal, AuthError>

Authenticate bearer (the raw token, the Bearer scheme already stripped). Return the Principal on success, or the reason it failed.
Source§

impl Clone for ApiKeyAuthenticator

Source§

fn clone(&self) -> ApiKeyAuthenticator

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 Default for ApiKeyAuthenticator

Source§

fn default() -> ApiKeyAuthenticator

Returns the “default value” for a type. 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> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

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

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

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

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

fn try_from(value: U) -> Result<T, !>

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

Source§

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

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

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