1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use std::{
    fmt,
    sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard},
};

use viz_utils::{futures::future::BoxFuture, thiserror::Error as ThisError, tracing};

use crate::{
    http,
    types::{Cookie, CookieJar, Key},
    Context, Extract, Response, Result,
};

/// Cookies Error
#[derive(ThisError, Debug, PartialEq)]
pub enum CookiesError {
    /// Failed to read cookies
    #[error("failed to read cookies")]
    Read,
    /// Failed to parse cookies
    #[error("failed to parse cookies")]
    Parse,
}

impl From<CookiesError> for Response {
    fn from(e: CookiesError) -> Self {
        (http::StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into()
    }
}

/// Extract typed information from the request's cookies
#[derive(Clone)]
pub struct Cookies {
    inner: Arc<(Key, RwLock<CookieJar>)>,
}

impl Cookies {
    fn key(&self) -> &Key {
        &self.inner.0
    }

    fn jar(&self) -> &RwLock<CookieJar> {
        &self.inner.1
    }

    /// Reads the CookieJar
    pub fn read(&self) -> RwLockReadGuard<'_, CookieJar> {
        self.jar().read().unwrap()
    }

    /// Writes the CookieJar
    pub fn write(&self) -> RwLockWriteGuard<'_, CookieJar> {
        self.jar().write().unwrap()
    }

    /// Gets a cookie by name
    pub fn get(&self, name: &str) -> Option<Cookie<'_>> {
        self.read().get(name).cloned()
    }

    /// Adds a cookie
    pub fn add(&self, cookie: Cookie<'_>) {
        self.write().add(cookie.into_owned())
    }

    /// Gets a signed cookie by name
    #[cfg(feature = "signed-cookies")]
    pub fn get_with_singed(&self, name: &str) -> Option<Cookie<'_>> {
        self.write().signed(self.key()).get(name)
    }

    /// Adds a signed cookie
    #[cfg(feature = "signed-cookies")]
    pub fn add_with_singed(&self, cookie: Cookie<'_>) {
        self.write().signed_mut(self.key()).add(cookie.into_owned())
    }

    /// Gets a private cookie by name
    #[cfg(feature = "private-cookies")]
    pub fn get_with_private(&self, name: &str) -> Option<Cookie<'_>> {
        self.write().private(self.key()).get(name)
    }

    /// Adds a private cookie
    #[cfg(feature = "private-cookies")]
    pub fn add_with_private(&self, cookie: Cookie<'_>) {
        self.write().private_mut(self.key()).add(cookie.into_owned())
    }
}

impl fmt::Debug for Cookies {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Cookies")
            .field("key", &self.key().master())
            .field("jar", &self.jar())
            .finish()
    }
}

impl From<(Key, CookieJar)> for Cookies {
    fn from(kc: (Key, CookieJar)) -> Self {
        Cookies { inner: Arc::new((kc.0, RwLock::new(kc.1))) }
    }
}

impl Extract for Cookies {
    type Error = CookiesError;

    #[inline]
    fn extract(cx: &mut Context) -> BoxFuture<'_, Result<Self, Self::Error>> {
        Box::pin(async move { cx.cookies() })
    }
}

impl Context {
    /// Gets cookies.
    pub fn cookies(&mut self) -> Result<Cookies, CookiesError> {
        if let Some(cookies) = self.extensions().get::<Cookies>().cloned() {
            return Ok(cookies);
        }

        let mut jar = CookieJar::new();

        if let Some(raw_cookie) = self.header_value(http::header::COOKIE) {
            for pair in raw_cookie
                .to_str()
                .map_err(|e| {
                    tracing::error!("failed to extract cookies: {}", e);
                    CookiesError::Read
                })?
                .split(';')
            {
                jar.add_original(Cookie::parse_encoded(pair.trim().to_string()).map_err(|e| {
                    tracing::error!("failed to parse cookies: {}", e);
                    CookiesError::Parse
                })?)
            }
        }

        let cookies = Cookies::from((Key::from(self.config().cookies.secret_key.as_bytes()), jar));

        self.extensions_mut().insert::<Cookies>(cookies.clone());

        Ok(cookies)
    }

    /// Gets single cookie by name.
    pub fn cookie(&self, name: &str) -> Option<Cookie<'_>> {
        self.extensions().get::<Cookies>()?.get(name)
    }
}