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
use async_trait::async_trait;
use bson::{doc, to_document};
pub use mongodb;
use mongodb::{options::UpdateOptions, Client, Collection};
use serde::{Deserialize, Serialize};
use time::OffsetDateTime;
use tower_sessions_core::{
    session::{Id, Record},
    session_store, ExpiredDeletion, SessionStore,
};

/// An error type for `MongoDBStore`.
#[derive(thiserror::Error, Debug)]
pub enum MongoDBStoreError {
    /// A variant to map to `mongodb::error::Error` errors.
    #[error(transparent)]
    MongoDB(#[from] mongodb::error::Error),

    /// A variant to map `rmp_serde` encode errors.
    #[error(transparent)]
    Encode(#[from] rmp_serde::encode::Error),

    /// A variant to map `rmp_serde` decode errors.
    #[error(transparent)]
    Decode(#[from] rmp_serde::decode::Error),

    /// A variant to map `mongodb::bson` encode errors.
    #[error(transparent)]
    BsonSerialize(#[from] bson::ser::Error),
}

impl From<MongoDBStoreError> for session_store::Error {
    fn from(err: MongoDBStoreError) -> Self {
        match err {
            MongoDBStoreError::MongoDB(inner) => session_store::Error::Backend(inner.to_string()),
            MongoDBStoreError::Decode(inner) => session_store::Error::Decode(inner.to_string()),
            MongoDBStoreError::Encode(inner) => session_store::Error::Encode(inner.to_string()),
            MongoDBStoreError::BsonSerialize(inner) => {
                session_store::Error::Encode(inner.to_string())
            }
        }
    }
}

#[derive(Serialize, Deserialize, Debug)]
struct MongoDBSessionRecord {
    data: bson::Binary,

    #[serde(rename = "expireAt")]
    expiry_date: bson::DateTime,
}

/// A MongoDB session store.
#[derive(Clone, Debug)]
pub struct MongoDBStore {
    collection: Collection<MongoDBSessionRecord>,
}

impl MongoDBStore {
    /// Create a new MongoDBStore store with the provided connection pool.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use tower_sessions::{mongodb::Client, MongoDBStore};
    ///
    /// # tokio_test::block_on(async {
    /// let database_url = std::option_env!("DATABASE_URL").unwrap();
    /// let client = Client::with_uri_str(database_url).await.unwrap();
    /// let session_store = MongoDBStore::new(client, "database".to_string());
    /// # })
    /// ```
    pub fn new(client: Client, database: String) -> Self {
        Self {
            collection: client.database(&database).collection("sessions"),
        }
    }
}

#[async_trait]
impl ExpiredDeletion for MongoDBStore {
    async fn delete_expired(&self) -> session_store::Result<()> {
        self.collection
            .delete_many(
                doc! { "expireAt": {"$lt": OffsetDateTime::now_utc()} },
                None,
            )
            .await
            .map_err(MongoDBStoreError::MongoDB)?;

        Ok(())
    }
}

#[async_trait]
impl SessionStore for MongoDBStore {
    async fn save(&self, record: &Record) -> session_store::Result<()> {
        let doc = to_document(&MongoDBSessionRecord {
            data: bson::Binary {
                subtype: bson::spec::BinarySubtype::Generic,
                bytes: rmp_serde::to_vec(record).map_err(MongoDBStoreError::Encode)?,
            },
            expiry_date: bson::DateTime::from(record.expiry_date),
        })
        .map_err(MongoDBStoreError::BsonSerialize)?;

        self.collection
            .update_one(
                doc! { "_id": record.id.to_string() },
                doc! { "$set": doc },
                UpdateOptions::builder().upsert(true).build(),
            )
            .await
            .map_err(MongoDBStoreError::MongoDB)?;

        Ok(())
    }

    async fn load(&self, session_id: &Id) -> session_store::Result<Option<Record>> {
        let doc = self
            .collection
            .find_one(
                doc! {
                    "_id": session_id.to_string(),
                    "expireAt": {"$gt": OffsetDateTime::now_utc()}
                },
                None,
            )
            .await
            .map_err(MongoDBStoreError::MongoDB)?;

        if let Some(doc) = doc {
            Ok(Some(
                rmp_serde::from_slice(&doc.data.bytes).map_err(MongoDBStoreError::Decode)?,
            ))
        } else {
            Ok(None)
        }
    }

    async fn delete(&self, session_id: &Id) -> session_store::Result<()> {
        self.collection
            .delete_one(doc! { "_id": session_id.to_string() }, None)
            .await
            .map_err(MongoDBStoreError::MongoDB)?;

        Ok(())
    }
}