    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

    use actix_web::{dev::ServiceRequest, guard::GuardContext, HttpMessage, HttpRequest};

    use crate::{error::GetIdentityError, Identity};

    /// Helper trait to retrieve an [`Identity`] instance from various `actix-web`'s types.
    pub trait IdentityExt {
        /// Retrieve the identity attached to the current session, if available.
        fn get_identity(&self) -> Result<Identity, GetIdentityError>;
    }

    impl IdentityExt for HttpRequest {
        fn get_identity(&self) -> Result<Identity, GetIdentityError> {
            Identity::extract(&self.extensions())
        }
    }

    impl IdentityExt for ServiceRequest {
        fn get_identity(&self) -> Result<Identity, GetIdentityError> {
            Identity::extract(&self.extensions())
        }
    }

    impl<'a> IdentityExt for GuardContext<'a> {
        fn get_identity(&self) -> Result<Identity, GetIdentityError> {
            Identity::extract(&self.req_data())
        }
    }