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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//! Default session storage which uses async redis requests
//!
//! Sessions are serialized to binary format and stored using [uuid::Uuid] key as bytes.
//! All sessions must have expirations time after which they will be automatically removed by
//! redis.
//!
//! [RedisStorage] is constructed by [RedisMiddlewareFactory] from [redis_async_pool::RedisPool] and shared
//! between all [RedisMiddleware] instances.

use crate::*;
use redis::aio::ConnectionLike;
use redis::AsyncCommands;
use std::marker::PhantomData;
use std::sync::Arc;

/// Redis implementation for [TokenStorage]
#[derive(Clone)]
struct RedisStorage<ClaimsType: Claims> {
    pool: redis_async_pool::RedisPool,
    _claims_type_marker: PhantomData<ClaimsType>,
}

impl<ClaimsType: Claims> RedisStorage<ClaimsType> {
    pub fn new(pool: redis_async_pool::RedisPool) -> Self {
        Self {
            pool,
            _claims_type_marker: Default::default(),
        }
    }
}

#[async_trait::async_trait(?Send)]
impl<ClaimsType> TokenStorage for RedisStorage<ClaimsType>
where
    ClaimsType: Claims,
{
    async fn get_by_jti(self: Arc<Self>, jti: &[u8]) -> Result<Vec<u8>, Error> {
        let pool = self.pool.clone();
        let mut conn = pool.get().await.map_err(|e| {
            #[cfg(feature = "use-tracing")]
            tracing::error!("Unable to obtain redis connection: {e}");
            Error::RedisConn
        })?;
        conn.get::<_, Vec<u8>>(jti).await.map_err(|e| {
            #[cfg(feature = "use-tracing")]
            tracing::error!("Session record not found in redis: {e}");
            Error::NotFound
        })
    }

    async fn set_by_jti(
        self: Arc<Self>,
        jwt_jti: &[u8],
        refresh_jti: &[u8],
        bytes: &[u8],
        mut exp: Duration,
    ) -> Result<(), Error> {
        bad_ttl!(
            exp,
            Duration::seconds(1),
            "Expiration time is bellow 1s. This is not allowed for redis server."
        );
        let pool = self.pool.clone();
        let mut conn = pool.get().await.map_err(|e| {
            #[cfg(feature = "use-tracing")]
            tracing::error!("Unable to obtain redis connection: {e}");
            Error::RedisConn
        })?;
        let mut pipeline = redis::Pipeline::new();
        pipeline
            .set_ex(jwt_jti, bytes, exp.as_seconds_f32() as usize)
            .set_ex(refresh_jti, bytes, exp.as_seconds_f32() as usize);
        conn.req_packed_commands(&pipeline, 0, 2)
            .await
            .map_err(|e| {
                #[cfg(feature = "use-tracing")]
                tracing::error!("Failed to save session in redis: {e}");
                Error::WriteFailed
            })?;
        Ok(())
    }

    async fn remove_by_jti(self: Arc<Self>, jti: &[u8]) -> Result<(), Error> {
        let pool = self.pool.clone();
        let mut conn = pool.get().await.map_err(|e| {
            #[cfg(feature = "use-tracing")]
            tracing::error!("Unable to obtain redis connection: {e}");
            Error::RedisConn
        })?;
        conn.del(jti).await.map_err(|e| {
            #[cfg(feature = "use-tracing")]
            tracing::error!("Session record can't be removed from redis: {e}");
            Error::NotFound
        })?;
        Ok(())
    }
}

impl<ClaimsType: Claims> SessionMiddlewareBuilder<ClaimsType> {
    #[must_use]
    pub fn with_redis_pool(mut self, pool: redis_async_pool::RedisPool) -> Self {
        let storage = Arc::new(RedisStorage::<ClaimsType>::new(pool));
        let storage = SessionStorage::new(storage, self.jwt_encoding_key.clone(), self.algorithm);
        self.storage = Some(storage);
        self
    }
}

#[cfg(test)]
mod tests {
    use actix_web::cookie::time::*;

    use super::*;
    use std::ops::Add;

    #[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq, Hash)]
    #[serde(rename_all = "snake_case")]
    pub struct Claims {
        #[serde(rename = "exp")]
        pub expires_at: usize,
        #[serde(rename = "iat")]
        pub issues_at: usize,
        /// Account login
        #[serde(rename = "sub")]
        pub subject: String,
        #[serde(rename = "aud")]
        pub audience: String,
        #[serde(rename = "jti")]
        pub jwt_id: uuid::Uuid,
        #[serde(rename = "aci")]
        pub account_id: i32,
    }

    impl crate::Claims for Claims {
        fn jti(&self) -> uuid::Uuid {
            self.jwt_id
        }

        fn subject(&self) -> &str {
            &self.subject
        }
    }

    async fn create_storage() -> (SessionStorage, SessionMiddlewareFactory<Claims>) {
        let redis = {
            use redis_async_pool::{RedisConnectionManager, RedisPool};
            RedisPool::new(
                RedisConnectionManager::new(
                    redis::Client::open("redis://localhost:6379")
                        .expect("Fail to connect to redis"),
                    true,
                    None,
                ),
                5,
            )
        };
        let jwt_signing_keys = JwtSigningKeys::generate(false).unwrap();
        SessionMiddlewareFactory::<Claims>::build(
            Arc::new(jwt_signing_keys.encoding_key),
            Arc::new(jwt_signing_keys.decoding_key),
            Algorithm::EdDSA,
        )
        .with_redis_pool(redis)
        .with_refresh_cookie(REFRESH_COOKIE_NAME)
        .with_refresh_header(REFRESH_HEADER_NAME)
        .with_jwt_cookie(JWT_COOKIE_NAME)
        .with_jwt_header(JWT_HEADER_NAME)
        .finish()
    }

    #[tokio::test]
    async fn check_encode() {
        let (store, _) = create_storage().await;
        let jwt_exp = JwtTtl(Duration::days(31));
        let refresh_exp = RefreshTtl(Duration::days(31));

        let original = Claims {
            subject: "me".into(),
            expires_at: OffsetDateTime::now_utc()
                .add(Duration::days(31))
                .unix_timestamp() as usize,
            issues_at: OffsetDateTime::now_utc().unix_timestamp() as usize,
            audience: "web".into(),
            jwt_id: Uuid::new_v4(),
            account_id: 24234,
        };

        store
            .store(original.clone(), jwt_exp, refresh_exp)
            .await
            .unwrap();
        let loaded = store.find_jwt(original.jwt_id).await.unwrap();
        assert_eq!(original, loaded);
    }
}