mqtt5 0.31.2

Complete MQTT v5.0 platform with high-performance async client and full-featured broker supporting TCP, TLS, WebSocket, authentication, bridging, and resource monitoring
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
use crate::broker::auth::{AuthProvider, AuthResult, EnhancedAuthResult};
use crate::error::Result;
use crate::packet::connect::ConnectPacket;
use crate::protocol::v5::reason_codes::ReasonCode;
use base64::prelude::*;
use ring::{hmac, signature};
use std::collections::HashMap;
use std::future::Future;
use std::net::SocketAddr;
use std::pin::Pin;
use tracing::{debug, warn};

#[derive(Clone)]
pub struct JwtVerifier {
    pub key: JwtVerifierKey,
    pub kid: Option<String>,
}

#[derive(Clone)]
pub enum JwtVerifierKey {
    Hs256(Vec<u8>),
    Rs256(Vec<u8>),
    Es256(Vec<u8>),
}

impl JwtVerifier {
    #[must_use]
    pub fn algorithm(&self) -> &'static str {
        match &self.key {
            JwtVerifierKey::Hs256(_) => "HS256",
            JwtVerifierKey::Rs256(_) => "RS256",
            JwtVerifierKey::Es256(_) => "ES256",
        }
    }

    #[must_use]
    pub fn kid(&self) -> Option<&str> {
        self.kid.as_deref()
    }

    #[must_use]
    pub fn hs256(key: Vec<u8>) -> Self {
        Self {
            key: JwtVerifierKey::Hs256(key),
            kid: None,
        }
    }

    #[must_use]
    pub fn rs256(key: Vec<u8>) -> Self {
        Self {
            key: JwtVerifierKey::Rs256(key),
            kid: None,
        }
    }

    #[must_use]
    pub fn es256(key: Vec<u8>) -> Self {
        Self {
            key: JwtVerifierKey::Es256(key),
            kid: None,
        }
    }
}

const DEFAULT_CLOCK_SKEW_SECS: u64 = 60;

pub struct JwtAuthProvider {
    verifiers: Vec<JwtVerifier>,
    required_issuer: Option<String>,
    required_audience: Option<String>,
    clock_skew_secs: u64,
}

impl JwtAuthProvider {
    #[must_use]
    pub fn with_hs256_secret(secret: impl AsRef<[u8]>) -> Self {
        Self {
            verifiers: vec![JwtVerifier::hs256(secret.as_ref().to_vec())],
            required_issuer: None,
            required_audience: None,
            clock_skew_secs: DEFAULT_CLOCK_SKEW_SECS,
        }
    }

    #[must_use]
    pub fn with_rs256_public_key(der: impl AsRef<[u8]>) -> Self {
        Self {
            verifiers: vec![JwtVerifier::rs256(der.as_ref().to_vec())],
            required_issuer: None,
            required_audience: None,
            clock_skew_secs: DEFAULT_CLOCK_SKEW_SECS,
        }
    }

    #[must_use]
    pub fn with_es256_public_key(der: impl AsRef<[u8]>) -> Self {
        Self {
            verifiers: vec![JwtVerifier::es256(der.as_ref().to_vec())],
            required_issuer: None,
            required_audience: None,
            clock_skew_secs: DEFAULT_CLOCK_SKEW_SECS,
        }
    }

    #[must_use]
    pub fn add_verifier(mut self, verifier: JwtVerifier) -> Self {
        self.verifiers.push(verifier);
        self
    }

    #[must_use]
    pub fn with_issuer(mut self, issuer: impl Into<String>) -> Self {
        self.required_issuer = Some(issuer.into());
        self
    }

    #[must_use]
    pub fn with_audience(mut self, audience: impl Into<String>) -> Self {
        self.required_audience = Some(audience.into());
        self
    }

    #[must_use]
    pub fn with_clock_skew(mut self, seconds: u64) -> Self {
        self.clock_skew_secs = seconds;
        self
    }

    fn verify_jwt(&self, token: &[u8]) -> std::result::Result<JwtClaims, JwtError> {
        let token_str =
            std::str::from_utf8(token).map_err(|_| JwtError::InvalidFormat("not UTF-8"))?;

        let parts: Vec<&str> = token_str.split('.').collect();
        if parts.len() != 3 {
            return Err(JwtError::InvalidFormat("expected 3 parts"));
        }

        let header_json = base64url_decode(parts[0])?;
        let payload_json = base64url_decode(parts[1])?;
        let signature_bytes = base64url_decode(parts[2])?;

        let header: JwtHeader = serde_json::from_slice(&header_json)
            .map_err(|_| JwtError::InvalidFormat("invalid header JSON"))?;

        let message = format!("{}.{}", parts[0], parts[1]);
        let message_bytes = message.as_bytes();

        let verifier = if self.verifiers.len() == 1 {
            &self.verifiers[0]
        } else if let Some(ref kid) = header.kid {
            self.verifiers
                .iter()
                .find(|v| v.kid() == Some(kid.as_str()))
                .ok_or(JwtError::InvalidClaim("unknown kid"))?
        } else {
            return Err(JwtError::MissingClaim("kid"));
        };

        if !Self::verify_signature(verifier, message_bytes, &signature_bytes) {
            return Err(JwtError::InvalidSignature);
        }

        let claims: JwtClaims = serde_json::from_slice(&payload_json)
            .map_err(|_| JwtError::InvalidFormat("invalid payload JSON"))?;

        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);

        let exp = claims.exp.ok_or(JwtError::MissingClaim("exp"))?;
        if now > exp + self.clock_skew_secs {
            return Err(JwtError::Expired);
        }

        if let Some(nbf) = claims.nbf {
            if now + self.clock_skew_secs < nbf {
                return Err(JwtError::NotYetValid);
            }
        }

        if let Some(ref required_iss) = self.required_issuer {
            match &claims.iss {
                Some(iss) if iss == required_iss => {}
                _ => return Err(JwtError::InvalidClaim("issuer mismatch")),
            }
        }

        if let Some(ref required_aud) = self.required_audience {
            match &claims.aud {
                Some(aud) if aud == required_aud => {}
                _ => return Err(JwtError::InvalidClaim("audience mismatch")),
            }
        }

        Ok(claims)
    }

    fn verify_signature(verifier: &JwtVerifier, message: &[u8], sig: &[u8]) -> bool {
        match &verifier.key {
            JwtVerifierKey::Hs256(secret) => {
                let key = hmac::Key::new(hmac::HMAC_SHA256, secret);
                hmac::verify(&key, message, sig).is_ok()
            }
            JwtVerifierKey::Rs256(public_key) => {
                let key = signature::UnparsedPublicKey::new(
                    &signature::RSA_PKCS1_2048_8192_SHA256,
                    public_key,
                );
                key.verify(message, sig).is_ok()
            }
            JwtVerifierKey::Es256(public_key) => {
                let key = signature::UnparsedPublicKey::new(
                    &signature::ECDSA_P256_SHA256_FIXED,
                    public_key,
                );
                key.verify(message, sig).is_ok()
            }
        }
    }
}

impl AuthProvider for JwtAuthProvider {
    fn authenticate<'a>(
        &'a self,
        _connect: &'a ConnectPacket,
        _client_addr: SocketAddr,
    ) -> Pin<Box<dyn Future<Output = Result<AuthResult>> + Send + 'a>> {
        Box::pin(async move { Ok(AuthResult::fail(ReasonCode::BadAuthenticationMethod)) })
    }

    fn authorize_publish<'a>(
        &'a self,
        _client_id: &str,
        _user_id: Option<&'a str>,
        _topic: &'a str,
    ) -> Pin<Box<dyn Future<Output = bool> + Send + 'a>> {
        Box::pin(async move { true })
    }

    fn authorize_subscribe<'a>(
        &'a self,
        _client_id: &str,
        _user_id: Option<&'a str>,
        _topic_filter: &'a str,
    ) -> Pin<Box<dyn Future<Output = bool> + Send + 'a>> {
        Box::pin(async move { true })
    }

    fn supports_enhanced_auth(&self) -> bool {
        true
    }

    fn authenticate_enhanced<'a>(
        &'a self,
        auth_method: &'a str,
        auth_data: Option<&'a [u8]>,
        _client_id: &'a str,
    ) -> Pin<Box<dyn Future<Output = Result<EnhancedAuthResult>> + Send + 'a>> {
        let method = auth_method.to_string();

        Box::pin(async move {
            if method != "JWT" {
                debug!(method = %method, "JWT auth provider received non-JWT method");
                return Ok(EnhancedAuthResult::fail(
                    method,
                    ReasonCode::BadAuthenticationMethod,
                ));
            }

            let Some(token) = auth_data else {
                warn!("JWT authentication failed: no token provided");
                return Ok(EnhancedAuthResult::fail_with_reason(
                    method,
                    ReasonCode::NotAuthorized,
                    "No JWT token provided".to_string(),
                ));
            };

            match self.verify_jwt(token) {
                Ok(claims) => {
                    let Some(user_id) = claims.sub else {
                        warn!("JWT authentication failed: missing sub claim");
                        return Ok(EnhancedAuthResult::fail_with_reason(
                            method,
                            ReasonCode::NotAuthorized,
                            "missing sub claim".to_string(),
                        ));
                    };
                    debug!(user_id = %user_id, "JWT authentication successful");
                    Ok(EnhancedAuthResult::success_with_user(method, user_id))
                }
                Err(e) => {
                    warn!(error = %e, "JWT authentication failed");
                    Ok(EnhancedAuthResult::fail_with_reason(
                        method,
                        ReasonCode::NotAuthorized,
                        e.to_string(),
                    ))
                }
            }
        })
    }

    fn reauthenticate<'a>(
        &'a self,
        auth_method: &'a str,
        auth_data: Option<&'a [u8]>,
        client_id: &'a str,
        _user_id: Option<&'a str>,
    ) -> Pin<Box<dyn Future<Output = Result<EnhancedAuthResult>> + Send + 'a>> {
        self.authenticate_enhanced(auth_method, auth_data, client_id)
    }
}

fn base64url_decode(input: &str) -> std::result::Result<Vec<u8>, JwtError> {
    let padded = match input.len() % 4 {
        2 => format!("{input}=="),
        3 => format!("{input}="),
        _ => input.to_string(),
    };

    let standard = padded.replace('-', "+").replace('_', "/");

    BASE64_STANDARD
        .decode(standard)
        .map_err(|_| JwtError::InvalidFormat("invalid base64"))
}

#[derive(Debug)]
enum JwtError {
    InvalidFormat(&'static str),
    InvalidSignature,
    Expired,
    NotYetValid,
    InvalidClaim(&'static str),
    MissingClaim(&'static str),
}

impl std::fmt::Display for JwtError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InvalidFormat(msg) => write!(f, "invalid JWT format: {msg}"),
            Self::InvalidSignature => write!(f, "invalid signature"),
            Self::Expired => write!(f, "token expired"),
            Self::NotYetValid => write!(f, "token not yet valid"),
            Self::InvalidClaim(msg) => write!(f, "invalid claim: {msg}"),
            Self::MissingClaim(claim) => write!(f, "missing required claim: {claim}"),
        }
    }
}

#[derive(serde::Deserialize)]
pub struct JwtHeader {
    pub alg: String,
    pub kid: Option<String>,
}

#[derive(serde::Deserialize, Default, Clone)]
pub struct JwtClaims {
    pub sub: Option<String>,
    pub iss: Option<String>,
    pub aud: Option<String>,
    pub exp: Option<u64>,
    pub nbf: Option<u64>,
    pub email: Option<String>,
    pub groups: Option<Vec<String>>,
    pub roles: Option<Vec<String>>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

impl JwtClaims {
    #[must_use]
    pub fn get_claim(&self, path: &str) -> Option<&serde_json::Value> {
        let parts: Vec<&str> = path.split('.').collect();
        let mut current: Option<&serde_json::Value> = self.extra.get(parts[0]);

        for part in &parts[1..] {
            current = current.and_then(|v| v.get(part));
        }

        current
    }

    #[must_use]
    pub fn get_claim_as_string(&self, path: &str) -> Option<String> {
        if path == "sub" {
            return self.sub.clone();
        }
        if path == "iss" {
            return self.iss.clone();
        }
        if path == "email" {
            return self.email.clone();
        }

        self.get_claim(path).and_then(|v| match v {
            serde_json::Value::String(s) => Some(s.clone()),
            _ => None,
        })
    }

    #[must_use]
    pub fn get_claim_as_array(&self, path: &str) -> Option<Vec<String>> {
        if path == "groups" {
            return self.groups.clone();
        }
        if path == "roles" {
            return self.roles.clone();
        }

        self.get_claim(path).and_then(|v| match v {
            serde_json::Value::Array(arr) => {
                let strings: Vec<String> = arr
                    .iter()
                    .filter_map(|item| item.as_str().map(String::from))
                    .collect();
                if strings.is_empty() {
                    None
                } else {
                    Some(strings)
                }
            }
            _ => None,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn create_hs256_token(secret: &[u8], claims: &str) -> String {
        let header = r#"{"alg":"HS256","typ":"JWT"}"#;
        let header_b64 = base64url_encode(header.as_bytes());
        let claims_b64 = base64url_encode(claims.as_bytes());
        let message = format!("{header_b64}.{claims_b64}");

        let key = hmac::Key::new(hmac::HMAC_SHA256, secret);
        let signature = hmac::sign(&key, message.as_bytes());
        let sig_b64 = base64url_encode(signature.as_ref());

        format!("{message}.{sig_b64}")
    }

    fn base64url_encode(data: &[u8]) -> String {
        BASE64_STANDARD
            .encode(data)
            .replace('+', "-")
            .replace('/', "_")
            .trim_end_matches('=')
            .to_string()
    }

    #[test]
    fn test_valid_hs256_token() {
        let secret = b"super-secret-key-for-testing-jwt";
        let provider = JwtAuthProvider::with_hs256_secret(secret);

        let claims = r#"{"sub":"user123","iss":"test","exp":9999999999}"#;
        let token = create_hs256_token(secret, claims);

        let result = provider.verify_jwt(token.as_bytes());
        assert!(result.is_ok());
        let claims = result.unwrap();
        assert_eq!(claims.sub, Some("user123".to_string()));
    }

    #[test]
    fn test_expired_token() {
        let secret = b"super-secret-key-for-testing-jwt";
        let provider = JwtAuthProvider::with_hs256_secret(secret);

        let claims = r#"{"sub":"user123","exp":1}"#;
        let token = create_hs256_token(secret, claims);

        let result = provider.verify_jwt(token.as_bytes());
        assert!(matches!(result, Err(JwtError::Expired)));
    }

    #[test]
    fn test_invalid_signature() {
        let secret = b"super-secret-key-for-testing-jwt";
        let wrong_secret = b"wrong-secret-key";
        let provider = JwtAuthProvider::with_hs256_secret(secret);

        let claims = r#"{"sub":"user123","exp":9999999999}"#;
        let token = create_hs256_token(wrong_secret, claims);

        let result = provider.verify_jwt(token.as_bytes());
        assert!(matches!(result, Err(JwtError::InvalidSignature)));
    }

    #[test]
    fn test_issuer_validation() {
        let secret = b"super-secret-key-for-testing-jwt";
        let provider = JwtAuthProvider::with_hs256_secret(secret).with_issuer("expected-issuer");

        let claims = r#"{"sub":"user123","iss":"wrong-issuer","exp":9999999999}"#;
        let token = create_hs256_token(secret, claims);

        let result = provider.verify_jwt(token.as_bytes());
        assert!(matches!(result, Err(JwtError::InvalidClaim(_))));

        let claims = r#"{"sub":"user123","iss":"expected-issuer","exp":9999999999}"#;
        let token = create_hs256_token(secret, claims);

        let result = provider.verify_jwt(token.as_bytes());
        assert!(result.is_ok());
    }

    #[test]
    fn test_audience_validation() {
        let secret = b"super-secret-key-for-testing-jwt";
        let provider = JwtAuthProvider::with_hs256_secret(secret).with_audience("mqtt-broker");

        let claims = r#"{"sub":"user123","aud":"mqtt-broker","exp":9999999999}"#;
        let token = create_hs256_token(secret, claims);

        let result = provider.verify_jwt(token.as_bytes());
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_enhanced_auth_success() {
        let secret = b"super-secret-key-for-testing-jwt";
        let provider = JwtAuthProvider::with_hs256_secret(secret);

        let claims = r#"{"sub":"user123","exp":9999999999}"#;
        let token = create_hs256_token(secret, claims);

        let result = provider
            .authenticate_enhanced("JWT", Some(token.as_bytes()), "client-1")
            .await
            .unwrap();

        assert_eq!(
            result.status,
            crate::broker::auth::EnhancedAuthStatus::Success
        );
    }

    #[tokio::test]
    async fn test_enhanced_auth_wrong_method() {
        let secret = b"super-secret-key-for-testing-jwt";
        let provider = JwtAuthProvider::with_hs256_secret(secret);

        let result = provider
            .authenticate_enhanced("SCRAM-SHA-256", None, "client-1")
            .await
            .unwrap();

        assert_eq!(
            result.status,
            crate::broker::auth::EnhancedAuthStatus::Failed
        );
        assert_eq!(result.reason_code, ReasonCode::BadAuthenticationMethod);
    }

    #[tokio::test]
    async fn test_enhanced_auth_no_token() {
        let secret = b"super-secret-key-for-testing-jwt";
        let provider = JwtAuthProvider::with_hs256_secret(secret);

        let result = provider
            .authenticate_enhanced("JWT", None, "client-1")
            .await
            .unwrap();

        assert_eq!(
            result.status,
            crate::broker::auth::EnhancedAuthStatus::Failed
        );
        assert_eq!(result.reason_code, ReasonCode::NotAuthorized);
    }
}