1use tacacs_plus_protocol::{AuthenticationMethod, PrivilegeLevel, UserInformation};
2
3use super::ClientError;
4
5pub(super) struct InvalidContext(());
6
7impl From<InvalidContext> for ClientError {
8 fn from(_value: InvalidContext) -> Self {
9 ClientError::InvalidContext
10 }
11}
12
13#[derive(Clone, PartialEq, Eq, Hash, Debug)]
15pub struct SessionContext {
16 pub(super) user: String,
17 pub(super) port: String,
18 pub(super) remote_address: String,
19 pub(super) privilege_level: PrivilegeLevel,
20 authentication_method: Option<AuthenticationMethod>,
21}
22
23impl SessionContext {
24 pub(super) fn as_user_information(&self) -> Result<UserInformation<'_>, InvalidContext> {
25 UserInformation::new(
26 self.user.as_str(),
27 self.port
28 .as_str()
29 .try_into()
30 .map_err(|_| InvalidContext(()))?,
31 self.remote_address
32 .as_str()
33 .try_into()
34 .map_err(|_| InvalidContext(()))?,
35 )
36 .ok_or(InvalidContext(()))
37 }
38
39 pub(super) fn authentication_method(&self) -> AuthenticationMethod {
43 self.authentication_method
44 .unwrap_or(AuthenticationMethod::NotSet)
45 }
46}
47
48#[derive(Debug, Clone, PartialEq, Eq, Hash)]
50pub struct ContextBuilder {
51 user: String,
52 port: String,
53 remote_address: String,
54 privilege_level: PrivilegeLevel,
55 authentication_method: Option<AuthenticationMethod>,
56}
57
58impl ContextBuilder {
60 pub fn new(user: String) -> Self {
62 Self {
63 user,
64 port: String::from("rust_client"),
65 remote_address: String::from("tacacs_plus_rs"),
66 privilege_level: Default::default(),
67 authentication_method: None,
68 }
69 }
70
71 pub fn port(&mut self, port: String) -> &mut Self {
73 self.port = port;
74 self
75 }
76
77 pub fn remote_address(&mut self, remote_address: String) -> &mut Self {
79 self.remote_address = remote_address;
80 self
81 }
82
83 pub fn privilege_level(&mut self, privilege_level: PrivilegeLevel) -> &mut Self {
85 self.privilege_level = privilege_level;
86 self
87 }
88
89 pub fn auth_method(&mut self, method: AuthenticationMethod) -> &mut Self {
93 self.authentication_method = Some(method);
94 self
95 }
96
97 pub fn build(&self) -> SessionContext {
99 SessionContext {
100 user: self.user.clone(),
101 port: self.port.clone(),
102 remote_address: self.remote_address.clone(),
103 privilege_level: self.privilege_level,
104 authentication_method: self.authentication_method,
105 }
106 }
107}