use std::future::Future;
use crate::wire::Value;
use crate::server::session::Session;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Credentials {
ApiKey(String),
UserPass(String, String),
Token(String),
None,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Principal<I = ()> {
pub name: String,
pub identity: I,
}
impl Principal {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
identity: (),
}
}
}
impl<I> Principal<I> {
pub fn with_identity(name: impl Into<String>, identity: I) -> Self {
Self {
name: name.into(),
identity,
}
}
}
#[derive(Debug, thiserror::Error)]
pub enum AuthError {
#[error("invalid credentials")]
InvalidCredentials,
#[error("{0}")]
Message(String),
}
pub trait Dispatch: Send + Sync + 'static {
type Identity: Send + Sync + 'static;
fn dispatch(
&self,
session: &Session<Self::Identity>,
command: &str,
args: Vec<Value>,
) -> impl Future<Output = Result<Value, String>> + Send;
fn authenticate(
&self,
creds: Credentials,
) -> impl Future<Output = Result<Principal<Self::Identity>, AuthError>> + Send;
fn capabilities(&self, principal: &Principal<Self::Identity>) -> Vec<String> {
let _ = principal;
vec![]
}
}