mirror of
https://github.com/BerriAI/litellm.git
synced 2026-12-01 01:18:34 +00:00
* refactor(rust): move SecretValue into litellm-secret-value * docs(rust): add rust-redaction skill * refactor(redis-client): carry credentials as SecretValue --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
48 lines
1.2 KiB
Rust
48 lines
1.2 KiB
Rust
use std::{future::Future, pin::Pin, time::SystemTime};
|
|
|
|
use litellm_secret_value::SecretValue;
|
|
|
|
use crate::{Error, hash_token};
|
|
|
|
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
|
pub struct KeyHash(String);
|
|
|
|
impl KeyHash {
|
|
pub fn from_token(token: &SecretValue) -> Self {
|
|
Self(hash_token(token.expose()))
|
|
}
|
|
|
|
pub fn as_str(&self) -> &str {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub enum KeyStatus {
|
|
Active { expires_at: Option<SystemTime> },
|
|
Revoked,
|
|
}
|
|
|
|
pub type LookupFuture<'a> =
|
|
Pin<Box<dyn Future<Output = Result<Option<KeyStatus>, Error>> + Send + 'a>>;
|
|
|
|
pub trait KeyLookup: Send + Sync {
|
|
fn lookup<'a>(&'a self, hash: &'a KeyHash) -> LookupFuture<'a>;
|
|
}
|
|
|
|
pub async fn verify_key(
|
|
lookup: &dyn KeyLookup,
|
|
token: &SecretValue,
|
|
now: impl FnOnce() -> SystemTime,
|
|
) -> Result<KeyHash, Error> {
|
|
let hash = KeyHash::from_token(token);
|
|
match lookup.lookup(&hash).await? {
|
|
Some(KeyStatus::Active { expires_at }) => {
|
|
if expires_at.is_some_and(|expiry| expiry <= now()) {
|
|
return Err(Error::Expired);
|
|
}
|
|
Ok(hash)
|
|
}
|
|
Some(KeyStatus::Revoked) | None => Err(Error::InvalidToken),
|
|
}
|
|
}
|