Skip to main content

potree_auth/authentication/ports/
authentication_engine.rs

1use std::fmt::Debug;
2
3use async_trait::async_trait;
4
5use super::super::domain::AuthorizeData;
6use super::super::domain::CallbackRequestParams;
7use super::super::domain::OidcSessionPersisted;
8use crate::user::domain::User;
9
10/// Defines the functionality that needs to be implemented for the application
11/// to perform OIDC authentication.
12#[async_trait]
13#[cfg_attr(test, mockall::automock)]
14pub trait AuthenticationEngine: Debug + Send + Sync + 'static {
15    /// Called as part of the OIDC [`/authorize`] endpoint.
16    ///
17    /// [`/authorize`]: https://openid.net/specs/openid-connect-core-1_0.html#AuthorizationEndpoint
18    async fn authorize(&self) -> Result<AuthorizeData, AuthenticationEngineError>;
19
20    /// After authentication, the IdP would redirect the user agent to the
21    /// callback route. This would handle the finalizing the OIDC flow to return
22    /// the validated user.
23    ///
24    /// The `persisted_data` would be extracted from the web session; it
25    /// should have been stored as part of the `/authorize` endpoint. The
26    /// `callback_params` would have come from the query params of the callback
27    /// handler.
28    async fn callback(
29        &self,
30        callback_params: CallbackRequestParams,
31        persisted_data: OidcSessionPersisted,
32    ) -> Result<User, AuthenticationEngineError>;
33}
34
35#[derive(Debug, Clone, thiserror::Error)]
36pub enum AuthenticationEngineError {
37    #[error("unable to set up infrastructure: {message}")]
38    Infrastructure { message: String },
39
40    #[error("unable exchange information with the IdP: {message}")]
41    IdpExchange { message: String },
42
43    #[error("unable to validate IdP data: {message}")]
44    Validation { message: String },
45}