couchbase_core/
authenticator.rs

1/*
2 *
3 *  * Copyright (c) 2025 Couchbase, Inc.
4 *  *
5 *  * Licensed under the Apache License, Version 2.0 (the "License");
6 *  * you may not use this file except in compliance with the License.
7 *  * You may obtain a copy of the License at
8 *  *
9 *  *    http://www.apache.org/licenses/LICENSE-2.0
10 *  *
11 *  * Unless required by applicable law or agreed to in writing, software
12 *  * distributed under the License is distributed on an "AS IS" BASIS,
13 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  * See the License for the specific language governing permissions and
15 *  * limitations under the License.
16 *
17 */
18
19use std::fmt::Debug;
20
21use crate::error::Result;
22use crate::service_type::ServiceType;
23
24#[derive(Debug, Clone, PartialEq, Hash)]
25#[non_exhaustive]
26pub enum Authenticator {
27    PasswordAuthenticator(PasswordAuthenticator),
28    CertificateAuthenticator(CertificateAuthenticator),
29}
30
31#[derive(Debug, Clone, PartialEq, Eq, Hash)]
32pub struct UserPassPair {
33    pub username: String,
34    pub password: String,
35}
36
37#[derive(Debug, Clone, PartialEq, Eq, Hash)]
38pub struct PasswordAuthenticator {
39    pub username: String,
40    pub password: String,
41}
42
43impl PasswordAuthenticator {
44    pub fn get_credentials(
45        &self,
46        _service_type: &ServiceType,
47        _host_port: String,
48    ) -> Result<UserPassPair> {
49        Ok(UserPassPair {
50            username: self.username.clone(),
51            password: self.password.clone(),
52        })
53    }
54}
55
56impl From<PasswordAuthenticator> for Authenticator {
57    fn from(value: PasswordAuthenticator) -> Self {
58        Authenticator::PasswordAuthenticator(value)
59    }
60}
61
62// CertificateAuthenticator expects the TlsConfig provided in AgentConfig to contain the certificate chain and private key.
63#[derive(Debug, Clone, PartialEq, Eq, Hash)]
64pub struct CertificateAuthenticator {}
65
66impl CertificateAuthenticator {
67    pub fn get_credentials(
68        &self,
69        _service_type: &ServiceType,
70        _host_port: String,
71    ) -> Result<UserPassPair> {
72        Ok(UserPassPair {
73            username: String::new(), // No username for certificate auth
74            password: String::new(), // No password for certificate auth
75        })
76    }
77}
78
79impl From<CertificateAuthenticator> for Authenticator {
80    fn from(value: CertificateAuthenticator) -> Self {
81        Authenticator::CertificateAuthenticator(value)
82    }
83}