pub struct ApiKeys { /* private fields */ }Expand description
The API key plugin: .plugin(ApiKeys::new().default_prefix("ak_")).
Defaults match Better Auth: 64-character keys with no prefix, the first 6 characters
stored as start, no default expiry, expiresIn between 1 and 365 days, metadata off,
a rate limit of 10 verifications per day per key, and no sessions from keys until
.sessions(true).
Implementations§
Source§impl ApiKeys
impl ApiKeys
pub fn new() -> Self
Sourcepub fn key_length(self, length: usize) -> Self
pub fn key_length(self, length: usize) -> Self
Random characters per key, after the prefix (default 64). Keys shorter than this are
refused by the session resolver with 403 INVALID_API_KEY before any lookup.
Sourcepub fn default_prefix(self, prefix: impl Into<String>) -> Self
pub fn default_prefix(self, prefix: impl Into<String>) -> Self
The prefix keys start with unless the create request names one, e.g. "ak_".
Sourcepub fn start_length(self, length: usize) -> Self
pub fn start_length(self, length: usize) -> Self
How many leading characters of each key are stored as start (default 6; 0 stores
none).
Sourcepub fn default_expires_in(self, ttl: Duration) -> Self
pub fn default_expires_in(self, ttl: Duration) -> Self
The expiry of keys created without expiresIn (default: never).
Sourcepub fn min_expires_in(self, min: Duration) -> Self
pub fn min_expires_in(self, min: Duration) -> Self
The smallest expiresIn a create or update may ask for (default 1 day).
Sourcepub fn max_expires_in(self, max: Duration) -> Self
pub fn max_expires_in(self, max: Duration) -> Self
The largest expiresIn a create or update may ask for (default 365 days).
Sourcepub fn enable_metadata(self, enabled: bool) -> Self
pub fn enable_metadata(self, enabled: bool) -> Self
Lets keys carry a JSON object of metadata (default off).
Sourcepub fn require_name(self, required: bool) -> Self
pub fn require_name(self, required: bool) -> Self
Refuses creates without a name with NAME_REQUIRED (default off).
Sourcepub fn default_permissions(self, permissions: Permissions) -> Self
pub fn default_permissions(self, permissions: Permissions) -> Self
The permissions keys created without permissions get.
Sourcepub fn rate_limit(self, window: Duration, max: i64) -> Self
pub fn rate_limit(self, window: Duration, max: i64) -> Self
The per-key rate limit new keys get: max verifications per window
(default 10 per day). The server API can set other values per key.
Sourcepub fn disable_rate_limit(self) -> Self
pub fn disable_rate_limit(self) -> Self
Turns per-key rate limiting off for every key, whatever the key says.
Sourcepub fn header(self, name: HeaderName) -> Self
pub fn header(self, name: HeaderName) -> Self
The header the session resolver reads keys from (default x-api-key).
Sourcepub fn key_getter(
self,
getter: impl Fn(&HeaderMap) -> Option<String> + Send + Sync + 'static,
) -> Self
pub fn key_getter( self, getter: impl Fn(&HeaderMap) -> Option<String> + Send + Sync + 'static, ) -> Self
Reads the key from the request some other way (e.g. Authorization: Bearer).
Replaces the header lookup.
Sourcepub fn sessions(self, enabled: bool) -> Self
pub fn sessions(self, enabled: bool) -> Self
Whether requests carrying a key get a session for its owner, like Better Auth’s
enableSessionForAPIKeys (default off). Off, keys are only checked through
auth.api_keys().verify(..). On, every such request is a verification and counts
against the key’s rate limit and remaining.
Trait Implementations§
Source§impl Plugin for ApiKeys
impl Plugin for ApiKeys
Source§const ID: &'static str = "api-key"
const ID: &'static str = "api-key"
Source§type Requires = ()
type Requires = ()
(), (Organization,).Source§type UserFields = ()
type UserFields = ()
user; () for none.Source§type SessionFields = ()
type SessionFields = ()
session; () for none.Source§fn schema(&self, schema: &mut SchemaBuilder)
fn schema(&self, schema: &mut SchemaBuilder)
schema.model::<M>()) or columns. UserFields and SessionFields are
added automatically.