Skip to main content

mz_auth/
password.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10use mz_ore::secure::Zeroize;
11use proptest_derive::Arbitrary;
12use serde::{Deserialize, Serialize};
13use std::fmt::{Debug, Display};
14
15use static_assertions::assert_not_impl_all;
16
17///Password is a String wrapper type that does not implement Display or Debug
18#[derive(
19    Clone,
20    PartialEq,
21    Eq,
22    Serialize,
23    Deserialize,
24    PartialOrd,
25    Ord,
26    Arbitrary
27)]
28pub struct Password(pub String);
29
30assert_not_impl_all!(Password: Display);
31
32impl From<String> for Password {
33    fn from(password: String) -> Self {
34        Password(password)
35    }
36}
37
38impl From<&str> for Password {
39    fn from(password: &str) -> Self {
40        Password(password.to_string())
41    }
42}
43
44impl Password {
45    /// Returns the password as a byte slice, suitable for hashing.
46    ///
47    /// Unlike the former `to_string().as_bytes()` pattern, this does not
48    /// create an unzeroed `String` copy on the heap.
49    pub fn as_bytes(&self) -> &[u8] {
50        self.0.as_bytes()
51    }
52
53    /// Returns the password as a string slice.
54    pub fn as_str(&self) -> &str {
55        &self.0
56    }
57}
58
59impl Drop for Password {
60    fn drop(&mut self) {
61        self.0.zeroize();
62    }
63}
64
65impl Debug for Password {
66    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67        write!(f, "Password(****)")
68    }
69}