this repo has no description
1use crate::authentication::session_state::TypedSession;
2use crate::utils::{ResponseErrorMessage, e500};
3use actix_web::body::MessageBody;
4use actix_web::dev::{ServiceRequest, ServiceResponse};
5use actix_web::error::InternalError;
6use actix_web::http::StatusCode;
7use actix_web::http::header::ContentType;
8use actix_web::middleware::Next;
9use actix_web::{FromRequest, HttpMessage, HttpResponse};
10use std::ops::Deref;
11
12#[derive(Clone, Debug)]
13pub struct UserDid(String);
14
15impl std::fmt::Display for UserDid {
16 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17 self.0.fmt(f)
18 }
19}
20
21impl Deref for UserDid {
22 type Target = String;
23
24 fn deref(&self) -> &Self::Target {
25 &self.0
26 }
27}
28
29const UNAUTHORIZED_MESSAGE: &str = "The user has not logged in";
30const UNAUTHORIZED_MESSAGE_ADMIN: &str = "This user is not an admin";
31
32pub async fn reject_anonymous_users(
33 mut req: ServiceRequest,
34 next: Next<impl MessageBody>,
35) -> Result<ServiceResponse<impl MessageBody>, actix_web::Error> {
36 let session = {
37 let (http_request, payload) = req.parts_mut();
38 TypedSession::from_request(http_request, payload).await
39 }?;
40
41 match session.get_user_did().map_err(e500)? {
42 Some(user_id) => {
43 req.extensions_mut().insert(UserDid(user_id));
44 next.call(req).await
45 }
46 None => {
47 let response = HttpResponse::build(StatusCode::UNAUTHORIZED)
48 .content_type(ContentType::json())
49 .json(ResponseErrorMessage::from(UNAUTHORIZED_MESSAGE));
50 let e = anyhow::anyhow!(UNAUTHORIZED_MESSAGE);
51 Err(InternalError::from_response(e, response).into())
52 }
53 }
54}
55
56pub async fn admin_authorization(
57 mut req: ServiceRequest,
58 next: Next<impl MessageBody>,
59) -> Result<ServiceResponse<impl MessageBody>, actix_web::Error> {
60 let session = {
61 let (http_request, payload) = req.parts_mut();
62 TypedSession::from_request(http_request, payload).await
63 }?;
64
65 match session.get_user_admin().map_err(e500)? {
66 Some(user_admin) => {
67 if user_admin {
68 match session.get_user_did().map_err(e500)? {
69 Some(user_id) => {
70 req.extensions_mut().insert(UserDid(user_id));
71 next.call(req).await
72 }
73 None => {
74 let response = HttpResponse::build(StatusCode::UNAUTHORIZED)
75 .content_type(ContentType::json())
76 .json(ResponseErrorMessage::from(UNAUTHORIZED_MESSAGE_ADMIN));
77 let e = anyhow::anyhow!(UNAUTHORIZED_MESSAGE_ADMIN);
78 Err(InternalError::from_response(e, response).into())
79 }
80 }
81 } else {
82 let response = HttpResponse::build(StatusCode::UNAUTHORIZED)
83 .content_type(ContentType::json())
84 .json(ResponseErrorMessage::from(UNAUTHORIZED_MESSAGE_ADMIN));
85 let e = anyhow::anyhow!(UNAUTHORIZED_MESSAGE_ADMIN);
86 Err(InternalError::from_response(e, response).into())
87 }
88 }
89 None => {
90 let response = HttpResponse::build(StatusCode::UNAUTHORIZED)
91 .content_type(ContentType::json())
92 .json(ResponseErrorMessage::from(UNAUTHORIZED_MESSAGE_ADMIN));
93 let e = anyhow::anyhow!(UNAUTHORIZED_MESSAGE_ADMIN);
94 Err(InternalError::from_response(e, response).into())
95 }
96 }
97}