cylinder 0.2.1

Cylinder is a cryptographic signing library with CLI tools.
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
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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
/*
 * Copyright 2018-2020 Cargill Incorporated
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ------------------------------------------------------------------------------
 */

//! Provides a set of APIs to both generate JSON Web Tokens (JWTs) with cylinder signing algorithms,
//! as well as parse and cryptographically validate the contents of the strings.
//!
//! The JWT header follows the standard, minimally consisting of a type (`"typ"`) of
//! `"cylinder+jwt"` and an algorithm (`"alg"`) value that depends on the Signer implementation.
//!
//! The claims section of the JWT automatically includes this issuer (`"iss"`) field.  This field
//! is set to public key of the token's signer.  In other words, a Cylinder JWT is issued by the
//! signing party.
//!
//! While this format will still be parseable by other JWT libraries, most likely the signing
//! algorithm specified in the header will not be understood.
//!
//! The token produced is actually JWT-like, as the algorithms currently provided by Cylinder are
//! not part of the standard set.  The implementation only provides the ability to create flat JSON
//! objects, both for the header and for the claims.  Complex, nested JSON objects are beyond the
//! scope of this initial design.
//!
//! Cylinder JWT is guarded by the feature `"jwt"`.
//!
//! # Example
//!
//! ## Creating a token
//!
//! The token is created via the `JsonWebTokenBuilder`, and may be signed with any `Signer`
//! implementation.
//!
//!
//! ```
//! use std::collections::HashMap;
//! use cylinder::{
//!     jwt::JsonWebTokenBuilder, PrivateKey, Context, Signer, secp256k1::Secp256k1Context
//! };
//!
//! let context = Secp256k1Context::new();
//! let private_key = context.new_random_private_key();
//! let signer = context.new_signer(private_key);
//!
//! let mut header = HashMap::new();
//! header.insert("example".into(), "header".into());
//!
//! let mut claims = HashMap::new();
//! claims.insert("example".into(), "claim".into());
//!
//! let encoded_token = JsonWebTokenBuilder::new()
//!     .with_header(header)
//!     .with_claims(claims)
//!     .build(&*signer)
//!     .expect("Unable to generate auth JWT");
//! ```
//!
//! The resulting string is
//!
//!  ```ignore
//!  "[Base-64-encoded bytes of the UTF-8 string of the header JSON].\
//!   [Base-64-encoded bytes of the UTF-8 string of the claims JSON].\
//!   [Base-64-encoded signature]"
//!  ```
//!
//! ## Parsing and Verifying
//!
//! The token produced can be parsed and validated, using a Verifier instance matching the signing
//! algorithm.
//!
//!```
//! # use std::collections::HashMap;
//! # use cylinder::jwt::JsonWebTokenBuilder;
//! # use cylinder::{PrivateKey, Signer};
//! use cylinder::{
//!     jwt::JsonWebTokenParser, Context, Verifier, secp256k1::Secp256k1Context
//! };
//!
//! let context = Secp256k1Context::new();
//! # let private_key = context.new_random_private_key();
//! # let signer = context.new_signer(private_key);
//! # let mut header = HashMap::new();
//! # header.insert("example".into(), "header".into());
//! # let mut claims = HashMap::new();
//! # claims.insert("example".into(), "claim".into());
//! # let encoded_token = JsonWebTokenBuilder::new()
//! #     .with_header(header)
//! #     .with_claims(claims)
//! #     .build(&*signer)
//! #     .expect("Unable to generate auth JWT");
//! let verifier = context.new_verifier();
//! let parser = JsonWebTokenParser::new(&*verifier);
//! let jwt = parser.parse(&encoded_token)
//!     .expect("Unable to parse token");
//!
//! assert_eq!(jwt.header().get("example"), Some(&String::from("header")));
//! assert_eq!(jwt.claims().get("example"), Some(&String::from("claim")));
//! ```

mod error;

use std::collections::HashMap;

use crate::{PublicKey, Signature, Signer, Verifier};

pub use error::{JsonWebTokenBuildError, JsonWebTokenParseError};

/// Builder for constructing the JWT string that would be included in HTTP request headers.
#[derive(Default)]
pub struct JsonWebTokenBuilder {
    header: HashMap<String, String>,
    claims: HashMap<String, String>,
}

impl JsonWebTokenBuilder {
    /// Constructs a new instance of the builder.
    pub fn new() -> Self {
        Self {
            header: HashMap::with_capacity(0),
            claims: HashMap::with_capacity(0),
        }
    }

    /// Sets the header of the token.
    ///
    /// The standard header keys of `alg` and `typ` will be added to the resulting JSON object. If
    /// these keys are included in the given map, they will be overridden at build time.
    pub fn with_header(mut self, header: HashMap<String, String>) -> Self {
        self.header = header;

        self
    }

    /// Sets the claims of the token.
    ///
    /// The standard header of `iss` (issuer) will be added to the resulting JSON object. This will
    /// be set to the public key value of the signer used at build time. If the key is included in
    /// the given map, it will be overridden.
    pub fn with_claims(mut self, claims: HashMap<String, String>) -> Self {
        self.claims = claims;

        self
    }

    /// Serializes and signs the JsonWebToken.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::collections::HashMap;
    /// use cylinder::{
    ///     jwt::JsonWebTokenBuilder, PrivateKey, Context, Signer, secp256k1::Secp256k1Context
    /// };
    ///
    /// let context = Secp256k1Context::new();
    /// let private_key = context.new_random_private_key();
    /// let signer = context.new_signer(private_key);
    ///
    /// let mut header = HashMap::new();
    /// header.insert("example".into(), "header".into());
    ///
    /// let mut claims = HashMap::new();
    /// claims.insert("example".into(), "claim".into());
    ///
    /// let encoded_token = JsonWebTokenBuilder::new()
    ///     .with_header(header)
    ///     .with_claims(claims)
    ///     .build(&*signer)
    ///     .expect("Unable to generate auth JWT");
    /// ```
    ///
    /// The resulting string is
    ///
    ///  ```ignore
    ///  "[Base-64-encoded bytes of the UTF-8 string of the header JSON].\
    ///   [Base-64-encoded bytes of the UTF-8 string of the claims JSON].\
    ///   [Base-64-encoded signature]"
    ///  ```
    ///
    /// # Errors
    ///
    /// A [`JsonWebTokenBuildError`](struct.JsonWebTokenBuildError.html) may be returned if the
    /// token can not be properly built or signed.
    pub fn build(self, signer: &dyn Signer) -> Result<String, JsonWebTokenBuildError> {
        let mut jwt_header = json::JsonValue::new_object();

        for (k, v) in self.header {
            jwt_header[k] = v.into();
        }
        jwt_header["alg"] = signer.algorithm_name().into();
        jwt_header["typ"] = "cylinder+jwt".into();

        // Header bytes are UTF-8 bytes of the JSON string representation of the header
        let header_bytes = json::stringify(jwt_header).into_bytes();

        let public_key = signer.public_key().map_err(|e| {
            JsonWebTokenBuildError::new(
                "Unable to get the public key from the provided signer".into(),
                e,
            )
        })?;

        let mut claims = json::JsonValue::new_object();
        for (k, v) in self.claims {
            claims[k] = v.into()
        }

        claims["iss"] = public_key.as_hex().into();

        // Claims bytes are the UTF-8 bytes of the JSON string representation of the header
        let claims_bytes = json::stringify(claims).into_bytes();

        let mut token = String::new();

        token.push_str(&base64::encode(header_bytes));
        token.push('.');
        token.push_str(&base64::encode(claims_bytes));

        let signature = signer
            .sign(token.as_bytes())
            .map_err(|e| JsonWebTokenBuildError::new("Unable to sign the token".into(), e))?;

        token.push('.');
        token.push_str(&base64::encode(signature.as_slice()));

        Ok(token)
    }
}

/// Parses a [`JsonWebToken`](struct.JsonWebToken.html) from an encoded token.
pub struct JsonWebTokenParser<'a> {
    verifier: &'a dyn Verifier,
}

impl<'a> JsonWebTokenParser<'a> {
    /// Constructs a new parser instance around the given verifier.
    pub fn new(verifier: &'a dyn Verifier) -> Self {
        Self { verifier }
    }

    /// Parses the token string provided and verifies the included signature.
    ///
    /// # Errors
    ///
    /// A [`JsonWebTokenParseError`](enum.JsonWebTokenParseError.html) may be returned if the token
    /// is not properly formed or the signature is not valid.
    pub fn parse(&self, jwt_string: &str) -> Result<JsonWebToken, JsonWebTokenParseError> {
        let mut encoded_token_parts = jwt_string.split('.');
        let (encoded_header, encoded_claims, encoded_signature) = {
            match (
                encoded_token_parts.next(),
                encoded_token_parts.next(),
                encoded_token_parts.next(),
            ) {
                (Some(encoded_header), Some(encoded_claims), Some(encoded_signature)) => {
                    (encoded_header, encoded_claims, encoded_signature)
                }
                (Some(_), Some(_), None) => {
                    return Err(JsonWebTokenParseError::InvalidToken(
                        "Missing signature".into(),
                    ))
                }
                (Some(_), None, _) => {
                    return Err(JsonWebTokenParseError::InvalidToken(
                        "Missing claims".into(),
                    ))
                }
                (None, _, _) => unreachable!(),
            }
        };

        let header = Self::parse_object(encoded_header, |object| {
            if !object.has_key("typ") || object["typ"] != "cylinder+jwt" {
                return Err(JsonWebTokenParseError::InvalidToken(
                    "JWT does not support cylinder extensions".into(),
                ));
            }

            if !object.has_key("alg") || object["alg"] != self.verifier.algorithm_name() {
                return Err(JsonWebTokenParseError::InvalidToken(
                    "JWT does not use a supported algorithm".into(),
                ));
            }

            Ok(())
        })?;

        let claims = Self::parse_object(encoded_claims, |object| {
            if !object.has_key("iss") {
                Err(JsonWebTokenParseError::InvalidToken(
                    "JWT claims has not provided the \"iss\" field".into(),
                ))
            } else {
                Ok(())
            }
        })?;

        let signature_bytes = base64::decode(encoded_signature).map_err(|_| {
            JsonWebTokenParseError::InvalidToken(
                "JWT signature is not valid Base64 encoded bytes".into(),
            )
        })?;

        let public_key = PublicKey::new_from_hex(&claims["iss"]).map_err(|_| {
            JsonWebTokenParseError::InvalidToken(
                "JWT claims do not include a valid public key".into(),
            )
        })?;

        let verified = self
            .verifier
            .verify(
                format!("{}.{}", encoded_header, encoded_claims).as_bytes(),
                &Signature::new(signature_bytes),
                &public_key,
            )
            .map_err(|_| JsonWebTokenParseError::InvalidSignature)?;

        if verified {
            Ok(JsonWebToken {
                header,
                claims,
                issuer: public_key,
            })
        } else {
            Err(JsonWebTokenParseError::InvalidSignature)
        }
    }

    fn parse_object<F>(
        encoded_object: &str,
        validate: F,
    ) -> Result<HashMap<String, String>, JsonWebTokenParseError>
    where
        F: Fn(&json::JsonValue) -> Result<(), JsonWebTokenParseError>,
    {
        let object = base64::decode(encoded_object)
            .map_err(|_| {
                JsonWebTokenParseError::InvalidToken(
                    "JWT object is not valid Base64 encoded bytes".into(),
                )
            })
            .and_then(|object_bytes| {
                String::from_utf8(object_bytes).map_err(|_| {
                    JsonWebTokenParseError::InvalidToken(
                        "JWT object is not valid UTF-8 bytes".into(),
                    )
                })
            })
            .and_then(|object_str| {
                json::parse(&object_str).map_err(|_| {
                    JsonWebTokenParseError::InvalidToken("JWT object is not valid JSON".into())
                })
            })?;

        if !object.is_object() {
            return Err(JsonWebTokenParseError::InvalidToken(
                "Malformed JWT object: expected object".into(),
            ));
        }

        validate(&object)?;

        Ok(object
            .entries()
            .map(|(k, v)| (k.to_string(), v.to_string()))
            .collect())
    }
}

/// Native representation of a JSON web token used for validation.
#[derive(Debug)]
pub struct JsonWebToken {
    issuer: PublicKey,
    header: HashMap<String, String>,
    claims: HashMap<String, String>,
}

impl JsonWebToken {
    /// Returns the header map for this JWT
    pub fn header(&self) -> &HashMap<String, String> {
        &self.header
    }

    /// Returns the claims map for this JWT
    pub fn claims(&self) -> &HashMap<String, String> {
        &self.claims
    }

    /// Returns the public key of the issuer of this JWT
    pub fn issuer(&self) -> &PublicKey {
        &self.issuer
    }
}

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

    use crate::{secp256k1::Secp256k1Context, Context, PrivateKey};

    /// This test constructs a signed Json Web Token string and verifies that it can be read and
    /// verified by the parse method.
    #[test]
    fn test_round_trip() {
        let context = Secp256k1Context::new();
        let private_key = PrivateKey::new(vec![
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 1,
        ]);
        let signer = context.new_signer(private_key);

        let encoded_token = JsonWebTokenBuilder::new()
            .build(&*signer)
            .expect("Unable to generate auth JWT");

        let verifier = context.new_verifier();

        let json_web_token = JsonWebTokenParser::new(&*verifier)
            .parse(&encoded_token)
            .expect("Unable to get public key from JWT");

        assert_eq!(
            &signer.public_key().expect("could not get pubkey"),
            json_web_token.issuer()
        );
    }

    /// This test constructs a signed Json Web Token string using custom header values and verifies
    /// that it can be read and verified by the parse method.
    #[test]
    fn test_round_trip_with_custom_header_values() {
        let context = Secp256k1Context::new();
        let private_key = PrivateKey::new(vec![
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 1,
        ]);
        let signer = context.new_signer(private_key);

        let mut header = HashMap::new();
        header.insert("test".into(), "hello".into());
        let encoded_token = JsonWebTokenBuilder::new()
            .with_header(header)
            .build(&*signer)
            .expect("Unable to generate auth JWT");

        let verifier = context.new_verifier();

        let json_web_token = JsonWebTokenParser::new(&*verifier)
            .parse(&encoded_token)
            .expect("Unable to get public key from JWT");

        assert_eq!(
            &signer.public_key().expect("could not get pubkey"),
            json_web_token.issuer()
        );

        assert_eq!(
            Some(&"hello".to_string()),
            json_web_token.header().get("test")
        );
    }

    /// This test constructs a signed JsonWebToken and then replaces the signature with an invalid
    /// one.  The parse_and_verify method should return an InvalidSignature error variant.
    #[test]
    fn test_bad_signature() {
        let context = Secp256k1Context::new();
        let private_key = PrivateKey::new(vec![
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 1,
        ]);
        let signer = context.new_signer(private_key);

        let encoded_token = JsonWebTokenBuilder::new()
            .build(&*signer)
            .expect("Unable to generate auth JWT");

        // Take the first two parts (the header and the claims).
        let mut token_parts = encoded_token.splitn(3, '.');
        let header = token_parts.next().expect("no header");
        let claims = token_parts.next().expect("no claims");
        let sig = token_parts.next().expect("no signature");
        let bad_sig = std::iter::repeat('0').take(sig.len()).collect::<String>();
        let badly_signed_encoded_token = format!("{}.{}.{}", header, claims, bad_sig);

        let verifier = context.new_verifier();

        match JsonWebTokenParser::new(&*verifier).parse(&badly_signed_encoded_token) {
            Err(JsonWebTokenParseError::InvalidSignature) => (),
            Err(err) => panic!("Unexpected error {:?}", err),
            Ok(_) => panic!("Should not have validated the token"),
        }
    }

    /// This test constructs a signed JsonWebToken and then replaces the signature with a different
    /// signer's signature.  The parse_and_verify method should return an InvalidSignature error
    /// variant.
    #[test]
    fn test_alternate_signer() {
        let context = Secp256k1Context::new();
        let private_key = PrivateKey::new(vec![
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 1,
        ]);
        let signer = context.new_signer(private_key);

        let encoded_token = JsonWebTokenBuilder::new()
            .build(&*signer)
            .expect("Unable to generate auth JWT");

        // Take the first two parts (the header and the claims).
        let mut token_parts = encoded_token.splitn(3, '.');
        let header = token_parts.next().expect("no header");
        let claims = token_parts.next().expect("no claims");

        // construct an alternate signer and sign the token
        let alt_signer = context.new_signer(context.new_random_private_key());

        let bad_sig = alt_signer
            .sign(format!("{}.{}", header, claims).as_bytes())
            .expect("Unable to create new signature");
        let badly_signed_encoded_token = format!("{}.{}.{}", header, claims, bad_sig);

        let verifier = context.new_verifier();

        match JsonWebTokenParser::new(&*verifier).parse(&badly_signed_encoded_token) {
            Err(JsonWebTokenParseError::InvalidSignature) => (),
            Err(err) => panic!("Unexpected error {:?}", err),
            Ok(_) => panic!("Should not have validated the token"),
        }
    }

    /// This test create tokens with badly formatted claims and header values, then it confirms
    /// that the values fail to parse.
    #[test]
    fn test_invalid_claims_json_structure() {
        let context = Secp256k1Context::new();
        let verifier = context.new_verifier();
        let parser = JsonWebTokenParser::new(&*verifier);

        // Test badly formatted claims
        fn test_bad_claims(parser: &JsonWebTokenParser, bad_claims: &str) {
            let header = r#"{
              "typ": "cylinder+jwt",
              "alg": "secp256k1"
            }"#;

            let token = format!(
                "{}.{}.changeme",
                base64::encode(header.as_bytes()),
                base64::encode(bad_claims.as_bytes())
            );

            match parser.parse(&token) {
                Err(JsonWebTokenParseError::InvalidToken(_)) => (),
                Err(err) => panic!("Unexpected error: {:?}", err),
                Ok(_) => panic!("Should not have parsed the token"),
            }
        }

        // invalid JSON type
        test_bad_claims(&parser, "[1, 2, 3]");
        // Missing JSON token
        test_bad_claims(&parser, r#"{"iss": "foobar""#);
        // Invalid JSON
        test_bad_claims(&parser, "bad_json");

        // Test badly formatted claims
        fn test_bad_header(parser: &JsonWebTokenParser, bad_header: &str) {
            let claims = r#"{
              "iss": "somepubkey
            }"#;
            let token = format!(
                "{}.{}.changeme",
                base64::encode(bad_header.as_bytes()),
                base64::encode(claims.as_bytes())
            );

            match parser.parse(&token) {
                Err(JsonWebTokenParseError::InvalidToken(_)) => (),
                Err(err) => panic!("Unexpected error: {:?}", err),
                Ok(_) => panic!("Should not have parsed the token"),
            }
        }

        // invalid JSON type
        test_bad_header(&parser, "[1, 2, 3]");
        // missing opening token
        test_bad_header(
            &parser,
            r#"
          "typ": "cylinder+jwt",
          "alg": "secp256k1"
        }"#,
        );
        // invalid JSON
        test_bad_header(&parser, "invalid_json");
    }

    /// This test creates a token that has bad base64 values.  It should fail to parse.
    #[test]
    fn test_bad_base64() {
        let context = Secp256k1Context::new();
        let verifier = context.new_verifier();
        let parser = JsonWebTokenParser::new(&*verifier);

        match parser.parse("This is bad base64.because it has whitespace.but all the correct parts")
        {
            Err(JsonWebTokenParseError::InvalidToken(_)) => (),
            Err(err) => panic!("Unexpected error: {:?}", err),
            Ok(_) => panic!("Should not have parsed the token"),
        }
    }

    /// This test creates a token with an alternative signing algorithm as well as verifies it
    /// using the same algorithm.
    #[cfg(feature = "hash")]
    #[test]
    fn test_alternative_algorithm() {
        let context = crate::hash::HashContext;
        let private_key = context.new_random_private_key();
        let signer = context.new_signer(private_key);

        let mut header = HashMap::new();
        header.insert("test".into(), "hello".into());

        let encoded_token = JsonWebTokenBuilder::new()
            .with_header(header)
            .build(&*signer)
            .expect("Unable to generate auth JWT");

        let verifier = context.new_verifier();
        let json_web_token = JsonWebTokenParser::new(&*verifier)
            .parse(&encoded_token)
            .expect("Unable to get public key from JWT");

        assert_eq!(
            &signer.public_key().expect("could not get pubkey"),
            json_web_token.issuer()
        );

        assert_eq!(
            Some(&"hello".to_string()),
            json_web_token.header().get("test")
        );
    }

    /// This test creates a token with an alternate signer relative to the verifier used during
    /// parsing of the token.
    #[cfg(feature = "hash")]
    #[test]
    fn test_mismatched_algorithm() {
        let context = crate::hash::HashContext;
        let private_key = context.new_random_private_key();
        let signer = context.new_signer(private_key);

        let encoded_token = JsonWebTokenBuilder::new()
            .build(&*signer)
            .expect("Unable to generate auth JWT");

        let context = Secp256k1Context::new();
        let verifier = context.new_verifier();
        let parser = JsonWebTokenParser::new(&*verifier);

        match parser.parse(&encoded_token) {
            Err(JsonWebTokenParseError::InvalidToken(_)) => (),
            Err(err) => panic!("Unexpected error: {:?}", err),
            Ok(_) => panic!("Should not have parsed the token"),
        }
    }
}