Skip to main content

potree_auth/authentication/application/
service.rs

1use std::sync::Arc;
2
3use super::super::domain::AuthorizeData;
4use super::super::domain::CallbackRequestParams;
5use super::super::domain::OidcSessionPersisted;
6use super::super::ports::authentication_engine::AuthenticationEngine;
7use super::error::AuthenticationServiceError;
8use crate::user::domain::User;
9
10/// A service for handling user authentication with OIDC.
11#[derive(Debug, Clone)]
12pub struct AuthenticationService {
13    authentication_engine: Arc<dyn AuthenticationEngine>,
14}
15
16impl AuthenticationService {
17    pub fn new(authentication_engine: Arc<dyn AuthenticationEngine>) -> Self {
18        Self {
19            authentication_engine,
20        }
21    }
22
23    //// Called as part of the OIDC [`/authorize`] endpoint.
24    ///
25    /// [`/authorize`]: https://openid.net/specs/openid-connect-core-1_0.html#AuthorizationEndpoint
26    pub async fn authorize(&self) -> Result<AuthorizeData, AuthenticationServiceError> {
27        Ok(self.authentication_engine.authorize().await?)
28    }
29
30    /// After authentication, the IdP would redirect the user agent to the
31    /// callback route. This would handle the finalizing the OIDC flow to return
32    /// the validated user.
33    ///
34    /// The `persisted_data` would be extracted from the web session; it
35    /// should have been stored as part of the `/authorize` endpoint. The
36    /// `callback_params` would have come from the query params of the callback
37    /// handler.
38    pub async fn callback(
39        &self,
40        callback_params: CallbackRequestParams,
41        persisted_data: OidcSessionPersisted,
42    ) -> Result<User, AuthenticationServiceError> {
43        Ok(self
44            .authentication_engine
45            .callback(callback_params, persisted_data)
46            .await?)
47    }
48}