drogue_bazaar/auth/pat/
mod.rs

1//! Personal access tokens (pat)
2
3use async_trait::async_trait;
4use drogue_client::user::v1::authn::{AuthenticationRequest, AuthenticationResponse};
5use std::fmt::{Debug, Formatter};
6use std::sync::Arc;
7
8use crate::auth::AuthError;
9pub use drogue_client::user::v1::authn::AuthenticationRequest as Request;
10pub use drogue_client::user::v1::authn::AuthenticationResponse as Response;
11pub use drogue_client::user::v1::authn::Outcome;
12
13#[derive(Clone)]
14pub struct Authenticator {
15    service: Arc<dyn Service>,
16}
17
18impl Debug for Authenticator {
19    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
20        f.debug_struct("Authenticator").finish()
21    }
22}
23
24impl Authenticator {
25    pub fn new<S>(service: S) -> Self
26    where
27        S: Service + 'static,
28    {
29        Self {
30            service: Arc::new(service),
31        }
32    }
33}
34
35impl Authenticator {
36    pub async fn authenticate(
37        &self,
38        request: AuthenticationRequest,
39    ) -> Result<AuthenticationResponse, AuthError> {
40        self.service.authenticate(request).await
41    }
42}
43
44/// Personal access token authenticator
45#[async_trait]
46pub trait Service {
47    /// authenticate a personal access token
48    async fn authenticate(
49        &self,
50        request: AuthenticationRequest,
51    ) -> Result<AuthenticationResponse, AuthError>;
52}
53
54#[async_trait]
55impl Service for drogue_client::user::v1::Client {
56    async fn authenticate(&self, request: Request) -> Result<AuthenticationResponse, AuthError> {
57        Ok(self
58            .authenticate_access_token(request)
59            .await
60            .map_err(|err| AuthError::Internal(err.to_string()))?)
61    }
62}