scufflecloud_core/
http_ext.rs

1use core_db_types::models::UserSession;
2use ext_traits::OptionExt;
3use tonic::Code;
4use tonic_types::ErrorDetails;
5
6use crate::middleware::ExpiredSession;
7
8pub(crate) trait CoreRequestExt: ext_traits::RequestExt {
9    fn session(&self) -> Option<&UserSession> {
10        self.extensions().get::<UserSession>()
11    }
12
13    fn session_or_err(&self) -> Result<&UserSession, tonic::Status> {
14        self.session()
15            .into_tonic_err(Code::Unauthenticated, "you must be logged in", ErrorDetails::new())
16    }
17
18    fn expired_session_or_err(&self) -> Result<&UserSession, tonic::Status> {
19        self.extensions().get::<ExpiredSession>().map(|s| &s.0).into_tonic_err(
20            Code::Unauthenticated,
21            "you must be logged in",
22            ErrorDetails::new(),
23        )
24    }
25
26    fn dashboard_origin<G: core_traits::ConfigInterface + 'static>(&self) -> Result<url::Url, tonic::Status> {
27        self.global::<G>()?
28            .dashboard_origin()
29            .cloned()
30            .or_else(|| self.origin())
31            .ok_or_else(|| tonic::Status::invalid_argument("missing origin header and no dashboard origin configured"))
32    }
33}
34
35impl<T> CoreRequestExt for T where T: ext_traits::RequestExt {}