1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#[cfg(test)]
mod textattrs_test;
use crate::attributes::*;
use crate::checks::*;
use crate::message::*;
use util::Error;
use std::fmt;
const MAX_USERNAME_B: usize = 513;
const MAX_REALM_B: usize = 763;
const MAX_SOFTWARE_B: usize = 763;
const MAX_NONCE_B: usize = 763;
pub type Username = TextAttribute;
pub type Realm = TextAttribute;
pub type Nonce = TextAttribute;
pub type Software = TextAttribute;
#[derive(Clone, Default)]
pub struct TextAttribute {
pub attr: AttrType,
pub text: String,
}
impl fmt::Display for TextAttribute {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.text)
}
}
impl Setter for TextAttribute {
fn add_to(&self, m: &mut Message) -> Result<(), Error> {
let text = self.text.as_bytes();
let max_len = match self.attr {
ATTR_USERNAME => MAX_USERNAME_B,
ATTR_REALM => MAX_REALM_B,
ATTR_SOFTWARE => MAX_SOFTWARE_B,
ATTR_NONCE => MAX_NONCE_B,
_ => return Err(Error::new(format!("Unsupported AttrType {}", self.attr))),
};
check_overflow(self.attr, text.len(), max_len)?;
m.add(self.attr, text);
Ok(())
}
}
impl Getter for TextAttribute {
fn get_from(&mut self, m: &Message) -> Result<(), Error> {
let attr = self.attr;
*self = TextAttribute::get_from_as(m, attr)?;
Ok(())
}
}
impl TextAttribute {
pub fn new(attr: AttrType, text: String) -> Self {
TextAttribute { attr, text }
}
pub fn get_from_as(m: &Message, attr: AttrType) -> Result<Self, Error> {
match attr {
ATTR_USERNAME => {}
ATTR_REALM => {}
ATTR_SOFTWARE => {}
ATTR_NONCE => {}
_ => return Err(Error::new(format!("Unsupported AttrType {}", attr))),
};
let a = m.get(attr)?;
let text = String::from_utf8(a)?;
Ok(TextAttribute { attr, text })
}
}