pub struct CsrfMiddleware { /* private fields */ }Expand description
Better Auth request protection middleware.
This mirrors the upstream TypeScript behavior:
- mutating requests with cookies require a trusted
Origin/Referer - first-login
POST /sign-up/emailandPOST /sign-in/emailalso use Fetch Metadata headers to block cross-site navigation attacks - callback / redirect targets are validated against trusted origins unless
advanced.disable_origin_checkis set
Implementations§
Source§impl CsrfMiddleware
impl CsrfMiddleware
pub const fn new(config: CsrfConfig, auth_config: Arc<AuthConfig>) -> Self
Sourcepub fn with_auth_config(&self, auth_config: Arc<AuthConfig>) -> Self
pub fn with_auth_config(&self, auth_config: Arc<AuthConfig>) -> Self
Retain protection settings with the effective request-local URL policy.
Sourcepub fn check_request_origin(&self, req: &AuthRequest) -> AuthResult<()>
pub fn check_request_origin(&self, req: &AuthRequest) -> AuthResult<()>
Router protection after route/body resolution and before application hooks.
§Errors
Rejects untrusted request origins and redirect targets.
Sourcepub fn check_form_origin(&self, req: &AuthRequest) -> AuthResult<()>
pub fn check_form_origin(&self, req: &AuthRequest) -> AuthResult<()>
Endpoint-local first-login protection, after application before hooks.
§Errors
Rejects cross-site navigation or untrusted first-login origins.
Sourcepub fn check_login_origin(&self, req: &AuthRequest) -> AuthResult<()>
pub fn check_login_origin(&self, req: &AuthRequest) -> AuthResult<()>
Check an explicitly selected login operation using its genuine incoming request. The request URL need not be an auth route for a public server API call.
§Errors
Rejects cross-site navigation or untrusted login origins.
Trait Implementations§
Source§impl Debug for CsrfMiddleware
impl Debug for CsrfMiddleware
Source§impl Middleware for CsrfMiddleware
impl Middleware for CsrfMiddleware
Source§fn before_request<'life0, 'life1, 'async_trait>(
&'life0 self,
req: &'life1 AuthRequest,
) -> Pin<Box<dyn Future<Output = AuthResult<Option<AuthResponse>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn before_request<'life0, 'life1, 'async_trait>(
&'life0 self,
req: &'life1 AuthRequest,
) -> Pin<Box<dyn Future<Output = AuthResult<Option<AuthResponse>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Called before the request is dispatched to plugins. Read more
Source§fn after_request<'life0, 'life1, 'async_trait>(
&'life0 self,
_req: &'life1 AuthRequest,
response: AuthResponse,
) -> Pin<Box<dyn Future<Output = AuthResult<AuthResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn after_request<'life0, 'life1, 'async_trait>(
&'life0 self,
_req: &'life1 AuthRequest,
response: AuthResponse,
) -> Pin<Box<dyn Future<Output = AuthResult<AuthResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Called after a response has been produced. Read more
Auto Trait Implementations§
impl !RefUnwindSafe for CsrfMiddleware
impl !UnwindSafe for CsrfMiddleware
impl Freeze for CsrfMiddleware
impl Send for CsrfMiddleware
impl Sync for CsrfMiddleware
impl Unpin for CsrfMiddleware
impl UnsafeUnpin for CsrfMiddleware
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more