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
//! Contains [`UserInfo`] which stores information about the current user.

use actix_http::{HttpMessage, StatusCode};
use actix_utils::future::{ready, Ready};
use actix_web::FromRequest;
use serde::Deserialize;
use uuid::Uuid;

use crate::error::ServiceError;

use super::claims::Claims;

/// Information about the user extracted from the token provided.
#[derive(Debug, Clone, Deserialize)]
pub struct UserInfo {
    /// The current users id.
    pub id: Uuid,
    /// The scopes the current user has.
    pub scopes: Vec<String>,
}

impl From<Claims> for UserInfo {
    fn from(value: Claims) -> Self {
        Self {
            id: value.sub,
            scopes: value.scope.split(' ').map(str::to_owned).collect(),
        }
    }
}

impl FromRequest for UserInfo {
    type Future = Ready<Result<Self, Self::Error>>;
    type Error = ServiceError;

    fn from_request(
        req: &actix_web::HttpRequest,
        _payload: &mut actix_http::Payload,
    ) -> Self::Future {
        let extensions = req.extensions();
        ready({
            extensions.get::<Self>().map_or_else(
                || {
                    Err(ServiceError::new(
                        StatusCode::INTERNAL_SERVER_ERROR,
                        &StatusCode::INTERNAL_SERVER_ERROR.to_string(),
                    ))
                },
                |user_info| Ok(user_info.clone()),
            )
        })
    }
}