Skip to content

Commit d8ce52a

Browse files
Refactoring
1 parent 7c7f507 commit d8ce52a

File tree

2 files changed

+29
-88
lines changed

2 files changed

+29
-88
lines changed

src/commons.rs

Lines changed: 9 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl From<&str> for SupportedProtocol {
143143

144144
impl From<String> for SupportedProtocol {
145145
fn from(value: String) -> Self {
146-
SupportedProtocol::from(value.as_str())
146+
Self::from(value.as_str())
147147
}
148148
}
149149

@@ -190,49 +190,13 @@ impl From<SupportedProtocol> for String {
190190

191191
impl From<&SupportedProtocol> for String {
192192
fn from(value: &SupportedProtocol) -> Self {
193-
match value {
194-
SupportedProtocol::Clustering => SUPPORTED_PROTOCOL_CLUSTERING.to_owned(),
195-
SupportedProtocol::AMQP => SUPPORTED_PROTOCOL_AMQP.to_owned(),
196-
SupportedProtocol::AMQPWithTLS => SUPPORTED_PROTOCOL_AMQP_WITH_TLS.to_owned(),
197-
SupportedProtocol::Stream => SUPPORTED_PROTOCOL_STREAM.to_owned(),
198-
SupportedProtocol::StreamWithTLS => SUPPORTED_PROTOCOL_STREAM_WITH_TLS.to_owned(),
199-
SupportedProtocol::MQTT => SUPPORTED_PROTOCOL_MQTT.to_owned(),
200-
SupportedProtocol::MQTTWithTLS => SUPPORTED_PROTOCOL_MQTT_WITH_TLS.to_owned(),
201-
SupportedProtocol::STOMP => SUPPORTED_PROTOCOL_STOMP.to_owned(),
202-
SupportedProtocol::STOMPWithTLS => SUPPORTED_PROTOCOL_STOMP_WITH_TLS.to_owned(),
203-
SupportedProtocol::AMQPOverWebSockets => {
204-
SUPPORTED_PROTOCOL_AMQP_OVER_WEBSOCKETS.to_owned()
205-
}
206-
SupportedProtocol::AMQPOverWebSocketsWithTLS => {
207-
SUPPORTED_PROTOCOL_AMQP_OVER_WEBSOCKETS_WITH_TLS.to_owned()
208-
}
209-
SupportedProtocol::MQTTOverWebSockets => {
210-
SUPPORTED_PROTOCOL_MQTT_OVER_WEBSOCKETS.to_owned()
211-
}
212-
SupportedProtocol::MQTTOverWebSocketsWithTLS => {
213-
SUPPORTED_PROTOCOL_MQTT_OVER_WEBSOCKETS_WITH_TLS.to_owned()
214-
}
215-
SupportedProtocol::STOMPOverWebsockets => {
216-
SUPPORTED_PROTOCOL_STOMP_OVER_WEBSOCKETS.to_owned()
217-
}
218-
SupportedProtocol::STOMPOverWebsocketsWithTLS => {
219-
SUPPORTED_PROTOCOL_STOMP_OVER_WEBSOCKETS_WITH_TLS.to_owned()
220-
}
221-
SupportedProtocol::Prometheus => SUPPORTED_PROTOCOL_PROMETHEUS.to_owned(),
222-
SupportedProtocol::PrometheusWithTLS => {
223-
SUPPORTED_PROTOCOL_PROMETHEUS_WITH_TLS.to_owned()
224-
}
225-
SupportedProtocol::HTTP => SUPPORTED_PROTOCOL_HTTP.to_owned(),
226-
SupportedProtocol::HTTPWithTLS => SUPPORTED_PROTOCOL_HTTP_WITH_TLS.to_owned(),
227-
SupportedProtocol::Other(s) => (*s).clone(),
228-
}
193+
value.clone().into()
229194
}
230195
}
231196

232197
impl fmt::Display for SupportedProtocol {
233198
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
234-
let proto: String = self.into();
235-
write!(f, "{proto}")
199+
write!(f, "{}", String::from(self))
236200
}
237201
}
238202

@@ -316,7 +280,7 @@ impl From<&str> for ExchangeType {
316280

317281
impl From<String> for ExchangeType {
318282
fn from(value: String) -> Self {
319-
ExchangeType::from(value.as_str())
283+
Self::from(value.as_str())
320284
}
321285
}
322286

@@ -381,13 +345,7 @@ impl From<&str> for QueueType {
381345

382346
impl From<String> for QueueType {
383347
fn from(value: String) -> Self {
384-
let val = value.to_ascii_lowercase();
385-
match val.as_str() {
386-
"classic" => QueueType::Classic,
387-
"quorum" => QueueType::Quorum,
388-
"stream" => QueueType::Stream,
389-
_ => QueueType::Unsupported(value),
390-
}
348+
Self::from(value.as_str())
391349
}
392350
}
393351

@@ -444,11 +402,7 @@ impl From<&str> for BindingDestinationType {
444402

445403
impl From<String> for BindingDestinationType {
446404
fn from(value: String) -> Self {
447-
match value.as_str() {
448-
"queue" => BindingDestinationType::Queue,
449-
"exchange" => BindingDestinationType::Exchange,
450-
_ => BindingDestinationType::Queue,
451-
}
405+
Self::from(value.as_str())
452406
}
453407
}
454408

@@ -568,20 +522,7 @@ impl From<&str> for PolicyTarget {
568522

569523
impl From<String> for PolicyTarget {
570524
fn from(value: String) -> Self {
571-
match value.as_str() {
572-
"queues" => PolicyTarget::Queues,
573-
"queue" => PolicyTarget::Queues,
574-
"classic_queues" => PolicyTarget::ClassicQueues,
575-
"classic_queue" => PolicyTarget::ClassicQueues,
576-
"quorum_queues" => PolicyTarget::QuorumQueues,
577-
"quorum_queue" => PolicyTarget::QuorumQueues,
578-
"streams" => PolicyTarget::Streams,
579-
"stream" => PolicyTarget::Streams,
580-
"exchanges" => PolicyTarget::Exchanges,
581-
"exchange" => PolicyTarget::Exchanges,
582-
"all" => PolicyTarget::All,
583-
_ => PolicyTarget::Queues,
584-
}
525+
Self::from(value.as_str())
585526
}
586527
}
587528

@@ -626,11 +567,7 @@ impl From<&str> for VirtualHostLimitTarget {
626567

627568
impl From<String> for VirtualHostLimitTarget {
628569
fn from(value: String) -> Self {
629-
match value.as_str() {
630-
"max-connections" => VirtualHostLimitTarget::MaxConnections,
631-
"max-queues" => VirtualHostLimitTarget::MaxQueues,
632-
_ => VirtualHostLimitTarget::MaxConnections,
633-
}
570+
Self::from(value.as_str())
634571
}
635572
}
636573

@@ -668,11 +605,7 @@ impl From<&str> for UserLimitTarget {
668605

669606
impl From<String> for UserLimitTarget {
670607
fn from(value: String) -> Self {
671-
match value.as_str() {
672-
"max-connections" => UserLimitTarget::MaxConnections,
673-
"max-channels" => UserLimitTarget::MaxChannels,
674-
_ => UserLimitTarget::MaxConnections,
675-
}
608+
Self::from(value.as_str())
676609
}
677610
}
678611

src/password_hashing.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,28 @@ pub fn salted_password_hash_sha512(salt: &[u8], password: &str) -> Vec<u8> {
4545
salted_password_hash(salt, password, &SHA512)
4646
}
4747

48+
/// Produces a Base64-encoded, salted password hash using the specified algorithm.
4849
///
49-
/// Produces a Base64-encoded, SHA-256 hashed, salted passowrd hash that can be passed
50+
/// See the [Credentials and Passwords guide](https://rabbitmq.com/docs/passwords/).
51+
pub fn base64_encoded_salted_password_hash(
52+
salt: &[u8],
53+
password: &str,
54+
algorithm: &HashingAlgorithm,
55+
) -> String {
56+
let salted = match algorithm {
57+
HashingAlgorithm::SHA256 => salted_password_hash_sha256(salt, password),
58+
HashingAlgorithm::SHA512 => salted_password_hash_sha512(salt, password),
59+
};
60+
rbase64::encode(salted.as_slice())
61+
}
62+
63+
///
64+
/// Produces a Base64-encoded, SHA-256 hashed, salted password hash that can be passed
5065
/// as [`crate::requests::UserParams::password_hash`] when adding a user with [`crate::blocking_api::Client::create_user`].
5166
///
5267
/// See the [Credentials and Passwords guide](https://rabbitmq.com/docs/passwords/).
5368
pub fn base64_encoded_salted_password_hash_sha256(salt: &[u8], password: &str) -> String {
54-
let salted = salted_password_hash_sha256(salt, password);
55-
rbase64::encode(salted.as_slice())
69+
base64_encoded_salted_password_hash(salt, password, &HashingAlgorithm::SHA256)
5670
}
5771

5872
///
@@ -61,8 +75,7 @@ pub fn base64_encoded_salted_password_hash_sha256(salt: &[u8], password: &str) -
6175
///
6276
/// See the [Credentials and Passwords guide](https://rabbitmq.com/docs/passwords/).
6377
pub fn base64_encoded_salted_password_hash_sha512(salt: &[u8], password: &str) -> String {
64-
let salted = salted_password_hash_sha512(salt, password);
65-
rbase64::encode(salted.as_slice())
78+
base64_encoded_salted_password_hash(salt, password, &HashingAlgorithm::SHA512)
6679
}
6780

6881
#[derive(Clone, Default, PartialEq, Eq, Hash, Debug)]
@@ -109,13 +122,8 @@ pub enum HashingError {
109122
}
110123

111124
impl HashingAlgorithm {
112-
pub fn salt_and_hash(&self, salt: &[u8], password: &str) -> Result<Vec<u8>, HashingError> {
113-
let hash = match self {
114-
HashingAlgorithm::SHA256 => salted_password_hash_sha256(salt, password),
115-
HashingAlgorithm::SHA512 => salted_password_hash_sha512(salt, password),
116-
};
117-
let encoded = rbase64::encode(hash.as_slice());
118-
Ok(encoded.as_bytes().to_vec())
125+
pub fn salt_and_hash(&self, salt: &[u8], password: &str) -> Result<String, HashingError> {
126+
Ok(base64_encoded_salted_password_hash(salt, password, self))
119127
}
120128
}
121129

0 commit comments

Comments
 (0)