pub struct CookieTemplate { /* private fields */ }Expand description
Builder for secure authentication cookies used by Gate.
Provides secure defaults that are automatically adjusted based on build configuration:
- Production builds: Secure=true, HttpOnly=true, SameSite=Strict, session cookie
- Debug builds: Secure=false (for localhost), SameSite=Lax, HttpOnly=true, session cookie
§Security Best Practices
The recommended approach is to start with CookieTemplate::recommended() and
customize only what you need:
use axum_gate::cookie_template::CookieTemplate;
use cookie::{time::Duration, SameSite};
// Secure defaults with custom name and expiration
let template = CookieTemplate::recommended()
.name("auth-token")
.persistent(Duration::hours(24));
// For OAuth/redirect flows that need cross-site navigation
let oauth_template = CookieTemplate::recommended()
.name("oauth-state")
.same_site(SameSite::Lax); // Allow cross-site for redirects§Security Features
- HttpOnly: Prevents JavaScript access to auth cookies (XSS protection)
- Secure: HTTPS-only in production (MITM protection)
- SameSite=Strict: Prevents CSRF attacks in production
- Session cookies: No persistent storage by default (privacy)
§Common Customizations
name("my-auth-cookie")- Set custom cookie namepersistent(Duration::hours(24))- Make cookie persist across browser sessionssame_site(SameSite::Lax)- Allow cross-site navigation (OAuth flows)domain(".example.com")- Share cookies across subdomains
Convert to cookie::Cookie via CookieTemplate::builder then .build(),
or use CookieTemplate::validate_and_build.
Implementations§
Source§impl CookieTemplate
impl CookieTemplate
Sourcepub fn recommended() -> Self
pub fn recommended() -> Self
Secure recommended defaults.
Sourcepub fn name(self, name: impl Into<Cow<'static, str>>) -> Self
pub fn name(self, name: impl Into<Cow<'static, str>>) -> Self
Set / override the cookie name.
Keep names short and avoid sensitive info.
Sourcepub fn value(self, value: impl Into<Cow<'static, str>>) -> Self
pub fn value(self, value: impl Into<Cow<'static, str>>) -> Self
Provide an initial value (normally left empty – the login code will insert the JWT).
Sourcepub fn domain(self, domain: impl Into<Cow<'static, str>>) -> Self
pub fn domain(self, domain: impl Into<Cow<'static, str>>) -> Self
Set the cookie domain. Avoid setting for single‑domain apps to retain host-only semantics (slightly tighter).
Sourcepub fn clear_domain(self) -> Self
pub fn clear_domain(self) -> Self
Unset the previously configured domain (host-only cookie).
Sourcepub fn insecure_dev_only(self) -> Self
pub fn insecure_dev_only(self) -> Self
Convenience: DISABLE secure flag for local dev ONLY.
In release builds this will panic to prevent accidental insecure
deployment. You must call this intentionally; no environment detection
is performed here.
Sourcepub fn same_site(self, same_site: SameSite) -> Self
pub fn same_site(self, same_site: SameSite) -> Self
Set the SameSite attribute (default Strict).
Consider Lax for some OAuth / cross-site redirect flows. Only use
None when you understand the CSRF implications and the need for
Secure.
Sourcepub fn clear_max_age(self) -> Self
pub fn clear_max_age(self) -> Self
Remove persistence (session cookie again).
Sourcepub fn persistent(self, duration: Duration) -> Self
pub fn persistent(self, duration: Duration) -> Self
Convenience for setting a persistent cookie lifetime.
Sourcepub fn short_lived(self) -> Self
pub fn short_lived(self) -> Self
Use a short-lived cookie (e.g. 15 minutes) – explicit for readability.
Sourcepub fn validate(&self) -> Result<(), CookieTemplateBuilderError>
pub fn validate(&self) -> Result<(), CookieTemplateBuilderError>
Validate the template configuration. Returns Ok(()) if fine.
Sourcepub fn builder(&self) -> CookieBuilder<'static>
pub fn builder(&self) -> CookieBuilder<'static>
Convert into the underlying cookie::CookieBuilder<'static>.
Sourcepub fn validate_and_build(
&self,
) -> Result<Cookie<'static>, CookieTemplateBuilderError>
pub fn validate_and_build( &self, ) -> Result<Cookie<'static>, CookieTemplateBuilderError>
Validate then build. Returns an error if invalid.
Sourcepub fn build_with_name_value(&self, name: &str, value: &str) -> Cookie<'static>
pub fn build_with_name_value(&self, name: &str, value: &str) -> Cookie<'static>
Build a cookie preserving all template attributes, having the name and value.
Sourcepub fn build_with_value(&self, value: &str) -> Cookie<'static>
pub fn build_with_value(&self, value: &str) -> Cookie<'static>
Build a cookie preserving attributes, overriding only the value.
Sourcepub fn build_with_name(&self, name: &str) -> Cookie<'static>
pub fn build_with_name(&self, name: &str) -> Cookie<'static>
Build a cookie preserving attributes, overriding only the name.
Sourcepub fn build_removal(&self) -> Cookie<'static>
pub fn build_removal(&self) -> Cookie<'static>
Build a removal cookie preserving attributes, overriding the name.
Get a reference to the configured cookie name without allocating.
Prefer this on hot paths (e.g., header extraction).
Trait Implementations§
Source§impl Clone for CookieTemplate
impl Clone for CookieTemplate
Source§fn clone(&self) -> CookieTemplate
fn clone(&self) -> CookieTemplate
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for CookieTemplate
impl Debug for CookieTemplate
Source§impl Default for CookieTemplate
impl Default for CookieTemplate
Source§impl PartialEq for CookieTemplate
impl PartialEq for CookieTemplate
impl Eq for CookieTemplate
impl StructuralPartialEq for CookieTemplate
Auto Trait Implementations§
impl Freeze for CookieTemplate
impl RefUnwindSafe for CookieTemplate
impl Send for CookieTemplate
impl Sync for CookieTemplate
impl Unpin for CookieTemplate
impl UnwindSafe for CookieTemplate
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more