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::{
7    AuthNResolverClient, AuthNResolverError, AuthenticationResult, ClientCredentialsRequest,
8};
9use modkit_macros::domain_model;
10
11use super::{DomainError, Service};
12
13/// Local client wrapping the service.
14///
15/// Registered in `ClientHub` by the module during `init()`.
16#[domain_model]
17pub struct AuthNResolverLocalClient {
18    svc: Arc<Service>,
19}
20
21impl AuthNResolverLocalClient {
22    #[must_use]
23    pub fn new(svc: Arc<Service>) -> Self {
24        Self { svc }
25    }
26}
27
28fn log_and_convert(op: &str, e: DomainError) -> AuthNResolverError {
29    tracing::error!(operation = op, error = ?e, "authn_resolver call failed");
30    e.into()
31}
32
33#[async_trait]
34impl AuthNResolverClient for AuthNResolverLocalClient {
35    async fn authenticate(
36        &self,
37        bearer_token: &str,
38    ) -> Result<AuthenticationResult, AuthNResolverError> {
39        self.svc
40            .authenticate(bearer_token)
41            .await
42            .map_err(|e| log_and_convert("authenticate", e))
43    }
44
45    async fn exchange_client_credentials(
46        &self,
47        request: &ClientCredentialsRequest,
48    ) -> Result<AuthenticationResult, AuthNResolverError> {
49        self.svc
50            .exchange_client_credentials(request)
51            .await
52            .map_err(|e| log_and_convert("exchange_client_credentials", e))
53    }
54}