Skip to main content

confluent_kafka/admin/
scram_mechanism.rs

1// Copyright 2025 Confluent Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Public SASL/SCRAM mechanism representation.
16
17/// Representation of a SASL/SCRAM Mechanism.
18///
19/// See [KIP-554: Add Broker-side SCRAM Config API](https://cwiki.apache.org/confluence/display/KAFKA/KIP-554%3A+Add+Broker-side+SCRAM+Config+API).
20///
21/// This code is duplicated in
22/// `crate::common::security::scram::internals::ScramMechanism`. The type field
23/// in both files must match and must not change. The type field is used both for
24/// passing `ScramCredentialUpsertion` and for the internal
25/// `UserScramCredentialRecord`. Do not change the type field.
26#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
27#[non_exhaustive]
28#[doc(alias = "org.apache.kafka.clients.admin.ScramMechanism")]
29pub enum ScramMechanism {
30    /// An unknown / unsupported mechanism (type indicator `0`).
31    Unknown,
32    /// SCRAM-SHA-256 (type indicator `1`).
33    ScramSha256,
34    /// SCRAM-SHA-512 (type indicator `2`).
35    ScramSha512,
36}
37
38/// Java's inherited `Enum.toString()`: the constant's name.
39impl std::fmt::Display for ScramMechanism {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        f.write_str(match self {
42            Self::Unknown => "UNKNOWN",
43            Self::ScramSha256 => "SCRAM_SHA_256",
44            Self::ScramSha512 => "SCRAM_SHA_512",
45        })
46    }
47}
48
49impl ScramMechanism {
50    /// All mechanisms, in declaration order (mirrors Java's `values()`).
51    const VALUES: [ScramMechanism; 3] = [
52        ScramMechanism::Unknown,
53        ScramMechanism::ScramSha256,
54        ScramMechanism::ScramSha512,
55    ];
56
57    /// Returns the instance corresponding to the given type indicator, otherwise
58    /// [`ScramMechanism::Unknown`].
59    ///
60    /// # Arguments
61    ///
62    /// - `r#type` - the type indicator.
63    #[doc(alias = "org.apache.kafka.clients.admin.ScramMechanism#fromType")]
64    pub fn from_type(r#type: i8) -> ScramMechanism {
65        for mechanism in ScramMechanism::VALUES {
66            if mechanism.r#type() == r#type {
67                return mechanism;
68            }
69        }
70        ScramMechanism::Unknown
71    }
72
73    /// Returns the mechanism corresponding to the SASL SCRAM mechanism name,
74    /// otherwise [`ScramMechanism::Unknown`].
75    ///
76    /// # Arguments
77    ///
78    /// - `mechanism_name` - the SASL SCRAM mechanism name
79    #[doc(alias = "org.apache.kafka.clients.admin.ScramMechanism#fromMechanismName")]
80    pub fn from_mechanism_name(mechanism_name: &str) -> ScramMechanism {
81        ScramMechanism::VALUES
82            .into_iter()
83            .find(|mechanism| mechanism.mechanism_name() == mechanism_name)
84            .unwrap_or(ScramMechanism::Unknown)
85    }
86
87    /// Returns the corresponding SASL SCRAM mechanism name: `SCRAM-SHA-256`, `SCRAM-SHA-512`, or
88    /// `UNKNOWN`.
89    ///
90    /// # See also
91    ///
92    /// - [Salted Challenge Response Authentication Mechanism (SCRAM) SASL and GSS-API Mechanisms,
93    ///   Section 4](https://tools.ietf.org/html/rfc5802#section-4)
94    #[doc(alias = "org.apache.kafka.clients.admin.ScramMechanism#mechanismName")]
95    pub fn mechanism_name(self) -> &'static str {
96        match self {
97            ScramMechanism::Unknown => "UNKNOWN",
98            ScramMechanism::ScramSha256 => "SCRAM-SHA-256",
99            ScramMechanism::ScramSha512 => "SCRAM-SHA-512",
100        }
101    }
102
103    /// Returns the type indicator for this SASL SCRAM mechanism.
104    #[doc(alias = "org.apache.kafka.clients.admin.ScramMechanism#type")]
105    pub fn r#type(self) -> i8 {
106        match self {
107            ScramMechanism::Unknown => 0,
108            ScramMechanism::ScramSha256 => 1,
109            ScramMechanism::ScramSha512 => 2,
110        }
111    }
112}
113
114#[cfg(test)]
115mod tests {
116    use super::*;
117
118    // Mirrors `ScramMechanismTest.testFromMechanismName`.
119    #[test]
120    #[doc(alias = "org.apache.kafka.clients.admin.ScramMechanismTest#testFromMechanismName")]
121    fn test_from_mechanism_name() {
122        assert_eq!(ScramMechanism::Unknown, ScramMechanism::from_mechanism_name("UNKNOWN"));
123        assert_eq!(
124            ScramMechanism::ScramSha256,
125            ScramMechanism::from_mechanism_name("SCRAM-SHA-256")
126        );
127        assert_eq!(
128            ScramMechanism::ScramSha512,
129            ScramMechanism::from_mechanism_name("SCRAM-SHA-512")
130        );
131        assert_eq!(ScramMechanism::Unknown, ScramMechanism::from_mechanism_name("some string"));
132        assert_eq!(ScramMechanism::Unknown, ScramMechanism::from_mechanism_name("scram-sha-256"));
133    }
134
135    #[test]
136    fn from_type_round_trips() {
137        assert_eq!(ScramMechanism::Unknown, ScramMechanism::from_type(0));
138        assert_eq!(ScramMechanism::ScramSha256, ScramMechanism::from_type(1));
139        assert_eq!(ScramMechanism::ScramSha512, ScramMechanism::from_type(2));
140        assert_eq!(ScramMechanism::Unknown, ScramMechanism::from_type(9));
141    }
142}