drogue_bazaar/auth/
user.rs

1//! Structures to work with users and identities.
2
3use drogue_client::user::v1::UserDetails;
4
5/// Information about the authenticated user, may be anonymous
6#[derive(Clone, Debug)]
7pub enum UserInformation {
8    Authenticated(UserDetails),
9    Anonymous,
10}
11
12pub const ANONYMOUS: UserInformation = UserInformation::Anonymous;
13static EMPTY_ROLES: Vec<String> = vec![];
14
15impl UserInformation {
16    pub fn user_id(&self) -> Option<&str> {
17        match self {
18            Self::Authenticated(details) => Some(&details.user_id),
19            Self::Anonymous => None,
20        }
21    }
22
23    pub fn roles(&self) -> &Vec<String> {
24        match self {
25            Self::Authenticated(details) => &details.roles,
26            Self::Anonymous => &EMPTY_ROLES,
27        }
28    }
29}
30
31/// Extractor for user information.
32#[cfg(feature = "actix")]
33impl actix_web::FromRequest for UserInformation {
34    type Error = actix_web::Error;
35    type Future = core::future::Ready<Result<Self, Self::Error>>;
36
37    fn from_request(req: &actix_web::HttpRequest, _: &mut actix_web::dev::Payload) -> Self::Future {
38        use actix_web::HttpMessage;
39        match req.extensions().get::<UserInformation>() {
40            Some(user) => core::future::ready(Ok(user.clone())),
41            None => core::future::ready(Ok(UserInformation::Anonymous)),
42        }
43    }
44}