Skip to main content

couchbase_core/
auth_mechanism.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 crate::error::Error;
20use crate::memdx;
21
22#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
23pub enum AuthMechanism {
24    Plain,
25    ScramSha1,
26    ScramSha256,
27    ScramSha512,
28    OAuthBearer,
29}
30
31impl From<AuthMechanism> for Vec<u8> {
32    fn from(value: AuthMechanism) -> Vec<u8> {
33        let txt = match value {
34            AuthMechanism::Plain => "PLAIN",
35            AuthMechanism::ScramSha1 => "SCRAM-SHA1",
36            AuthMechanism::ScramSha256 => "SCRAM-SHA256",
37            AuthMechanism::ScramSha512 => "SCRAM-SHA512",
38            AuthMechanism::OAuthBearer => "OAUTHBEARER",
39        };
40
41        txt.into()
42    }
43}
44
45impl TryFrom<&str> for AuthMechanism {
46    type Error = Error;
47
48    fn try_from(value: &str) -> Result<Self, Self::Error> {
49        let mech = match value {
50            "PLAIN" => AuthMechanism::Plain,
51            "SCRAM-SHA1" => AuthMechanism::ScramSha1,
52            "SCRAM-SHA256" => AuthMechanism::ScramSha256,
53            "SCRAM-SHA512" => AuthMechanism::ScramSha512,
54            "OAUTHBEARER" => AuthMechanism::OAuthBearer,
55            _ => {
56                return Err(Error::new_invalid_argument_error(
57                    format!("unsupported auth mechanism {value}"),
58                    None,
59                ));
60            }
61        };
62
63        Ok(mech)
64    }
65}
66
67impl From<AuthMechanism> for memdx::auth_mechanism::AuthMechanism {
68    fn from(value: AuthMechanism) -> Self {
69        match value {
70            AuthMechanism::Plain => memdx::auth_mechanism::AuthMechanism::Plain,
71            AuthMechanism::ScramSha1 => memdx::auth_mechanism::AuthMechanism::ScramSha1,
72            AuthMechanism::ScramSha256 => memdx::auth_mechanism::AuthMechanism::ScramSha256,
73            AuthMechanism::ScramSha512 => memdx::auth_mechanism::AuthMechanism::ScramSha512,
74            AuthMechanism::OAuthBearer => memdx::auth_mechanism::AuthMechanism::OAuthBearer,
75        }
76    }
77}