kanidmd_lib/credential/
apppwd.rs1use crate::{
2 credential::{CryptoPolicy, Password},
3 prelude::*,
4};
5use kanidm_proto::internal::OperationError;
6use std::{cmp::Ordering, fmt};
7
8#[derive(Clone)]
9pub struct ApplicationPassword {
10 pub uuid: Uuid,
11 pub(crate) application: Uuid,
12 pub(crate) label: String,
13 pub(crate) password: Password,
14}
15
16impl fmt::Debug for ApplicationPassword {
17 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18 f.debug_struct("ApplicationPassword")
19 .field("uuid", &self.uuid)
20 .field("application", &self.application)
21 .field("label", &self.label)
22 .finish()
23 }
24}
25
26impl ApplicationPassword {
27 pub fn new(
28 application: Uuid,
29 label: &str,
30 cleartext: &str,
31 policy: &CryptoPolicy,
32 ) -> Result<ApplicationPassword, OperationError> {
33 let pw = Password::new(policy, cleartext).map_err(|e| {
34 error!(crypto_err = ?e);
35 OperationError::CryptographyError
36 })?;
37 let ap = ApplicationPassword {
38 uuid: Uuid::new_v4(),
39 application,
40 label: label.to_string(),
41 password: pw,
42 };
43 Ok(ap)
44 }
45}
46
47impl PartialEq for ApplicationPassword {
48 fn eq(&self, other: &Self) -> bool {
49 self.uuid == other.uuid
50 || (self.application == other.application && self.label == other.label)
51 }
52}
53
54impl Eq for ApplicationPassword {}
55
56impl PartialOrd for ApplicationPassword {
57 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
58 Some(self.cmp(other))
59 }
60}
61
62impl Ord for ApplicationPassword {
63 fn cmp(&self, other: &Self) -> Ordering {
64 self.uuid.cmp(&other.uuid)
65 }
66}