Skip to main content

authn_resolver/domain/
local_client.rs

1//! Local (in-process) client for the `AuthN` resolver.
2
3use std::sync::Arc;
4
5use async_trait::async_trait;
6use authn_resolver_sdk::{AuthNResolverClient, AuthNResolverError, AuthenticationResult};
7use modkit_macros::domain_model;
8
9use super::{DomainError, Service};
10
11/// Local client wrapping the service.
12///
13/// Registered in `ClientHub` by the module during `init()`.
14#[domain_model]
15pub struct AuthNResolverLocalClient {
16    svc: Arc<Service>,
17}
18
19impl AuthNResolverLocalClient {
20    #[must_use]
21    pub fn new(svc: Arc<Service>) -> Self {
22        Self { svc }
23    }
24}
25
26fn log_and_convert(op: &str, e: DomainError) -> AuthNResolverError {
27    tracing::error!(operation = op, error = ?e, "authn_resolver call failed");
28    e.into()
29}
30
31#[async_trait]
32impl AuthNResolverClient for AuthNResolverLocalClient {
33    async fn authenticate(
34        &self,
35        bearer_token: &str,
36    ) -> Result<AuthenticationResult, AuthNResolverError> {
37        self.svc
38            .authenticate(bearer_token)
39            .await
40            .map_err(|e| log_and_convert("authenticate", e))
41    }
42}