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 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
#![doc = include_str!("../README.md")]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(
rust_2018_idioms,
missing_docs,
missing_debug_implementations,
unused_lifetimes,
unused_qualifications
)]
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(all(feature = "alloc", not(feature = "std")))]
extern crate alloc;
use base64ct::{Base64UrlUnpadded, Encoding};
use core::convert::TryFrom;
#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::{string::String, vec, vec::Vec};
#[cfg(feature = "std")]
use std::{string::String, vec, vec::Vec};
pub use error::Error;
pub mod algorithm;
pub mod error;
pub mod sign;
pub mod time;
pub mod verify;
use error::Result;
/// Represents an unverified JSON Web Token.
///
/// An unverified JWT token may or may not contain valid data. The type attempts basic parsing of
/// the JWT and provides methods to attempt to get the decoded contents.
///
/// After constructing an instance of this type, a developer could decode the header to determine
/// what to do with the JWT.
///
/// ```
/// # fn try_main() -> Result<(), min_jwt::error::Error> {
/// use min_jwt::UnverifiedJwt;
///
/// let jwt = "\
/// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva\
/// G4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\
/// ";
/// let unverified_jwt = UnverifiedJwt::with_str(jwt)?;
///
/// /* if need to read the header */
/// let decoded_header = unverified_jwt.decode_header()?;
///
/// assert_eq!(String::from_utf8(decoded_header).unwrap(), "{\"alg\":\"HS256\",\"typ\":\"JWT\"}");
///
/// /* use Serde or other library to deserialize the decoded header into a custom type */
///
/// /* check the header to determine the algorithm used to sign the JWT */
///
/// /* use a verifier from the min_jwt::verifier package to verify the JWT signature */
///
/// # Ok(())
/// # }
/// # fn main() {
/// # try_main().unwrap();
/// # }
/// ```
#[derive(Debug)]
pub struct UnverifiedJwt<'a> {
/// The encoded header part.
header: &'a str,
/// The encoded claims part.
claims: &'a str,
/// The encoded header part + "." + encoded claims part.
signed_data: &'a str,
/// The encoded signature.
signature: &'a str,
}
#[derive(Debug)]
struct SplitJwt<'a> {
header: &'a str,
claims: &'a str,
signed_data: &'a str,
signature: &'a str,
}
impl<'a> TryFrom<&'a str> for UnverifiedJwt<'a> {
type Error = Error;
fn try_from(value: &'a str) -> Result<Self> {
let split_jwt = Self::split(value)?;
Ok(UnverifiedJwt {
header: split_jwt.header,
claims: split_jwt.claims,
signed_data: split_jwt.signed_data,
signature: split_jwt.signature,
})
}
}
impl<'a> UnverifiedJwt<'a> {
/// Attempts to construct an `UnverifiedJwt`.
///
/// Only basic parsing is done with this method, so, even if an `UnverifiedJwt` is successfully
/// returned, the JWT may contain invalid data (e.g any of the parts may not be correctly
/// base64 encoded).
///
/// # Errors
///
/// The function may return an error variant because the string slice is an invalid JWT string.
///
/// ```
/// # use min_jwt::Error;
/// #
/// # fn try_main() -> Result<(), Error> {
/// use min_jwt::UnverifiedJwt;
///
/// let jwt = "\
/// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva\
/// G4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\
/// ";
/// let unverified_jwt = UnverifiedJwt::with_str(jwt)?;
/// # Ok(())
/// # }
/// # fn main() {
/// # try_main().unwrap();
/// # }
/// ```
pub fn with_str(jwt: &str) -> Result<UnverifiedJwt<'_>> {
let split_jwt = Self::split(jwt)?;
Ok(UnverifiedJwt {
header: split_jwt.header,
claims: split_jwt.claims,
signed_data: split_jwt.signed_data,
signature: split_jwt.signature,
})
}
/// Decodes the header part by parsing the JWT for the header and base64 decoding the header.
///
/// # Errors
///
/// If the header part is not correctly base64 encoded, the function will return an error variant.
///
/// ```
/// # use min_jwt::Error;
/// #
/// # fn try_main() -> Result<(), Error> {
/// use min_jwt::UnverifiedJwt;
///
/// let jwt = "\
/// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva\
/// G4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\
/// ";
/// let unverified_jwt = UnverifiedJwt::with_str(&jwt)?;
///
/// let decoded_header = unverified_jwt.decode_header()?;
///
/// assert_eq!(String::from_utf8(decoded_header).unwrap(), "{\"alg\":\"HS256\",\"typ\":\"JWT\"}");
///
/// /* use Serde or other library to deserialize the decoded header */
///
/// # Ok(())
/// # }
/// # fn main() {
/// # try_main().unwrap();
/// # }
/// ```
pub fn decode_header(&self) -> Result<Vec<u8>> {
Ok(Base64UrlUnpadded::decode_vec(self.header)?)
}
// Currently not pub. Should a SignatureVerifiedJwt be required before looking at the claims?
/// Decodes the claims part by parsing the JWT for the claims and base64 decoding the claims.
///
/// # Errors
///
/// If the claims part is not correctly base64 encoded, the function will return an error variant.
fn decode_claims(&self) -> Result<Vec<u8>> {
Ok(Base64UrlUnpadded::decode_vec(self.claims)?)
}
/// Decodes the signature part by parsing the JWT for the signature and base64 decoding the
/// signature.
///
/// # Errors
///
/// If the signature part is not correctly base64 encoded, the function will return an error variant.
///
/// ```
/// # use min_jwt::Error;
/// #
/// # fn try_main() -> Result<(), Error> {
/// use min_jwt::UnverifiedJwt;
///
/// let jwt = "\
/// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva\
/// G4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\
/// ";
/// let unverified_jwt = UnverifiedJwt::with_str(&jwt)?;
///
/// let decoded_signature = unverified_jwt.decode_signature()?;
///
/// /* use a cryptography library to verify the signed data with the decoded signature */
///
/// # Ok(())
/// # }
/// # fn main() {
/// # try_main().unwrap();
/// # }
/// ```
pub fn decode_signature(&self) -> Result<Vec<u8>> {
Ok(Base64UrlUnpadded::decode_vec(self.signature)?)
}
/// Returns the signed data.
///
/// The signed data is the encoded header + "." + encoded claims.
///
/// ```
/// # use min_jwt::Error;
/// #
/// # fn try_main() -> Result<(), Error> {
/// use min_jwt::UnverifiedJwt;
///
/// let jwt = "\
/// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva\
/// G4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\
/// ";
/// let unverified_jwt = UnverifiedJwt::with_str(&jwt)?;
///
/// assert_eq!("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ", unverified_jwt.signed_data());
///
/// # Ok(())
/// # }
/// # fn main() {
/// # try_main().unwrap();
/// # }
/// ```
#[must_use]
pub fn signed_data(&self) -> &'a str {
self.signed_data
}
/// Returns the encoded header part.
///
/// Practically, the `decode_header` method is more useful since the returned data from this
/// method is still base64 encoded.
///
/// The encoded header is available for debugging purposes.
///
/// ```
/// # use min_jwt::Error;
/// #
/// # fn try_main() -> Result<(), Error> {
/// use min_jwt::UnverifiedJwt;
///
/// let jwt = "\
/// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva\
/// G4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\
/// ";
/// let unverified_jwt = UnverifiedJwt::with_str(&jwt)?;
///
/// assert_eq!("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9", unverified_jwt.encoded_header());
///
/// # Ok(())
/// # }
/// # fn main() {
/// # try_main().unwrap();
/// # }
/// ```
#[must_use]
pub fn encoded_header(&self) -> &'a str {
self.header
}
/// Returns the encoded signature part.
///
/// Practically, the `decode_signature` method is more useful since the returned data from this
/// method is still base64 encoded.
///
/// The encoded signature is available for debugging purposes.
///
/// ```
/// # use min_jwt::Error;
/// #
/// # fn try_main() -> Result<(), Error> {
/// use min_jwt::UnverifiedJwt;
///
/// let jwt = "\
/// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva\
/// G4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\
/// ";
/// let unverified_jwt = UnverifiedJwt::with_str(&jwt)?;
///
/// assert_eq!("SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c", unverified_jwt.encoded_signature());
///
/// # Ok(())
/// # }
/// # fn main() {
/// # try_main().unwrap();
/// # }
/// ```
#[must_use]
pub fn encoded_signature(&self) -> &'a str {
self.signature
}
fn split(jwt: &str) -> Result<SplitJwt<'_>> {
let mut parts = jwt.rsplitn(2, '.');
let signature = parts.next().ok_or_else(Error::malformed_jwt)?;
let signed_data = parts.next().ok_or_else(Error::malformed_jwt)?;
if parts.next().is_some() {
return Err(Error::malformed_jwt());
}
let mut parts = signed_data.rsplitn(3, '.');
let claims = parts.next().ok_or_else(Error::malformed_jwt)?;
let header = parts.next().ok_or_else(Error::malformed_jwt)?;
if parts.next().is_some() {
return Err(Error::malformed_jwt());
}
Ok(SplitJwt {
header,
claims,
signed_data,
signature,
})
}
}
/// Represents a JSON Web Token which has had its signature verified.
///
/// A signature verified JWT contains signed data which was verified with the included
/// signature. The signed data is the encoded header + "." + encoded claims.
///
/// ```
/// # use min_jwt::Error;
/// #
/// # #[cfg(feature = "ring")]
/// # fn try_main() -> Result<(), Error> {
/// use ring::hmac;
///
/// let jwt = "\
/// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva\
/// G4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\
/// ";
///
/// let hmac_key_bytes = String::from("your-256-bit-secret").into_bytes();
/// let hmac_key = hmac::Key::new(hmac::HMAC_SHA256, &hmac_key_bytes);
///
/// let verifier = min_jwt::verify::ring::HmacKeyVerifier::with_hs256(&hmac_key);
/// let signature_verified_jwt = min_jwt::verify(jwt, &verifier)?;
///
/// let decoded_claims = signature_verified_jwt.decode_claims()?;
///
/// /* validate claims */
/// # Ok(())
/// # }
/// # fn main() {
/// # #[cfg(feature = "ring")]
/// # try_main().unwrap();
/// # }
/// ```
#[derive(Debug)]
pub struct SignatureVerifiedJwt<'a> {
unverified_jwt: UnverifiedJwt<'a>,
}
impl<'a> SignatureVerifiedJwt<'a> {
/// Decodes the header part by parsing the JWT for the header and base64 decoding the header.
///
/// # Errors
///
/// If the header part is not correctly base64 encoded, the function will return an error variant.
///
/// ```
/// # use min_jwt::Error;
/// #
/// # #[cfg(feature = "ring")]
/// # fn try_main() -> Result<(), Error> {
/// use ring::hmac;
///
/// let jwt = "\
/// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva\
/// G4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\
/// ";
///
/// let hmac_key_bytes = String::from("your-256-bit-secret").into_bytes();
/// let hmac_key = hmac::Key::new(hmac::HMAC_SHA256, &hmac_key_bytes);
///
/// let verifier = min_jwt::verify::ring::HmacKeyVerifier::with_hs256(&hmac_key);
/// let signature_verified_jwt = min_jwt::verify(jwt, &verifier)?;
///
/// let decoded_header = signature_verified_jwt.decode_header()?;
///
/// assert_eq!(String::from_utf8(decoded_header).unwrap(), "{\"alg\":\"HS256\",\"typ\":\"JWT\"}");
///
/// # Ok(())
/// # }
/// # fn main() {
/// # #[cfg(feature = "ring")]
/// # try_main().unwrap();
/// # }
/// ```
#[inline]
pub fn decode_header(&self) -> Result<Vec<u8>> {
self.unverified_jwt.decode_header()
}
/// Decodes the claims part by parsing the JWT for the claims and base64 decoding the claims.
///
/// # Errors
///
/// If the claims part is not correctly base64 encoded, the function will return an error variant.
///
/// ```
/// # use min_jwt::Error;
/// #
/// # #[cfg(feature = "ring")]
/// # fn try_main() -> Result<(), Error> {
/// use ring::hmac;
///
/// let jwt = "\
/// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva\
/// G4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\
/// ";
///
/// let hmac_key_bytes = String::from("your-256-bit-secret").into_bytes();
/// let hmac_key = hmac::Key::new(hmac::HMAC_SHA256, &hmac_key_bytes);
///
/// let verifier = min_jwt::verify::ring::HmacKeyVerifier::with_hs256(&hmac_key);
/// let signature_verified_jwt = min_jwt::verify(jwt, &verifier)?;
///
/// let decoded_claims = signature_verified_jwt.decode_claims()?;
///
/// assert_eq!(String::from_utf8(decoded_claims).unwrap(), "{\"sub\":\"1234567890\",\"name\":\"John Doe\",\"iat\":1516239022}");
///
/// # Ok(())
/// # }
/// # fn main() {
/// # #[cfg(feature = "ring")]
/// # try_main().unwrap();
/// # }
/// ```
#[inline]
pub fn decode_claims(&self) -> Result<Vec<u8>> {
self.unverified_jwt.decode_claims()
}
/// Decodes the signature part by parsing the JWT for the signature and base64 decoding the
/// signature.
///
/// # Errors
///
/// If the signature part is not correctly base64 encoded, the function will return an error variant.
///
/// ```
/// # use min_jwt::Error;
/// #
/// # #[cfg(feature = "ring")]
/// # fn try_main() -> Result<(), Error> {
/// use ring::hmac;
///
/// let jwt = "\
/// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva\
/// G4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\
/// ";
///
/// let hmac_key_bytes = String::from("your-256-bit-secret").into_bytes();
/// let hmac_key = hmac::Key::new(hmac::HMAC_SHA256, &hmac_key_bytes);
///
/// let verifier = min_jwt::verify::ring::HmacKeyVerifier::with_hs256(&hmac_key);
/// let signature_verified_jwt = min_jwt::verify(jwt, &verifier)?;
///
/// let decoded_signature = signature_verified_jwt.decode_signature()?;
///
/// /* use a cryptography library to verify the signed data with the decoded signature */
///
/// # Ok(())
/// # }
/// # fn main() {
/// # #[cfg(feature = "ring")]
/// # try_main().unwrap();
/// # }
/// ```
#[inline]
pub fn decode_signature(&self) -> Result<Vec<u8>> {
self.unverified_jwt.decode_signature()
}
/// Returns the signed data.
///
/// The signed data is the encoded header + "." + encoded claims.
///
/// ```
/// # use min_jwt::Error;
/// #
/// # #[cfg(feature = "ring")]
/// # fn try_main() -> Result<(), Error> {
/// use ring::hmac;
///
/// let jwt = "\
/// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva\
/// G4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\
/// ";
///
/// let hmac_key_bytes = String::from("your-256-bit-secret").into_bytes();
/// let hmac_key = hmac::Key::new(hmac::HMAC_SHA256, &hmac_key_bytes);
///
/// let verifier = min_jwt::verify::ring::HmacKeyVerifier::with_hs256(&hmac_key);
/// let signature_verified_jwt = min_jwt::verify(jwt, &verifier)?;
///
/// assert_eq!("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ", signature_verified_jwt .signed_data());
///
/// # Ok(())
/// # }
/// # fn main() {
/// # #[cfg(feature = "ring")]
/// # try_main().unwrap();
/// # }
/// ```
#[inline]
#[must_use]
pub fn signed_data(&self) -> &'a str {
self.unverified_jwt.signed_data()
}
/// Returns the encoded header part.
///
/// Practically, the `decode_header` method is more useful since the returned data from this
/// method is still base64 encoded.
///
/// The encoded header is available for debugging purposes.
///
/// ```
/// # use min_jwt::Error;
/// #
/// # #[cfg(feature = "ring")]
/// # fn try_main() -> Result<(), Error> {
/// use ring::hmac;
///
/// let jwt = "\
/// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva\
/// G4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\
/// ";
///
/// let hmac_key_bytes = String::from("your-256-bit-secret").into_bytes();
/// let hmac_key = hmac::Key::new(hmac::HMAC_SHA256, &hmac_key_bytes);
///
/// let verifier = min_jwt::verify::ring::HmacKeyVerifier::with_hs256(&hmac_key);
/// let signature_verified_jwt = min_jwt::verify(jwt, &verifier)?;
///
/// assert_eq!("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9", signature_verified_jwt.encoded_header());
///
/// /* use a cryptography library to verify the signed data with the decoded signature */
///
/// # Ok(())
/// # }
/// # fn main() {
/// # #[cfg(feature = "ring")]
/// # try_main().unwrap();
/// # }
/// ```
#[inline]
#[must_use]
pub fn encoded_header(&self) -> &'a str {
self.unverified_jwt.encoded_header()
}
/// Returns the encoded claims part.
///
/// Practically, the `decode_claims` method is more useful since the returned data from this
/// method is still base64 encoded.
///
/// The encoded claims is available for debugging purposes.
///
///
/// ```
/// # use min_jwt::Error;
/// #
/// # #[cfg(feature = "ring")]
/// # fn try_main() -> Result<(), Error> {
/// use ring::hmac;
///
/// let jwt = "\
/// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva\
/// G4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\
/// ";
///
/// let hmac_key_bytes = String::from("your-256-bit-secret").into_bytes();
/// let hmac_key = hmac::Key::new(hmac::HMAC_SHA256, &hmac_key_bytes);
///
/// let verifier = min_jwt::verify::ring::HmacKeyVerifier::with_hs256(&hmac_key);
/// let signature_verified_jwt = min_jwt::verify(jwt, &verifier)?;
///
/// assert_eq!("eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ", signature_verified_jwt.encoded_claims());
///
/// /* use a cryptography library to verify the signed data with the decoded signature */
///
/// # Ok(())
/// # }
/// # fn main() {
/// # #[cfg(feature = "ring")]
/// # try_main().unwrap();
/// # }
/// ```
#[inline]
#[must_use]
pub fn encoded_claims(&self) -> &'a str {
self.unverified_jwt.claims
}
/// Returns the encoded signature part.
///
/// Practically, the `decode_signature` method is more useful since the returned data from this
/// method is still base64 encoded.
///
/// The encoded signature is available for debugging purposes.
///
/// ```
/// # use min_jwt::Error;
/// #
/// # #[cfg(feature = "ring")]
/// # fn try_main() -> Result<(), Error> {
/// use ring::hmac;
///
/// let jwt = "\
/// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva\
/// G4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\
/// ";
///
/// let hmac_key_bytes = String::from("your-256-bit-secret").into_bytes();
/// let hmac_key = hmac::Key::new(hmac::HMAC_SHA256, &hmac_key_bytes);
///
/// let verifier = min_jwt::verify::ring::HmacKeyVerifier::with_hs256(&hmac_key);
/// let signature_verified_jwt = min_jwt::verify(jwt, &verifier)?;
///
/// assert_eq!("SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c", signature_verified_jwt.encoded_signature());
///
/// /* use a cryptography library to verify the signed data with the decoded signature */
///
/// # Ok(())
/// # }
/// # fn main() {
/// # #[cfg(feature = "ring")]
/// # try_main().unwrap();
/// # }
/// ```
#[inline]
#[must_use]
pub fn encoded_signature(&self) -> &'a str {
self.unverified_jwt.encoded_signature()
}
}
/// A marker trait for a JWT's header.
pub trait Header {}
/// A marker trait for a JWT's claims.
pub trait Claims {}
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
/// Contains the algorithm and the key ID used to sign the JWT.
///
/// The `BasicHeader` type is intended to be used for generic algorithms which
/// only require common information in JWTs. If more specific fields need to be
/// deserialized, a custom application specific type would be required to
/// deserialize all of the fields.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[non_exhaustive]
pub struct BasicHeader<'a> {
/// The signing algorithm.
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub alg: Option<&'a str>,
/// The key ID.
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub kid: Option<&'a str>,
/// The type of token.
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub typ: Option<&'a str>,
}
impl<'a> Header for BasicHeader<'a> {}
/// Contains the issuer ID, when the token was issued, and when the token
/// expires.
///
/// The `BasicClaims` type is intended to be used for generic algorithms which
/// only require common information in JWTs. For most applications, a custom
/// application specific type would be required to deserialize all of the
/// fields.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[non_exhaustive]
pub struct BasicClaims<'a> {
/// The issuer of the token.
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub iss: Option<&'a str>,
/// When the token was issued as the number of seconds since the Unix epoch.
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub iat: Option<u64>,
/// When the token should expire as the number of seconds since the Unix epoch.
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub exp: Option<u64>,
/// The intended audience.
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub aud: Option<&'a str>,
/// The subject.
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub sub: Option<&'a str>,
}
impl<'a> Claims for BasicClaims<'a> {}
/// Serializes the types to JSON, base64 encodes the JSON, constructs the
/// signing input, signs the data, and then returns the JWT.
///
/// # Errors
///
/// The function may return an error variant because the key pair is invalid.
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
#[cfg_attr(docsrs, doc(cfg(feature = "serde_json")))]
#[cfg(all(feature = "serde", feature = "serde_json"))]
pub fn serialize_encode_and_sign<H, C, S>(header: &H, claims: &C, signing_key: S) -> Result<String>
where
H: Header + Serialize,
C: Claims + Serialize,
S: sign::Signer,
{
let header = serde_json::to_vec(&header).map_err(|_| Error::unspecified())?;
let claims = serde_json::to_vec(&claims).map_err(|_| Error::unspecified())?;
encode_and_sign(&header, &claims, signing_key)
}
/// Base64 encodes byte representations of the header and claims, constructs the
/// signing input, signs the data, and then returns the JWT.
///
/// # Errors
///
/// The function may return an error variant because the key pair is invalid.
pub fn encode_and_sign<S>(header: &[u8], claims: &[u8], signing_key: S) -> Result<String>
where
S: sign::Signer,
{
let encoded_header_len = Base64UrlUnpadded::encoded_len(header);
let signed_data_len = encoded_header_len + Base64UrlUnpadded::encoded_len(claims) + 1;
let mut output = vec![0; signed_data_len];
Base64UrlUnpadded::encode(header, &mut output[..encoded_header_len])?;
output[encoded_header_len] = b'.';
Base64UrlUnpadded::encode(claims, &mut output[encoded_header_len + 1..])?;
let signature = signing_key
.sign(&output)
.map_err(|_| Error::unspecified())?;
let signature = signature.as_ref();
let final_len = signed_data_len + 1 + Base64UrlUnpadded::encoded_len(signature);
output.reserve_exact(final_len);
output.resize(final_len, 0);
output[signed_data_len] = b'.';
Base64UrlUnpadded::encode(signature, &mut output[signed_data_len + 1..])?;
Ok(String::from_utf8(output).map_err(|_| base64ct::InvalidEncodingError)?)
}
/// Attempts to verify a JWT's signature.
///
/// # Errors
///
/// If the public key or signature is invalid, the function will return an error variant.
pub fn verify<V>(unverified_jwt: &str, verifying_key: V) -> Result<SignatureVerifiedJwt<'_>>
where
V: verify::Verifier,
{
let unverified_jwt = UnverifiedJwt::with_str(unverified_jwt)?;
let signed_data = unverified_jwt.signed_data().as_bytes();
let decoded_signature = unverified_jwt.decode_signature()?;
verifying_key
.verify(signed_data, &decoded_signature)
.map(|_| SignatureVerifiedJwt { unverified_jwt })
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(any(feature = "ring", feature = "p256"))]
pub(crate) fn jwt_claims_str() -> String {
String::from(
"{\"sub\":\"1234567890\",\"name\":\"John Doe\",\"admin\":true,\"iat\":1516239022}",
)
}
#[test]
fn split_unverified_jwt_normal_parts() {
let jwt = String::from("abc.defg.vwxyz");
let SplitJwt {
header,
claims,
signed_data,
signature,
} = UnverifiedJwt::split(&jwt).unwrap();
assert_eq!("abc", header);
assert_eq!("defg", claims);
assert_eq!("abc.defg", signed_data);
assert_eq!("vwxyz", signature);
}
#[test]
fn with_str_unverified_jwt_normal_parts() {
let jwt = String::from("abc.defg.vwxyz");
let unverified_jwt = UnverifiedJwt::with_str(&jwt).unwrap();
assert_eq!("abc", unverified_jwt.encoded_header());
assert_eq!("abc.defg", unverified_jwt.signed_data());
assert_eq!("vwxyz", unverified_jwt.encoded_signature());
}
#[test]
fn split_unverified_jwt_no_data_in_parts() {
let jwt = String::from("..");
let SplitJwt {
header,
claims,
signed_data,
signature,
} = UnverifiedJwt::split(&jwt).unwrap();
assert_eq!("", header);
assert_eq!("", claims);
assert_eq!(".", signed_data);
assert_eq!("", signature);
}
#[test]
fn with_str_unverified_jwt_no_data_in_parts() {
let jwt = String::from("..");
let unverified_jwt = UnverifiedJwt::with_str(&jwt).unwrap();
assert_eq!("", unverified_jwt.encoded_header());
assert_eq!(".", unverified_jwt.signed_data());
assert_eq!("", unverified_jwt.encoded_signature());
}
#[test]
fn split_unverified_jwt_too_many_parts() {
let jwt = String::from("abc.defg.lmnop.vwxyz");
let error = UnverifiedJwt::split(&jwt).unwrap_err();
assert!(error.is_malformed_jwt());
}
#[test]
fn with_str_unverified_jwt_too_many_parts() {
let jwt = String::from("abc.defg.lmnop.vwxyz");
let error = UnverifiedJwt::with_str(&jwt).unwrap_err();
assert!(error.is_malformed_jwt());
}
#[test]
fn split_unverified_jwt_too_few_parts() {
let jwt = String::from("abc.defg");
let error = UnverifiedJwt::split(&jwt).unwrap_err();
assert!(error.is_malformed_jwt());
}
#[test]
fn with_str_unverified_jwt_too_few_parts() {
let jwt = String::from("abc.defg");
let error = UnverifiedJwt::with_str(&jwt).unwrap_err();
assert!(error.is_malformed_jwt());
}
}
#[cfg(feature = "p256")]
#[cfg_attr(docsrs, doc(cfg(feature = "p256")))]
mod p256;
#[cfg(feature = "ring")]
#[cfg_attr(docsrs, doc(cfg(feature = "ring")))]
mod ring;
#[cfg(feature = "rsa")]
#[cfg_attr(docsrs, doc(cfg(feature = "rsa")))]
mod rsa;