nestrs_core/strategy.rs
1//! Authentication strategy primitives (NestJS Passport-style analogue).
2
3use axum::http::request::Parts;
4
5/// Error produced by [`AuthStrategy::validate`].
6#[derive(Debug, Clone)]
7pub struct AuthError {
8 pub message: String,
9}
10
11impl AuthError {
12 pub fn unauthorized(message: impl Into<String>) -> Self {
13 Self {
14 message: message.into(),
15 }
16 }
17}
18
19/// Auth strategy contract similar to Nest's Passport strategy adapters.
20///
21/// A guard can delegate token/header parsing + claim validation to a strategy and map failures
22/// to `GuardError::Unauthorized`.
23#[async_trait::async_trait]
24pub trait AuthStrategy: Send + Sync + 'static {
25 type Payload: Send + Sync + 'static;
26
27 async fn validate(&self, parts: &Parts) -> Result<Self::Payload, AuthError>;
28}