Skip to main content

gestalt/
auth.rs

1use std::time::Duration;
2
3use tonic::codegen::async_trait;
4
5use crate::api::RuntimeMetadata;
6use crate::error::{Error, Result};
7pub use crate::generated::v1::{
8    AuthenticatedUser, BeginLoginRequest, BeginLoginResponse, CompleteLoginRequest,
9};
10
11#[async_trait]
12/// Lifecycle and login contract for Gestalt authentication providers.
13pub trait AuthenticationProvider: Send + Sync + 'static {
14    /// Configures the provider before it starts serving requests.
15    async fn configure(
16        &self,
17        _name: &str,
18        _config: serde_json::Map<String, serde_json::Value>,
19    ) -> Result<()> {
20        Ok(())
21    }
22
23    /// Returns runtime metadata that should augment the static manifest.
24    fn metadata(&self) -> Option<RuntimeMetadata> {
25        None
26    }
27
28    /// Returns non-fatal warnings the host should surface to users.
29    fn warnings(&self) -> Vec<String> {
30        Vec::new()
31    }
32
33    /// Performs an optional health check.
34    async fn health_check(&self) -> Result<()> {
35        Ok(())
36    }
37
38    /// Starts provider-owned background work after configuration.
39    async fn start(&self) -> Result<()> {
40        Ok(())
41    }
42
43    /// Shuts the provider down before the runtime exits.
44    async fn close(&self) -> Result<()> {
45        Ok(())
46    }
47
48    /// Starts an interactive login flow.
49    async fn begin_login(&self, req: BeginLoginRequest) -> Result<BeginLoginResponse>;
50
51    /// Finishes an interactive login flow.
52    async fn complete_login(&self, req: CompleteLoginRequest) -> Result<AuthenticatedUser>;
53
54    /// Validates an externally minted token when supported.
55    async fn validate_external_token(&self, _token: &str) -> Result<Option<AuthenticatedUser>> {
56        Err(Error::unimplemented(
57            "authentication provider does not support external token validation",
58        ))
59    }
60
61    /// Returns the TTL the host should use for persisted sessions.
62    fn session_ttl(&self) -> Option<Duration> {
63        None
64    }
65}