Skip to main content

ApiKey

Struct ApiKey 

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

Represents an API key with associated metadata and permissions.

§Example

use actix_security::http::security::api_key::ApiKey;

let key = ApiKey::new("sk_live_abc123")
    .name("Production API Key")
    .owner("service-account@example.com")
    .roles(vec!["API_USER".into()])
    .authorities(vec!["api:read".into(), "api:write".into()])
    .expires_in(Duration::from_secs(86400 * 365)); // 1 year

Implementations§

Source§

impl ApiKey

Source

pub fn new(key: impl Into<String>) -> Self

Creates a new API key with the given value.

§Arguments
  • key - The API key string
§Example
let key = ApiKey::new("sk_live_abc123");
Source

pub fn builder(key: impl Into<String>) -> ApiKeyBuilder

Creates a builder for more complex key creation.

Source

pub fn name(self, name: impl Into<String>) -> Self

Sets the human-readable name for this key.

Source

pub fn owner(self, owner: impl Into<String>) -> Self

Sets the owner of this key.

Source

pub fn roles(self, roles: Vec<String>) -> Self

Sets the roles for this key.

Source

pub fn authorities(self, authorities: Vec<String>) -> Self

Sets the authorities for this key.

Source

pub fn enabled(self, enabled: bool) -> Self

Sets whether the key is enabled.

Source

pub fn expires_at(self, expires_at: SystemTime) -> Self

Sets the expiration time.

Source

pub fn expires_in(self, duration: Duration) -> Self

Sets expiration relative to now.

Source

pub fn with_metadata( self, key: impl Into<String>, value: impl Into<String>, ) -> Self

Adds metadata to the key.

Source

pub fn get_key(&self) -> &str

Returns the API key value.

Source

pub fn get_name(&self) -> Option<&str>

Returns the human-readable name.

Source

pub fn get_owner(&self) -> Option<&str>

Returns the owner.

Source

pub fn get_roles(&self) -> &[String]

Returns the roles.

Source

pub fn get_authorities(&self) -> &[String]

Returns the authorities.

Source

pub fn is_enabled(&self) -> bool

Returns whether the key is enabled.

Source

pub fn get_created_at(&self) -> SystemTime

Returns when the key was created.

Source

pub fn get_expires_at(&self) -> Option<SystemTime>

Returns when the key expires.

Source

pub fn get_metadata(&self) -> &HashMap<String, String>

Returns the metadata.

Source

pub fn is_expired(&self) -> bool

Checks if the key has expired.

Source

pub fn is_valid(&self) -> bool

Checks if the key is valid (enabled and not expired).

Source

pub fn has_role(&self, role: &str) -> bool

Checks if the key has the specified role.

Source

pub fn has_any_role(&self, roles: &[&str]) -> bool

Checks if the key has any of the specified roles.

Source

pub fn has_authority(&self, authority: &str) -> bool

Checks if the key has the specified authority.

Source

pub fn has_any_authority(&self, authorities: &[&str]) -> bool

Checks if the key has any of the specified authorities.

Trait Implementations§

Source§

impl Clone for ApiKey

Source§

fn clone(&self) -> ApiKey

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 ApiKey

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for ApiKey

§

impl RefUnwindSafe for ApiKey

§

impl Send for ApiKey

§

impl Sync for ApiKey

§

impl Unpin for ApiKey

§

impl UnwindSafe for ApiKey

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.

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

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

Source§

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

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

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

Source§

fn vzip(self) -> V

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