Skip to main content

dioxus_clerk/core/
verification.rs

1//! Server-side credential verification outcome shared across crates.
2
3use super::ClerkAuth;
4
5/// Result of checking request credentials before handlers or server functions run.
6#[derive(Debug, Clone, PartialEq, Eq)]
7#[must_use = "a verification outcome carries the auth decision; dropping it silently skips the check"]
8#[non_exhaustive]
9pub enum VerificationOutcome {
10    /// No bearer credentials or session cookie were present on the request.
11    Missing,
12    /// Credentials were present and verified.
13    Valid(ClerkAuth),
14    /// Credentials were present but invalid.
15    Invalid(InvalidTokenReason),
16    /// Verification infrastructure was unavailable. Server middleware fails closed.
17    Unavailable,
18}
19
20/// Why a presented token failed verification.
21///
22/// Only reasons an application can meaningfully act on are distinguished;
23/// everything else (malformed token, bad signature, claim mismatch) is
24/// [`InvalidTokenReason::Other`] so failure details never leak to clients.
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
26#[non_exhaustive]
27pub enum InvalidTokenReason {
28    /// The token is past its `exp` claim; the client should refresh its session.
29    Expired,
30    /// The token's `nbf`/`iat` claim is in the future beyond the accepted clock skew.
31    NotYetValid,
32    /// Any other verification failure.
33    Other,
34}
35
36impl VerificationOutcome {
37    /// Returns verified auth claims only for a valid outcome.
38    #[must_use]
39    pub fn auth(&self) -> Option<&ClerkAuth> {
40        match self {
41            Self::Valid(auth) => Some(auth),
42            _ => None,
43        }
44    }
45
46    /// Consumes the outcome, returning verified auth only for a valid outcome.
47    #[must_use]
48    pub fn into_auth(self) -> Option<ClerkAuth> {
49        match self {
50            Self::Valid(auth) => Some(auth),
51            _ => None,
52        }
53    }
54}