otoroshictl 0.0.12

a CLI to manage your otoroshi clusters with style ;)
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
//! Otoroshi challenge protocol implementation.
//!
//! Supports both V1 (simple echo) and V2 (JWT challenge/response) protocols
//! with configurable HMAC algorithms (HS256, HS384, HS512).

use std::str::FromStr;

use chrono::Utc;
use jsonwebtoken::{DecodingKey, EncodingKey, Header, Validation, decode, encode};
use serde::{Deserialize, Serialize};
use thiserror::Error;

/// Default header name for incoming challenge token.
pub const DEFAULT_STATE_HEADER: &str = "Otoroshi-State";

/// Default header name for outgoing response token.
pub const DEFAULT_STATE_RESP_HEADER: &str = "Otoroshi-State-Resp";

/// Expected issuer in Otoroshi challenge tokens.
pub const OTOROSHI_ISSUER: &str = "Otoroshi";

/// Default JWT token expiry time in seconds.
pub const DEFAULT_TOKEN_EXPIRY_SECONDS: i64 = 30;

/// Supported HMAC algorithms for Otoroshi protocol.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Algorithm {
    HS256,
    HS384,
    #[default]
    HS512,
}

impl FromStr for Algorithm {
    type Err = std::convert::Infallible;

    /// Parse algorithm from string (e.g., "HS256", "HS384", "HS512").
    /// Defaults to HS512 for unknown values.
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s.to_uppercase().as_str() {
            "HS256" => Algorithm::HS256,
            "HS384" => Algorithm::HS384,
            _ => Algorithm::HS512,
        })
    }
}

impl Algorithm {
    fn as_jsonwebtoken(self) -> jsonwebtoken::Algorithm {
        match self {
            Algorithm::HS256 => jsonwebtoken::Algorithm::HS256,
            Algorithm::HS384 => jsonwebtoken::Algorithm::HS384,
            Algorithm::HS512 => jsonwebtoken::Algorithm::HS512,
        }
    }
}

/// Errors that can occur during protocol operations.
#[derive(Debug, Error)]
pub enum ProtocolError {
    /// JWT verification failed.
    #[error("JWT verification failed: {0}")]
    VerificationFailed(#[from] jsonwebtoken::errors::Error),

    /// JWT encoding failed.
    #[error("Failed to create response token: {0}")]
    EncodingFailed(jsonwebtoken::errors::Error),
}

/// Claims expected in the Otoroshi challenge JWT.
///
/// Only the `state` field is needed; other JWT claims are ignored.
#[derive(Debug, Deserialize)]
struct ChallengeClaims {
    /// The challenge state value to echo back in the response.
    state: String,
}

/// Claims for the response JWT sent back to Otoroshi.
#[derive(Debug, Serialize)]
struct ResponseClaims {
    /// The echoed state value from the challenge.
    #[serde(rename = "state-resp")]
    state_resp: String,
    /// Audience (required by Otoroshi).
    aud: String,
    /// Issued at timestamp.
    iat: i64,
    /// Not before timestamp.
    nbf: i64,
    /// Expiration timestamp.
    exp: i64,
}

/// Otoroshi protocol handler for V1/V2 challenge-response.
#[derive(Debug, Clone)]
pub struct OtoroshiProtocol {
    /// Algorithm for verifying incoming tokens.
    pub algo_in: Algorithm,
    /// Secret for verifying incoming tokens (raw bytes).
    pub secret_in: Vec<u8>,
    /// Algorithm for signing outgoing tokens.
    pub algo_out: Algorithm,
    /// Secret for signing outgoing tokens (raw bytes).
    pub secret_out: Vec<u8>,
    /// JWT token TTL in seconds.
    pub ttl: i64,
}

impl OtoroshiProtocol {
    /// Create a new protocol handler with the same secret and algorithm for both directions.
    pub fn new(secret: &[u8], algorithm: Algorithm) -> Self {
        Self {
            algo_in: algorithm,
            secret_in: secret.to_vec(),
            algo_out: algorithm,
            secret_out: secret.to_vec(),
            ttl: DEFAULT_TOKEN_EXPIRY_SECONDS,
        }
    }

    /// Create a new protocol handler with configurable TTL.
    pub fn new_with_ttl(secret: &[u8], algorithm: Algorithm, ttl: i64) -> Self {
        Self {
            algo_in: algorithm,
            secret_in: secret.to_vec(),
            algo_out: algorithm,
            secret_out: secret.to_vec(),
            ttl,
        }
    }

    /// Create a new protocol handler with separate secrets and algorithms for each direction.
    pub fn new_asymmetric(
        secret_in: &[u8],
        algo_in: Algorithm,
        secret_out: &[u8],
        algo_out: Algorithm,
    ) -> Self {
        Self {
            algo_in,
            secret_in: secret_in.to_vec(),
            algo_out,
            secret_out: secret_out.to_vec(),
            ttl: DEFAULT_TOKEN_EXPIRY_SECONDS,
        }
    }

    /// Process a V1 challenge (simple echo).
    ///
    /// Returns the same state value that was passed in.
    pub fn process_v1(&self, state: &str) -> String {
        state.to_string()
    }

    /// Process a V2 challenge (JWT verification and response generation).
    ///
    /// Verifies the incoming JWT token, extracts the state, and creates a response token.
    pub fn process_v2(&self, token: &str) -> Result<String, ProtocolError> {
        let state = self.verify_challenge(token)?;
        self.create_response_token(&state)
    }

    /// Verify an Otoroshi V2 challenge token.
    ///
    /// Validates the JWT signature and extracts the state claim.
    pub fn verify_challenge(&self, token: &str) -> Result<String, ProtocolError> {
        let mut validation = Validation::new(self.algo_in.as_jsonwebtoken());
        // Require expiration and issuer claims for security
        validation.set_required_spec_claims(&["exp", "iss"]);
        validation.set_issuer(&[OTOROSHI_ISSUER]);
        validation.validate_aud = false;
        // Be lenient with expiration for clock skew (matches Otoroshi's acceptLeeway default)
        validation.leeway = 10;

        let token_data = decode::<ChallengeClaims>(
            token,
            &DecodingKey::from_secret(&self.secret_in),
            &validation,
        )?;

        Ok(token_data.claims.state)
    }

    /// Create a response token for Otoroshi.
    ///
    /// Generates a JWT containing the echoed state value with appropriate claims.
    pub fn create_response_token(&self, state: &str) -> Result<String, ProtocolError> {
        let now = Utc::now().timestamp();

        let claims = ResponseClaims {
            state_resp: state.to_string(),
            aud: OTOROSHI_ISSUER.to_string(),
            iat: now,
            nbf: now,
            exp: now + self.ttl,
        };

        encode(
            &Header::new(self.algo_out.as_jsonwebtoken()),
            &claims,
            &EncodingKey::from_secret(&self.secret_out),
        )
        .map_err(ProtocolError::EncodingFailed)
    }
}

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

    const TEST_SECRET: &[u8] = b"test-secret-key-for-testing";

    // -------------------------------------------------------------------------
    // Algorithm tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_algorithm_from_str_hs256() {
        assert_eq!(Algorithm::from_str("HS256").unwrap(), Algorithm::HS256);
        assert_eq!(Algorithm::from_str("hs256").unwrap(), Algorithm::HS256);
    }

    #[test]
    fn test_algorithm_from_str_hs384() {
        assert_eq!(Algorithm::from_str("HS384").unwrap(), Algorithm::HS384);
        assert_eq!(Algorithm::from_str("hs384").unwrap(), Algorithm::HS384);
    }

    #[test]
    fn test_algorithm_from_str_hs512() {
        assert_eq!(Algorithm::from_str("HS512").unwrap(), Algorithm::HS512);
        assert_eq!(Algorithm::from_str("hs512").unwrap(), Algorithm::HS512);
    }

    #[test]
    fn test_algorithm_from_str_defaults_to_hs512() {
        assert_eq!(Algorithm::from_str("unknown").unwrap(), Algorithm::HS512);
        assert_eq!(Algorithm::from_str("").unwrap(), Algorithm::HS512);
    }

    #[test]
    fn test_algorithm_default() {
        assert_eq!(Algorithm::default(), Algorithm::HS512);
    }

    // -------------------------------------------------------------------------
    // Protocol constructor tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_protocol_new() {
        let protocol = OtoroshiProtocol::new(TEST_SECRET, Algorithm::HS256);
        assert_eq!(protocol.algo_in, Algorithm::HS256);
        assert_eq!(protocol.algo_out, Algorithm::HS256);
        assert_eq!(protocol.secret_in, TEST_SECRET);
        assert_eq!(protocol.secret_out, TEST_SECRET);
        assert_eq!(protocol.ttl, DEFAULT_TOKEN_EXPIRY_SECONDS);
    }

    #[test]
    fn test_protocol_new_with_ttl() {
        let protocol = OtoroshiProtocol::new_with_ttl(TEST_SECRET, Algorithm::HS384, 60);
        assert_eq!(protocol.algo_in, Algorithm::HS384);
        assert_eq!(protocol.ttl, 60);
    }

    #[test]
    fn test_protocol_new_asymmetric() {
        let secret_in = b"secret-in";
        let secret_out = b"secret-out";
        let protocol = OtoroshiProtocol::new_asymmetric(
            secret_in,
            Algorithm::HS256,
            secret_out,
            Algorithm::HS512,
        );
        assert_eq!(protocol.algo_in, Algorithm::HS256);
        assert_eq!(protocol.algo_out, Algorithm::HS512);
        assert_eq!(protocol.secret_in, secret_in);
        assert_eq!(protocol.secret_out, secret_out);
    }

    // -------------------------------------------------------------------------
    // V1 protocol tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_process_v1_echoes_state() {
        let protocol = OtoroshiProtocol::new(TEST_SECRET, Algorithm::HS512);
        assert_eq!(protocol.process_v1("test-state"), "test-state");
        assert_eq!(protocol.process_v1(""), "");
        assert_eq!(
            protocol.process_v1("special-chars-!@#$%"),
            "special-chars-!@#$%"
        );
    }

    // -------------------------------------------------------------------------
    // Response token creation tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_create_response_token_success() {
        let protocol = OtoroshiProtocol::new(TEST_SECRET, Algorithm::HS512);
        let token = protocol.create_response_token("my-state").unwrap();

        // Token should be a valid JWT (three parts separated by dots)
        let parts: Vec<&str> = token.split('.').collect();
        assert_eq!(parts.len(), 3);
    }

    #[test]
    fn test_create_response_token_can_be_decoded() {
        let protocol = OtoroshiProtocol::new(TEST_SECRET, Algorithm::HS512);
        let token = protocol.create_response_token("my-state").unwrap();

        // Decode and verify the token contains expected claims
        let mut validation = Validation::new(jsonwebtoken::Algorithm::HS512);
        validation.set_required_spec_claims::<&str>(&[]);
        validation.set_audience(&[OTOROSHI_ISSUER]);

        #[derive(Debug, Deserialize)]
        struct TestClaims {
            #[serde(rename = "state-resp")]
            state_resp: String,
            aud: String,
            iat: i64,
            #[allow(dead_code)]
            nbf: i64,
            exp: i64,
        }

        let decoded =
            decode::<TestClaims>(&token, &DecodingKey::from_secret(TEST_SECRET), &validation)
                .unwrap();

        assert_eq!(decoded.claims.state_resp, "my-state");
        assert_eq!(decoded.claims.aud, OTOROSHI_ISSUER);
        assert!(decoded.claims.exp > decoded.claims.iat);
    }

    #[test]
    fn test_create_response_token_respects_ttl() {
        let protocol = OtoroshiProtocol::new_with_ttl(TEST_SECRET, Algorithm::HS512, 120);
        let token = protocol.create_response_token("state").unwrap();

        let mut validation = Validation::new(jsonwebtoken::Algorithm::HS512);
        validation.set_required_spec_claims::<&str>(&[]);
        validation.set_audience(&[OTOROSHI_ISSUER]);

        #[derive(Debug, Deserialize)]
        struct TestClaims {
            iat: i64,
            exp: i64,
        }

        let decoded =
            decode::<TestClaims>(&token, &DecodingKey::from_secret(TEST_SECRET), &validation)
                .unwrap();

        assert_eq!(decoded.claims.exp - decoded.claims.iat, 120);
    }

    // -------------------------------------------------------------------------
    // Challenge verification tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_verify_challenge_valid_token() {
        let protocol = OtoroshiProtocol::new(TEST_SECRET, Algorithm::HS512);

        // Create a valid challenge token
        #[derive(Serialize)]
        struct Challenge {
            state: String,
            iss: String,
            iat: i64,
            exp: i64,
        }

        let now = Utc::now().timestamp();
        let claims = Challenge {
            state: "challenge-state".to_string(),
            iss: OTOROSHI_ISSUER.to_string(),
            iat: now,
            exp: now + 60,
        };

        let token = encode(
            &Header::new(jsonwebtoken::Algorithm::HS512),
            &claims,
            &EncodingKey::from_secret(TEST_SECRET),
        )
        .unwrap();

        let state = protocol.verify_challenge(&token).unwrap();
        assert_eq!(state, "challenge-state");
    }

    #[test]
    fn test_verify_challenge_invalid_signature() {
        let protocol = OtoroshiProtocol::new(TEST_SECRET, Algorithm::HS512);

        // Create a token with a different secret
        #[derive(Serialize)]
        struct Challenge {
            state: String,
            iss: String,
            exp: i64,
        }

        let claims = Challenge {
            state: "state".to_string(),
            iss: OTOROSHI_ISSUER.to_string(),
            exp: Utc::now().timestamp() + 60,
        };

        let token = encode(
            &Header::new(jsonwebtoken::Algorithm::HS512),
            &claims,
            &EncodingKey::from_secret(b"wrong-secret"),
        )
        .unwrap();

        let result = protocol.verify_challenge(&token);
        assert!(result.is_err());
    }

    #[test]
    fn test_verify_challenge_malformed_token() {
        let protocol = OtoroshiProtocol::new(TEST_SECRET, Algorithm::HS512);
        let result = protocol.verify_challenge("not-a-valid-jwt");
        assert!(result.is_err());
    }

    #[test]
    fn test_verify_challenge_missing_issuer() {
        let protocol = OtoroshiProtocol::new(TEST_SECRET, Algorithm::HS512);

        // Token without iss claim should be rejected
        #[derive(Serialize)]
        struct Challenge {
            state: String,
            exp: i64,
        }

        let token = encode(
            &Header::new(jsonwebtoken::Algorithm::HS512),
            &Challenge {
                state: "test".to_string(),
                exp: Utc::now().timestamp() + 60,
            },
            &EncodingKey::from_secret(TEST_SECRET),
        )
        .unwrap();

        let result = protocol.verify_challenge(&token);
        assert!(result.is_err());
    }

    #[test]
    fn test_verify_challenge_wrong_issuer() {
        let protocol = OtoroshiProtocol::new(TEST_SECRET, Algorithm::HS512);

        // Token with wrong iss claim should be rejected
        #[derive(Serialize)]
        struct Challenge {
            state: String,
            iss: String,
            exp: i64,
        }

        let token = encode(
            &Header::new(jsonwebtoken::Algorithm::HS512),
            &Challenge {
                state: "test".to_string(),
                iss: "NotOtoroshi".to_string(),
                exp: Utc::now().timestamp() + 60,
            },
            &EncodingKey::from_secret(TEST_SECRET),
        )
        .unwrap();

        let result = protocol.verify_challenge(&token);
        assert!(result.is_err());
    }

    #[test]
    fn test_verify_challenge_missing_expiration() {
        let protocol = OtoroshiProtocol::new(TEST_SECRET, Algorithm::HS512);

        // Token without exp claim should be rejected
        #[derive(Serialize)]
        struct Challenge {
            state: String,
            iss: String,
        }

        let token = encode(
            &Header::new(jsonwebtoken::Algorithm::HS512),
            &Challenge {
                state: "test".to_string(),
                iss: OTOROSHI_ISSUER.to_string(),
            },
            &EncodingKey::from_secret(TEST_SECRET),
        )
        .unwrap();

        let result = protocol.verify_challenge(&token);
        assert!(result.is_err());
    }

    // -------------------------------------------------------------------------
    // Full V2 flow tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_process_v2_full_flow() {
        let protocol = OtoroshiProtocol::new(TEST_SECRET, Algorithm::HS512);

        // Create a challenge token
        #[derive(Serialize)]
        struct Challenge {
            state: String,
            iss: String,
            iat: i64,
            exp: i64,
        }

        let now = Utc::now().timestamp();
        let challenge_token = encode(
            &Header::new(jsonwebtoken::Algorithm::HS512),
            &Challenge {
                state: "roundtrip-state".to_string(),
                iss: OTOROSHI_ISSUER.to_string(),
                iat: now,
                exp: now + 60,
            },
            &EncodingKey::from_secret(TEST_SECRET),
        )
        .unwrap();

        // Process the challenge
        let response_token = protocol.process_v2(&challenge_token).unwrap();

        // Verify the response contains the correct state
        let mut validation = Validation::new(jsonwebtoken::Algorithm::HS512);
        validation.set_required_spec_claims::<&str>(&[]);
        validation.set_audience(&[OTOROSHI_ISSUER]);

        #[derive(Deserialize)]
        struct Response {
            #[serde(rename = "state-resp")]
            state_resp: String,
        }

        let decoded = decode::<Response>(
            &response_token,
            &DecodingKey::from_secret(TEST_SECRET),
            &validation,
        )
        .unwrap();

        assert_eq!(decoded.claims.state_resp, "roundtrip-state");
    }

    // -------------------------------------------------------------------------
    // Different algorithm tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_create_and_verify_with_hs256() {
        let protocol = OtoroshiProtocol::new(TEST_SECRET, Algorithm::HS256);
        let token = protocol.create_response_token("state").unwrap();

        let mut validation = Validation::new(jsonwebtoken::Algorithm::HS256);
        validation.set_required_spec_claims::<&str>(&[]);
        validation.set_audience(&[OTOROSHI_ISSUER]);

        let result = decode::<serde_json::Value>(
            &token,
            &DecodingKey::from_secret(TEST_SECRET),
            &validation,
        );
        assert!(result.is_ok());
    }

    #[test]
    fn test_create_and_verify_with_hs384() {
        let protocol = OtoroshiProtocol::new(TEST_SECRET, Algorithm::HS384);
        let token = protocol.create_response_token("state").unwrap();

        let mut validation = Validation::new(jsonwebtoken::Algorithm::HS384);
        validation.set_required_spec_claims::<&str>(&[]);
        validation.set_audience(&[OTOROSHI_ISSUER]);

        let result = decode::<serde_json::Value>(
            &token,
            &DecodingKey::from_secret(TEST_SECRET),
            &validation,
        );
        assert!(result.is_ok());
    }

    // -------------------------------------------------------------------------
    // Expiration tests (leeway = 10s, matches Otoroshi default)
    // -------------------------------------------------------------------------

    #[test]
    fn test_verify_challenge_expired_within_leeway() {
        let protocol = OtoroshiProtocol::new(TEST_SECRET, Algorithm::HS512);

        // Create a token that expired 5 seconds ago (within 10s leeway)
        #[derive(Serialize)]
        struct Challenge {
            state: String,
            iss: String,
            exp: i64,
        }

        let now = Utc::now().timestamp();
        let token = encode(
            &Header::new(jsonwebtoken::Algorithm::HS512),
            &Challenge {
                state: "leeway-test".to_string(),
                iss: OTOROSHI_ISSUER.to_string(),
                exp: now - 5, // Expired 5s ago
            },
            &EncodingKey::from_secret(TEST_SECRET),
        )
        .unwrap();

        // Should succeed due to 10s leeway
        let result = protocol.verify_challenge(&token);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "leeway-test");
    }

    #[test]
    fn test_verify_challenge_expired_beyond_leeway() {
        let protocol = OtoroshiProtocol::new(TEST_SECRET, Algorithm::HS512);

        // Create a token that expired 15 seconds ago (beyond 10s leeway)
        #[derive(Serialize)]
        struct Challenge {
            state: String,
            iss: String,
            exp: i64,
        }

        let now = Utc::now().timestamp();
        let token = encode(
            &Header::new(jsonwebtoken::Algorithm::HS512),
            &Challenge {
                state: "expired-test".to_string(),
                iss: OTOROSHI_ISSUER.to_string(),
                exp: now - 15, // Expired 15s ago
            },
            &EncodingKey::from_secret(TEST_SECRET),
        )
        .unwrap();

        // Should fail - beyond leeway
        let result = protocol.verify_challenge(&token);
        assert!(result.is_err());
    }
}