1use crate::error::AuthError;
4use crate::identity::AuthIdentity;
5use async_trait::async_trait;
6use doido_core::Result;
7use doido_model::sea_orm::DatabaseConnection;
8use http::request::Parts;
9
10#[async_trait]
12pub trait AuthStrategy: Send + Sync {
13 fn name(&self) -> &str;
14
15 async fn authenticate(
16 &self,
17 parts: &Parts,
18 db: &DatabaseConnection,
19 ) -> Result<Option<AuthIdentity>>;
20}
21
22pub fn identity_from_parts(parts: &Parts) -> Option<AuthIdentity> {
24 parts.extensions.get::<AuthIdentity>().cloned()
25}
26
27pub fn require_identity(parts: &Parts) -> Result<AuthIdentity, AuthError> {
29 identity_from_parts(parts).ok_or(AuthError::Unauthorized)
30}