reinhardt-websockets 0.2.0

WebSocket support for real-time bidirectional communication
Documentation
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//! WebSocket authentication and authorization
//!
//! This module provides authentication and authorization hooks for WebSocket connections,
//! integrating with Reinhardt's auth system.

use crate::connection::{WebSocketConnection, WebSocketError, WebSocketResult};
use async_trait::async_trait;
use std::sync::Arc;

/// Authentication result for WebSocket connections
pub type AuthResult<T> = Result<T, AuthError>;

/// Authentication errors
#[derive(Debug, thiserror::Error)]
pub enum AuthError {
	/// Authentication failed with the given reason.
	#[error("Authentication failed: {0}")]
	AuthenticationFailed(String),
	/// Authorization was denied for the given reason.
	#[error("Authorization denied: {0}")]
	AuthorizationDenied(String),
	/// The provided credentials are invalid.
	#[error("Invalid credentials")]
	InvalidCredentials,
	/// The authentication token has expired.
	#[error("Token expired")]
	TokenExpired,
	/// No authentication credentials were provided.
	#[error("Missing authentication")]
	MissingAuthentication,
}

/// Authenticated user information
pub trait AuthUser: Send + Sync + std::fmt::Debug {
	/// Get user identifier
	fn id(&self) -> &str;
	/// Get username
	fn username(&self) -> &str;
	/// Check if user is authenticated
	fn is_authenticated(&self) -> bool;
	/// Check if user has specific permission
	fn has_permission(&self, permission: &str) -> bool;
}

/// Simple user implementation for WebSocket authentication
///
/// # Examples
///
/// ```
/// use reinhardt_websockets::auth::{SimpleAuthUser, AuthUser};
///
/// let user = SimpleAuthUser::new(
///     "user_123".to_string(),
///     "alice".to_string(),
///     vec!["chat.read".to_string(), "chat.write".to_string()],
/// );
///
/// assert_eq!(user.id(), "user_123");
/// assert_eq!(user.username(), "alice");
/// assert!(user.is_authenticated());
/// assert!(user.has_permission("chat.read"));
/// assert!(!user.has_permission("admin.access"));
/// ```
#[derive(Debug, Clone)]
pub struct SimpleAuthUser {
	id: String,
	username: String,
	permissions: Vec<String>,
}

impl SimpleAuthUser {
	/// Create a new authenticated user
	pub fn new(id: String, username: String, permissions: Vec<String>) -> Self {
		Self {
			id,
			username,
			permissions,
		}
	}
}

impl AuthUser for SimpleAuthUser {
	fn id(&self) -> &str {
		&self.id
	}

	fn username(&self) -> &str {
		&self.username
	}

	fn is_authenticated(&self) -> bool {
		!self.id.is_empty()
	}

	fn has_permission(&self, permission: &str) -> bool {
		self.permissions.contains(&permission.to_string())
	}
}

/// WebSocket authenticator trait
///
/// Implementors define how to authenticate WebSocket connections.
#[async_trait]
pub trait WebSocketAuthenticator: Send + Sync {
	/// Authenticate a WebSocket connection
	///
	/// # Arguments
	///
	/// * `connection` - The WebSocket connection to authenticate
	/// * `credentials` - Authentication credentials (e.g., token, cookie)
	///
	/// # Returns
	///
	/// Returns the authenticated user on success, or an error on failure.
	async fn authenticate(
		&self,
		connection: &Arc<WebSocketConnection>,
		credentials: &str,
	) -> AuthResult<Box<dyn AuthUser>>;
}

/// Token-based WebSocket authenticator
///
/// # Examples
///
/// ```
/// use reinhardt_websockets::auth::{TokenAuthenticator, WebSocketAuthenticator, SimpleAuthUser};
/// use reinhardt_websockets::WebSocketConnection;
/// use tokio::sync::mpsc;
/// use std::sync::Arc;
///
/// # tokio_test::block_on(async {
/// let authenticator = TokenAuthenticator::new(vec![
///     ("valid_token".to_string(), SimpleAuthUser::new(
///         "user_1".to_string(),
///         "alice".to_string(),
///         vec!["chat.read".to_string()],
///     )),
/// ]);
///
/// let (tx, _rx) = mpsc::unbounded_channel();
/// let conn = Arc::new(WebSocketConnection::new("conn_1".to_string(), tx));
///
/// let user = authenticator.authenticate(&conn, "valid_token").await.unwrap();
/// assert_eq!(user.username(), "alice");
/// # });
/// ```
pub struct TokenAuthenticator {
	tokens: std::collections::HashMap<String, SimpleAuthUser>,
}

impl TokenAuthenticator {
	/// Create a new token authenticator with predefined tokens
	pub fn new(tokens: Vec<(String, SimpleAuthUser)>) -> Self {
		Self {
			tokens: tokens.into_iter().collect(),
		}
	}

	/// Add a token to the authenticator
	pub fn add_token(&mut self, token: String, user: SimpleAuthUser) {
		self.tokens.insert(token, user);
	}

	/// Remove a token from the authenticator
	pub fn remove_token(&mut self, token: &str) -> Option<SimpleAuthUser> {
		self.tokens.remove(token)
	}
}

#[async_trait]
impl WebSocketAuthenticator for TokenAuthenticator {
	async fn authenticate(
		&self,
		_connection: &Arc<WebSocketConnection>,
		credentials: &str,
	) -> AuthResult<Box<dyn AuthUser>> {
		self.tokens
			.get(credentials)
			.map(|user| Box::new(user.clone()) as Box<dyn AuthUser>)
			.ok_or(AuthError::InvalidCredentials)
	}
}

/// Authorization policy for WebSocket messages
#[async_trait]
pub trait AuthorizationPolicy: Send + Sync {
	/// Check if a user is authorized to perform an action
	///
	/// # Arguments
	///
	/// * `user` - The authenticated user
	/// * `action` - The action to authorize (e.g., "send_message", "join_room")
	/// * `resource` - Optional resource identifier (e.g., room ID)
	///
	/// # Returns
	///
	/// Returns `Ok(())` if authorized, or an error if denied.
	async fn authorize(
		&self,
		user: &dyn AuthUser,
		action: &str,
		resource: Option<&str>,
	) -> AuthResult<()>;
}

/// Permission-based authorization policy
///
/// # Examples
///
/// ```
/// use reinhardt_websockets::auth::{
///     PermissionBasedPolicy, AuthorizationPolicy, SimpleAuthUser
/// };
///
/// # tokio_test::block_on(async {
/// let policy = PermissionBasedPolicy::new(vec![
///     ("send_message".to_string(), "chat.write".to_string()),
///     ("delete_message".to_string(), "chat.admin".to_string()),
/// ]);
///
/// let user = SimpleAuthUser::new(
///     "user_1".to_string(),
///     "alice".to_string(),
///     vec!["chat.write".to_string()],
/// );
///
/// // User can send messages
/// assert!(policy.authorize(&user, "send_message", None).await.is_ok());
///
/// // User cannot delete messages (lacks chat.admin permission)
/// assert!(policy.authorize(&user, "delete_message", None).await.is_err());
/// # });
/// ```
pub struct PermissionBasedPolicy {
	action_permissions: std::collections::HashMap<String, String>,
}

impl PermissionBasedPolicy {
	/// Create a new permission-based policy
	pub fn new(action_permissions: Vec<(String, String)>) -> Self {
		Self {
			action_permissions: action_permissions.into_iter().collect(),
		}
	}

	/// Add an action-permission mapping
	pub fn add_permission(&mut self, action: String, permission: String) {
		self.action_permissions.insert(action, permission);
	}
}

#[async_trait]
impl AuthorizationPolicy for PermissionBasedPolicy {
	async fn authorize(
		&self,
		user: &dyn AuthUser,
		action: &str,
		_resource: Option<&str>,
	) -> AuthResult<()> {
		let required_permission = self
			.action_permissions
			.get(action)
			.ok_or_else(|| AuthError::AuthorizationDenied(format!("Unknown action: {}", action)))?;

		if user.has_permission(required_permission) {
			Ok(())
		} else {
			Err(AuthError::AuthorizationDenied(format!(
				"Missing permission: {}",
				required_permission
			)))
		}
	}
}

/// Authenticated WebSocket connection wrapper
///
/// # Examples
///
/// ```
/// use reinhardt_websockets::auth::{AuthenticatedConnection, SimpleAuthUser};
/// use reinhardt_websockets::WebSocketConnection;
/// use tokio::sync::mpsc;
/// use std::sync::Arc;
///
/// let (tx, _rx) = mpsc::unbounded_channel();
/// let conn = Arc::new(WebSocketConnection::new("conn_1".to_string(), tx));
/// let user = SimpleAuthUser::new(
///     "user_1".to_string(),
///     "alice".to_string(),
///     vec!["chat.read".to_string()],
/// );
///
/// let auth_conn = AuthenticatedConnection::new(conn, Box::new(user));
/// assert_eq!(auth_conn.user().username(), "alice");
/// ```
pub struct AuthenticatedConnection {
	connection: Arc<WebSocketConnection>,
	user: Box<dyn AuthUser>,
}

impl AuthenticatedConnection {
	/// Create a new authenticated connection
	pub fn new(connection: Arc<WebSocketConnection>, user: Box<dyn AuthUser>) -> Self {
		Self { connection, user }
	}

	/// Get the underlying WebSocket connection
	pub fn connection(&self) -> &Arc<WebSocketConnection> {
		&self.connection
	}

	/// Get the authenticated user
	pub fn user(&self) -> &dyn AuthUser {
		self.user.as_ref()
	}

	/// Send a message with authorization check
	pub async fn send_with_auth<P: AuthorizationPolicy>(
		&self,
		message: crate::connection::Message,
		policy: &P,
	) -> WebSocketResult<()> {
		policy
			.authorize(self.user.as_ref(), "send_message", None)
			.await
			.map_err(|_| WebSocketError::Protocol("authorization failed".to_string()))?;

		self.connection.send(message).await
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::connection::Message;
	use tokio::sync::mpsc;

	#[test]
	fn test_simple_auth_user() {
		let user = SimpleAuthUser::new(
			"user_123".to_string(),
			"alice".to_string(),
			vec!["read".to_string(), "write".to_string()],
		);

		assert_eq!(user.id(), "user_123");
		assert_eq!(user.username(), "alice");
		assert!(user.is_authenticated());
		assert!(user.has_permission("read"));
		assert!(user.has_permission("write"));
		assert!(!user.has_permission("admin"));
	}

	#[tokio::test]
	async fn test_token_authenticator_valid() {
		let user = SimpleAuthUser::new(
			"user_1".to_string(),
			"alice".to_string(),
			vec!["chat.read".to_string()],
		);

		let authenticator = TokenAuthenticator::new(vec![("token123".to_string(), user)]);

		let (tx, _rx) = mpsc::unbounded_channel();
		let conn = Arc::new(WebSocketConnection::new("conn_1".to_string(), tx));

		let auth_user = authenticator.authenticate(&conn, "token123").await.unwrap();
		assert_eq!(auth_user.username(), "alice");
	}

	#[tokio::test]
	async fn test_token_authenticator_invalid() {
		let authenticator = TokenAuthenticator::new(vec![]);

		let (tx, _rx) = mpsc::unbounded_channel();
		let conn = Arc::new(WebSocketConnection::new("conn_1".to_string(), tx));

		let result = authenticator.authenticate(&conn, "invalid_token").await;
		assert!(result.is_err());
		assert!(matches!(result.unwrap_err(), AuthError::InvalidCredentials));
	}

	#[tokio::test]
	async fn test_permission_based_policy_authorized() {
		let policy = PermissionBasedPolicy::new(vec![(
			"send_message".to_string(),
			"chat.write".to_string(),
		)]);

		let user = SimpleAuthUser::new(
			"user_1".to_string(),
			"alice".to_string(),
			vec!["chat.write".to_string()],
		);

		let result = policy.authorize(&user, "send_message", None).await;
		assert!(result.is_ok());
	}

	#[tokio::test]
	async fn test_permission_based_policy_denied() {
		let policy = PermissionBasedPolicy::new(vec![(
			"delete_message".to_string(),
			"chat.admin".to_string(),
		)]);

		let user = SimpleAuthUser::new(
			"user_1".to_string(),
			"alice".to_string(),
			vec!["chat.write".to_string()],
		);

		let result = policy.authorize(&user, "delete_message", None).await;
		assert!(result.is_err());
		assert!(matches!(
			result.unwrap_err(),
			AuthError::AuthorizationDenied(_)
		));
	}

	#[tokio::test]
	async fn test_authenticated_connection_send_with_auth() {
		let policy = PermissionBasedPolicy::new(vec![(
			"send_message".to_string(),
			"chat.write".to_string(),
		)]);

		let user = SimpleAuthUser::new(
			"user_1".to_string(),
			"alice".to_string(),
			vec!["chat.write".to_string()],
		);

		let (tx, mut rx) = mpsc::unbounded_channel();
		let conn = Arc::new(WebSocketConnection::new("conn_1".to_string(), tx));
		let auth_conn = AuthenticatedConnection::new(conn, Box::new(user));

		let msg = Message::text("Hello".to_string());
		auth_conn.send_with_auth(msg, &policy).await.unwrap();

		assert!(matches!(rx.try_recv(), Ok(Message::Text { .. })));
	}

	#[tokio::test]
	async fn test_authenticated_connection_send_with_auth_denied() {
		let policy = PermissionBasedPolicy::new(vec![(
			"send_message".to_string(),
			"chat.admin".to_string(),
		)]);

		let user = SimpleAuthUser::new(
			"user_1".to_string(),
			"alice".to_string(),
			vec!["chat.write".to_string()],
		);

		let (tx, _rx) = mpsc::unbounded_channel();
		let conn = Arc::new(WebSocketConnection::new("conn_1".to_string(), tx));
		let auth_conn = AuthenticatedConnection::new(conn, Box::new(user));

		let msg = Message::text("Hello".to_string());
		let result = auth_conn.send_with_auth(msg, &policy).await;

		assert!(result.is_err());
	}
}