Skip to main content

static_authn_plugin/domain/
client.rs

1//! Client implementation for the static `AuthN` resolver plugin.
2//!
3//! Implements `AuthNResolverPluginClient` using the domain service.
4
5use async_trait::async_trait;
6use authn_resolver_sdk::{AuthNResolverError, AuthNResolverPluginClient, AuthenticationResult};
7
8use super::service::Service;
9
10#[async_trait]
11impl AuthNResolverPluginClient for Service {
12    async fn authenticate(
13        &self,
14        bearer_token: &str,
15    ) -> Result<AuthenticationResult, AuthNResolverError> {
16        self.authenticate(bearer_token)
17            .ok_or_else(|| AuthNResolverError::Unauthorized("invalid token".to_owned()))
18    }
19}
20
21#[cfg(test)]
22#[cfg_attr(coverage_nightly, coverage(off))]
23mod tests {
24    use super::*;
25    use crate::config::StaticAuthNPluginConfig;
26
27    #[tokio::test]
28    async fn plugin_trait_accept_all_succeeds() {
29        let service = Service::from_config(&StaticAuthNPluginConfig::default());
30        let plugin: &dyn AuthNResolverPluginClient = &service;
31
32        let result = plugin.authenticate("any-token").await;
33        assert!(result.is_ok());
34    }
35
36    #[tokio::test]
37    async fn plugin_trait_empty_token_unauthorized() {
38        let service = Service::from_config(&StaticAuthNPluginConfig::default());
39        let plugin: &dyn AuthNResolverPluginClient = &service;
40
41        let result = plugin.authenticate("").await;
42        assert!(result.is_err());
43        match result.unwrap_err() {
44            AuthNResolverError::Unauthorized(_) => {}
45            other => panic!("Expected Unauthorized, got: {other:?}"),
46        }
47    }
48}