pub struct ApiKeysPlugin { /* private fields */ }Expand description
API Keys plugin. Allows issuing and revoking API keys with scoped permissions.
Keys are stored as SHA-256 hashes only. The raw key is returned exactly once at creation time and is never persisted.
Implementations§
Source§impl ApiKeysPlugin
impl ApiKeysPlugin
pub fn new() -> Self
Sourcepub fn create_key(
&self,
name: &str,
user_id: &str,
scopes: Vec<String>,
) -> CreatedApiKey
pub fn create_key( &self, name: &str, user_id: &str, scopes: Vec<String>, ) -> CreatedApiKey
Create a new API key. Returns a CreatedApiKey containing the raw key.
The raw key is not stored; only its SHA-256 hash is retained.
Sourcepub fn resolve(&self, key: &str) -> Option<AuthContext>
pub fn resolve(&self, key: &str) -> Option<AuthContext>
Resolve an API key to an auth context. Hashes the provided key and performs an O(1) HashMap lookup.
The returned AuthContext is DETACHED from this store — if the key
is later revoked, callers holding the context won’t see the change.
This matters for middleware/session layers that cache the resolved
context across requests. Such callers should also call
[is_active] on every request or re-resolve to pick up
revocations.
Sourcepub fn is_active(&self, key: &str) -> bool
pub fn is_active(&self, key: &str) -> bool
Returns true if the raw key still exists in the store. Use this to
validate a cached AuthContext against the current revocation state
before trusting it on a subsequent request.
Sourcepub fn has_scope(&self, key: &str, scope: &str) -> bool
pub fn has_scope(&self, key: &str, scope: &str) -> bool
Check if an API key has a specific scope.
Trait Implementations§
Source§impl Plugin for ApiKeysPlugin
impl Plugin for ApiKeysPlugin
Source§fn on_request(
&self,
_method: &str,
_path: &str,
_auth: &AuthContext,
) -> Result<(), PluginError>
fn on_request( &self, _method: &str, _path: &str, _auth: &AuthContext, ) -> Result<(), PluginError>
Source§fn on_init(&self, _ctx: &PluginContext)
fn on_init(&self, _ctx: &PluginContext)
Source§fn routes(&self) -> Vec<PluginRoute>
fn routes(&self) -> Vec<PluginRoute>
Source§fn before_insert(
&self,
_entity: &str,
_data: &mut Value,
_auth: &AuthContext,
) -> Result<(), PluginError>
fn before_insert( &self, _entity: &str, _data: &mut Value, _auth: &AuthContext, ) -> Result<(), PluginError>
Source§fn after_insert(
&self,
_entity: &str,
_id: &str,
_data: &Value,
_auth: &AuthContext,
)
fn after_insert( &self, _entity: &str, _id: &str, _data: &Value, _auth: &AuthContext, )
Source§fn before_update(
&self,
_entity: &str,
_id: &str,
_data: &mut Value,
_auth: &AuthContext,
) -> Result<(), PluginError>
fn before_update( &self, _entity: &str, _id: &str, _data: &mut Value, _auth: &AuthContext, ) -> Result<(), PluginError>
Source§fn after_update(
&self,
_entity: &str,
_id: &str,
_data: &Value,
_auth: &AuthContext,
)
fn after_update( &self, _entity: &str, _id: &str, _data: &Value, _auth: &AuthContext, )
Source§fn before_delete(
&self,
_entity: &str,
_id: &str,
_auth: &AuthContext,
) -> Result<(), PluginError>
fn before_delete( &self, _entity: &str, _id: &str, _auth: &AuthContext, ) -> Result<(), PluginError>
Source§fn after_delete(&self, _entity: &str, _id: &str, _auth: &AuthContext)
fn after_delete(&self, _entity: &str, _id: &str, _auth: &AuthContext)
Source§fn on_request_with_meta(
&self,
method: &str,
path: &str,
auth: &AuthContext,
_meta: &RequestMeta<'_>,
) -> Result<(), PluginError>
fn on_request_with_meta( &self, method: &str, path: &str, auth: &AuthContext, _meta: &RequestMeta<'_>, ) -> Result<(), PluginError>
on_request] that also receives per-request
metadata (peer IP today; more fields may be added later). The
default implementation delegates to on_request so existing
plugins keep working without changes. Plugins that care about
IP — notably rate limiting — override this hook.