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 yearImplementations§
Source§impl ApiKey
impl ApiKey
Sourcepub fn builder(key: impl Into<String>) -> ApiKeyBuilder
pub fn builder(key: impl Into<String>) -> ApiKeyBuilder
Creates a builder for more complex key creation.
Sets the authorities for this key.
Sourcepub fn expires_at(self, expires_at: SystemTime) -> Self
pub fn expires_at(self, expires_at: SystemTime) -> Self
Sets the expiration time.
Sourcepub fn expires_in(self, duration: Duration) -> Self
pub fn expires_in(self, duration: Duration) -> Self
Sets expiration relative to now.
Sourcepub fn with_metadata(
self,
key: impl Into<String>,
value: impl Into<String>,
) -> Self
pub fn with_metadata( self, key: impl Into<String>, value: impl Into<String>, ) -> Self
Adds metadata to the key.
Returns the authorities.
Sourcepub fn is_enabled(&self) -> bool
pub fn is_enabled(&self) -> bool
Returns whether the key is enabled.
Sourcepub fn get_created_at(&self) -> SystemTime
pub fn get_created_at(&self) -> SystemTime
Returns when the key was created.
Sourcepub fn get_expires_at(&self) -> Option<SystemTime>
pub fn get_expires_at(&self) -> Option<SystemTime>
Returns when the key expires.
Sourcepub fn get_metadata(&self) -> &HashMap<String, String>
pub fn get_metadata(&self) -> &HashMap<String, String>
Returns the metadata.
Sourcepub fn is_expired(&self) -> bool
pub fn is_expired(&self) -> bool
Checks if the key has expired.
Sourcepub fn has_any_role(&self, roles: &[&str]) -> bool
pub fn has_any_role(&self, roles: &[&str]) -> bool
Checks if the key has any of the specified roles.
Checks if the key has the specified authority.
Checks if the key has any of the specified authorities.
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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