    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
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256

    use std::rc::Rc;

    use actix_session::SessionExt;
    use actix_utils::future::{ready, Ready};
    use actix_web::{
        body::MessageBody,
        cookie::time::{format_description::well_known::Rfc3339, OffsetDateTime},
        dev::{Service, ServiceRequest, ServiceResponse, Transform},
        Error, HttpMessage as _, Result,
    };
    use futures_core::future::LocalBoxFuture;

    use crate::{
        config::{Configuration, IdentityMiddlewareBuilder},
        identity::IdentityInner,
        Identity,
    };

    /// Identity management middleware.
    ///
    /// ```no_run
    /// use actix_web::{cookie::Key, App, HttpServer};
    /// use actix_session::storage::RedisSessionStore;
    /// use actix_identity::{Identity, IdentityMiddleware};
    /// use actix_session::{Session, SessionMiddleware};
    ///
    /// #[actix_web::main]
    /// async fn main() {
    ///     let secret_key = Key::generate();
    ///     let redis_store = RedisSessionStore::new("redis://127.0.0.1:6379").await.unwrap();
    ///
    ///     HttpServer::new(move || {
    ///        App::new()
    ///            // Install the identity framework first.
    ///            .wrap(IdentityMiddleware::default())
    ///            // The identity system is built on top of sessions.
    ///            // You must install the session middleware to leverage `actix-identity`.
    ///            .wrap(SessionMiddleware::new(redis_store.clone(), secret_key.clone()))
    ///     })
    /// # ;
    /// }
    /// ```
    #[derive(Default, Clone)]
    pub struct IdentityMiddleware {
        configuration: Rc<Configuration>,
    }

    impl IdentityMiddleware {
        pub(crate) fn new(configuration: Configuration) -> Self {
            Self {
                configuration: Rc::new(configuration),
            }
        }

        /// A fluent API to configure [`IdentityMiddleware`].
        pub fn builder() -> IdentityMiddlewareBuilder {
            IdentityMiddlewareBuilder::new()
        }
    }

    impl<S, B> Transform<S, ServiceRequest> for IdentityMiddleware
    where
        S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
        S::Future: 'static,
        B: MessageBody + 'static,
    {
        type Response = ServiceResponse<B>;
        type Error = Error;
        type Transform = InnerIdentityMiddleware<S>;
        type InitError = ();
        type Future = Ready<Result<Self::Transform, Self::InitError>>;

        fn new_transform(&self, service: S) -> Self::Future {
            ready(Ok(InnerIdentityMiddleware {
                service: Rc::new(service),
                configuration: Rc::clone(&self.configuration),
            }))
        }
    }

    #[doc(hidden)]
    pub struct InnerIdentityMiddleware<S> {
        service: Rc<S>,
        configuration: Rc<Configuration>,
    }

    impl<S> Clone for InnerIdentityMiddleware<S> {
        fn clone(&self) -> Self {
            Self {
                service: Rc::clone(&self.service),
                configuration: Rc::clone(&self.configuration),
            }
        }
    }

    impl<S, B> Service<ServiceRequest> for InnerIdentityMiddleware<S>
    where
        S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
        S::Future: 'static,
        B: MessageBody + 'static,
    {
        type Response = ServiceResponse<B>;
        type Error = Error;
        type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;

        actix_service::forward_ready!(service);

        fn call(&self, req: ServiceRequest) -> Self::Future {
            let srv = Rc::clone(&self.service);
            let configuration = Rc::clone(&self.configuration);
            Box::pin(async move {
                let identity_inner = IdentityInner {
                    session: req.get_session(),
                    logout_behaviour: configuration.on_logout.clone(),
                    is_login_deadline_enabled: configuration.login_deadline.is_some(),
                    is_visit_deadline_enabled: configuration.visit_deadline.is_some(),
                };
                req.extensions_mut().insert(identity_inner);
                enforce_policies(&req, &configuration);
                srv.call(req).await
            })
        }
    }

    // easier to scan with returns where they are
    // especially if the function body were to evolve in the future
    #[allow(clippy::needless_return)]
    fn enforce_policies(req: &ServiceRequest, configuration: &Configuration) {
        let must_extract_identity =
            configuration.login_deadline.is_some() || configuration.visit_deadline.is_some();

        if !must_extract_identity {
            return;
        }

        let identity = match Identity::extract(&req.extensions()) {
            Ok(identity) => identity,
            Err(err) => {
                tracing::debug!(
                    error.display = %err,
                    error.debug = ?err,
                    "Failed to extract an `Identity` from the incoming request."
                );
                return;
            }
        };

        if let Some(login_deadline) = configuration.login_deadline {
            if matches!(
                enforce_login_deadline(&identity, login_deadline),
                PolicyDecision::LogOut
            ) {
                identity.logout();
                return;
            }
        }

        if let Some(visit_deadline) = configuration.visit_deadline {
            if matches!(
                enforce_visit_deadline(&identity, visit_deadline),
                PolicyDecision::LogOut
            ) {
                identity.logout();
                return;
            } else if let Err(err) = identity.set_last_visited_at() {
                tracing::warn!(
                    error.display = %err,
                    error.debug = ?err,
                    "Failed to set the last visited timestamp on `Identity` for an incoming request."
                );
            }
        }
    }

    fn enforce_login_deadline(
        identity: &Identity,
        login_deadline: std::time::Duration,
    ) -> PolicyDecision {
        match identity.logged_at() {
            Ok(None) => {
                tracing::info!(
                    "Login deadline is enabled, but there is no login timestamp in the session \
                    state attached to the incoming request. Logging the user out."
                );
                PolicyDecision::LogOut
            }
            Err(err) => {
                tracing::info!(
                    error.display = %err,
                    error.debug = ?err,
                    "Login deadline is enabled but we failed to extract the login timestamp from the \
                    session state attached to the incoming request. Logging the user out."
                );
                PolicyDecision::LogOut
            }
            Ok(Some(logged_in_at)) => {
                let elapsed = OffsetDateTime::now_utc() - logged_in_at;
                if elapsed > login_deadline {
                    tracing::info!(
                        user.logged_in_at = %logged_in_at.format(&Rfc3339).unwrap_or_default(),
                        identity.login_deadline_seconds = login_deadline.as_secs(),
                        identity.elapsed_since_login_seconds = elapsed.whole_seconds(),
                        "Login deadline is enabled and too much time has passed since the user logged \
                        in. Logging the user out."
                    );
                    PolicyDecision::LogOut
                } else {
                    PolicyDecision::StayLoggedIn
                }
            }
        }
    }

    fn enforce_visit_deadline(
        identity: &Identity,
        visit_deadline: std::time::Duration,
    ) -> PolicyDecision {
        match identity.last_visited_at() {
            Ok(None) => {
                tracing::info!(
                    "Last visit deadline is enabled, but there is no last visit timestamp in the \
                    session state attached to the incoming request. Logging the user out."
                );
                PolicyDecision::LogOut
            }
            Err(err) => {
                tracing::info!(
                    error.display = %err,
                    error.debug = ?err,
                    "Last visit deadline is enabled but we failed to extract the last visit timestamp \
                    from the session state attached to the incoming request. Logging the user out."
                );
                PolicyDecision::LogOut
            }
            Ok(Some(last_visited_at)) => {
                let elapsed = OffsetDateTime::now_utc() - last_visited_at;
                if elapsed > visit_deadline {
                    tracing::info!(
                        user.last_visited_at = %last_visited_at.format(&Rfc3339).unwrap_or_default(),
                        identity.visit_deadline_seconds = visit_deadline.as_secs(),
                        identity.elapsed_since_last_visit_seconds = elapsed.whole_seconds(),
                        "Last visit deadline is enabled and too much time has passed since the last \
                        time the user visited. Logging the user out."
                    );
                    PolicyDecision::LogOut
                } else {
                    PolicyDecision::StayLoggedIn
                }
            }
        }
    }

    enum PolicyDecision {
        StayLoggedIn,
        LogOut,
    }