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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//! Password protocol extension.
//!
//! Passwords are sent in plain text!!
//!
//! # Example
//! Server:
//! ```
//! let mut passwords = HashMap::new();
//! passwords.insert("user1".to_string(), "pw".to_string());
//! let (mux, fut) = ServerMux::new(
//!     rx,
//!     tx,
//!     128,
//!     Some(&[Box::new(PasswordProtocolExtensionBuilder::new_server(passwords))])
//! );
//! ```
//!
//! Client:
//! ```
//! let (mux, fut) = ClientMux::new(
//!     rx,
//!     tx,
//!     128,
//!     Some(&[
//!          Box::new(PasswordProtocolExtensionBuilder::new_client(
//!             "user1".to_string(),
//!             "pw".to_string()
//!         ))
//!     ])
//! );
//! ```
//! See [the docs](https://github.com/MercuryWorkshop/wisp-protocol/blob/v2/protocol.md#0x02---password-authentication)

use std::{collections::HashMap, error::Error, fmt::Display, string::FromUtf8Error};

use async_trait::async_trait;
use bytes::{Buf, BufMut, Bytes, BytesMut};

use crate::{
	ws::{LockedWebSocketWrite, WebSocketRead},
	Role, WispError,
};

use super::{AnyProtocolExtension, ProtocolExtension, ProtocolExtensionBuilder};

#[derive(Debug, Clone)]
/// Password protocol extension.
///
/// **Passwords are sent in plain text!!**
/// **This extension will panic when encoding if the username's length does not fit within a u8
/// or the password's length does not fit within a u16.**
pub struct PasswordProtocolExtension {
	/// The username to log in with.
	///
	/// This string's length must fit within a u8.
	pub username: String,
	/// The password to log in with.
	///
	/// This string's length must fit within a u16.
	pub password: String,
	role: Role,
}

impl PasswordProtocolExtension {
	/// Password protocol extension ID.
	pub const ID: u8 = 0x02;

	/// Create a new password protocol extension for the server.
	///
	/// This signifies that the server requires a password.
	pub fn new_server() -> Self {
		Self {
			username: String::new(),
			password: String::new(),
			role: Role::Server,
		}
	}

	/// Create a new password protocol extension for the client, with a username and password.
	///
	/// The username's length must fit within a u8. The password's length must fit within a
	/// u16.
	pub fn new_client(username: String, password: String) -> Self {
		Self {
			username,
			password,
			role: Role::Client,
		}
	}
}

#[async_trait]
impl ProtocolExtension for PasswordProtocolExtension {
	fn get_id(&self) -> u8 {
		Self::ID
	}

	fn get_supported_packets(&self) -> &'static [u8] {
		&[]
	}

	fn encode(&self) -> Bytes {
		match self.role {
			Role::Server => Bytes::new(),
			Role::Client => {
				let username = Bytes::from(self.username.clone().into_bytes());
				let password = Bytes::from(self.password.clone().into_bytes());
				let username_len = u8::try_from(username.len()).expect("username was too long");
				let password_len = u16::try_from(password.len()).expect("password was too long");

				let mut bytes =
					BytesMut::with_capacity(3 + username_len as usize + password_len as usize);
				bytes.put_u8(username_len);
				bytes.put_u16_le(password_len);
				bytes.extend(username);
				bytes.extend(password);
				bytes.freeze()
			}
		}
	}

	async fn handle_handshake(
		&mut self,
		_: &mut dyn WebSocketRead,
		_: &LockedWebSocketWrite,
	) -> Result<(), WispError> {
		Ok(())
	}

	async fn handle_packet(
		&mut self,
		_: Bytes,
		_: &mut dyn WebSocketRead,
		_: &LockedWebSocketWrite,
	) -> Result<(), WispError> {
		Ok(())
	}

	fn box_clone(&self) -> Box<dyn ProtocolExtension + Sync + Send> {
		Box::new(self.clone())
	}
}

#[derive(Debug)]
enum PasswordProtocolExtensionError {
	Utf8Error(FromUtf8Error),
	InvalidUsername,
	InvalidPassword,
}

impl Display for PasswordProtocolExtensionError {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		use PasswordProtocolExtensionError as E;
		match self {
			E::Utf8Error(e) => write!(f, "{}", e),
			E::InvalidUsername => write!(f, "Invalid username"),
			E::InvalidPassword => write!(f, "Invalid password"),
		}
	}
}

impl Error for PasswordProtocolExtensionError {}

impl From<PasswordProtocolExtensionError> for WispError {
	fn from(value: PasswordProtocolExtensionError) -> Self {
		WispError::ExtensionImplError(Box::new(value))
	}
}

impl From<FromUtf8Error> for PasswordProtocolExtensionError {
	fn from(value: FromUtf8Error) -> Self {
		PasswordProtocolExtensionError::Utf8Error(value)
	}
}

impl From<PasswordProtocolExtension> for AnyProtocolExtension {
	fn from(value: PasswordProtocolExtension) -> Self {
		AnyProtocolExtension(Box::new(value))
	}
}

/// Password protocol extension builder.
///
/// **Passwords are sent in plain text!!**
pub struct PasswordProtocolExtensionBuilder {
	/// Map of users and their passwords to allow. Only used on server.
	pub users: HashMap<String, String>,
	/// Username to authenticate with. Only used on client.
	pub username: String,
	/// Password to authenticate with. Only used on client.
	pub password: String,
}

impl PasswordProtocolExtensionBuilder {
	/// Create a new password protocol extension builder for the server, with a map of users
	/// and passwords to allow.
	pub fn new_server(users: HashMap<String, String>) -> Self {
		Self {
			users,
			username: String::new(),
			password: String::new(),
		}
	}

	/// Create a new password protocol extension builder for the client, with a username and
	/// password to authenticate with.
	pub fn new_client(username: String, password: String) -> Self {
		Self {
			users: HashMap::new(),
			username,
			password,
		}
	}
}

impl ProtocolExtensionBuilder for PasswordProtocolExtensionBuilder {
	fn get_id(&self) -> u8 {
		PasswordProtocolExtension::ID
	}

	fn build_from_bytes(
		&self,
		mut payload: Bytes,
		role: crate::Role,
	) -> Result<AnyProtocolExtension, WispError> {
		match role {
			Role::Server => {
				if payload.remaining() < 3 {
					return Err(WispError::PacketTooSmall);
				}

				let username_len = payload.get_u8();
				let password_len = payload.get_u16_le();
				if payload.remaining() < (password_len + username_len as u16) as usize {
					return Err(WispError::PacketTooSmall);
				}

				use PasswordProtocolExtensionError as EError;
				let username =
					String::from_utf8(payload.copy_to_bytes(username_len as usize).to_vec())
						.map_err(|x| WispError::from(EError::from(x)))?;
				let password =
					String::from_utf8(payload.copy_to_bytes(password_len as usize).to_vec())
						.map_err(|x| WispError::from(EError::from(x)))?;

				let Some(user) = self.users.iter().find(|x| *x.0 == username) else {
					return Err(EError::InvalidUsername.into());
				};

				if *user.1 != password {
					return Err(EError::InvalidPassword.into());
				}

				Ok(PasswordProtocolExtension {
					username,
					password,
					role,
				}
				.into())
			}
			Role::Client => {
				Ok(PasswordProtocolExtension::new_client(String::new(), String::new()).into())
			}
		}
	}

	fn build_to_extension(&self, role: Role) -> AnyProtocolExtension {
		match role {
			Role::Server => PasswordProtocolExtension::new_server(),
			Role::Client => {
				PasswordProtocolExtension::new_client(self.username.clone(), self.password.clone())
			}
		}
		.into()
	}
}