2022-07-09 19:00:15 +01:00
|
|
|
use actix_web::{dev::ServiceRequest, guard::GuardContext, HttpMessage, HttpRequest};
|
|
|
|
|
2023-01-06 20:05:12 -06:00
|
|
|
use crate::{error::GetIdentityError, Identity};
|
2022-07-09 19:00:15 +01:00
|
|
|
|
|
|
|
/// 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.
|
2023-01-06 20:05:12 -06:00
|
|
|
fn get_identity(&self) -> Result<Identity, GetIdentityError>;
|
2022-07-09 19:00:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl IdentityExt for HttpRequest {
|
2023-01-06 20:05:12 -06:00
|
|
|
fn get_identity(&self) -> Result<Identity, GetIdentityError> {
|
2022-07-09 19:00:15 +01:00
|
|
|
Identity::extract(&self.extensions())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IdentityExt for ServiceRequest {
|
2023-01-06 20:05:12 -06:00
|
|
|
fn get_identity(&self) -> Result<Identity, GetIdentityError> {
|
2022-07-09 19:00:15 +01:00
|
|
|
Identity::extract(&self.extensions())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> IdentityExt for GuardContext<'a> {
|
2023-01-06 20:05:12 -06:00
|
|
|
fn get_identity(&self) -> Result<Identity, GetIdentityError> {
|
2022-07-09 19:00:15 +01:00
|
|
|
Identity::extract(&self.req_data())
|
|
|
|
}
|
|
|
|
}
|