jsonwebtoken/src/lib.rs

88 lines
2.5 KiB
Rust
Raw Normal View History

2015-10-31 11:37:15 -04:00
//! Create and parses JWT (JSON Web Tokens)
//!
2017-04-13 03:36:32 -04:00
//! Documentation: [stable](https://docs.rs/jsonwebtoken/)
2017-04-12 21:08:07 -04:00
#![deny(missing_docs)]
2017-02-22 02:45:28 -05:00
2019-06-16 11:51:43 -04:00
mod algorithms;
2018-10-28 14:58:35 -04:00
mod crypto;
2019-11-09 06:42:40 -05:00
mod decoding;
2019-06-16 11:51:43 -04:00
/// All the errors
2015-10-31 11:37:15 -04:00
pub mod errors;
mod header;
2019-11-03 07:55:36 -05:00
mod pem_decoder;
mod pem_encoder;
2017-02-22 02:45:28 -05:00
mod serialization;
2017-04-11 01:41:44 -04:00
mod validation;
2019-06-16 11:51:43 -04:00
pub use algorithms::Algorithm;
pub use crypto::{sign, verify};
2019-11-09 06:42:40 -05:00
pub use decoding::{dangerous_unsafe_decode, decode, decode_header, decode_rsa_jwk};
pub use header::Header;
2017-07-02 17:49:14 -04:00
pub use serialization::TokenData;
2018-10-28 14:58:35 -04:00
pub use validation::Validation;
2017-04-13 03:36:32 -04:00
use serde::ser::Serialize;
2019-11-09 06:42:40 -05:00
use crate::errors::Result;
2019-11-11 14:16:34 -05:00
use crate::serialization::b64_encode_part;
2017-04-13 03:36:32 -04:00
/// Encode the header and claims given and sign the payload using the algorithm from the header and the key
///
2019-11-09 06:42:40 -05:00
/// ```rust
/// use serde::{Deserialize, Serialize};
2017-04-13 03:36:32 -04:00
/// use jsonwebtoken::{encode, Algorithm, Header};
///
2019-11-09 06:42:40 -05:00
/// #[derive(Debug, Serialize, Deserialize)]
2017-04-13 03:36:32 -04:00
/// struct Claims {
/// sub: String,
/// company: String
/// }
///
/// let my_claims = Claims {
/// sub: "b@b.com".to_owned(),
/// company: "ACME".to_owned()
/// };
///
/// // my_claims is a struct that implements Serialize
/// // This will create a JWT using HS256 as algorithm
/// let token = encode(&Header::default(), &my_claims, "secret".as_ref()).unwrap();
2017-04-13 03:36:32 -04:00
/// ```
pub fn encode<T: Serialize>(header: &Header, claims: &T, key: &[u8]) -> Result<String> {
2019-11-11 14:16:34 -05:00
let encoded_header = b64_encode_part(&header)?;
let encoded_claims = b64_encode_part(&claims)?;
2019-11-09 06:42:40 -05:00
let message = [encoded_header.as_ref(), encoded_claims.as_ref()].join(".");
let signature = sign(&*message, key, header.alg)?;
2017-04-13 03:36:32 -04:00
2019-11-09 06:42:40 -05:00
Ok([message, signature].join("."))
}
/// TODO
pub fn encode_rsa_public_pkcs1_pem(modulus: &[u8], exponent: &[u8]) -> Result<String> {
pem_encoder::encode_rsa_public_pkcs1_pem(modulus, exponent)
}
/// TODO
pub fn encode_rsa_public_pkcs1_der(modulus: &[u8], exponent: &[u8]) -> Result<Vec<u8>> {
pem_encoder::encode_rsa_public_pkcs1_der(modulus, exponent)
}
/// TODO
pub fn encode_rsa_public_pkcs8_pem(modulus: &[u8], exponent: &[u8]) -> Result<String> {
pem_encoder::encode_rsa_public_pkcs8_pem(modulus, exponent)
}
/// TODO
pub fn encode_rsa_public_pkcs8_der(modulus: &[u8], exponent: &[u8]) -> Result<Vec<u8>> {
pem_encoder::encode_rsa_public_pkcs8_der(modulus, exponent)
}
/// TODO
pub fn encode_ec_public_pem(x_coordinate: &[u8]) -> Result<String> {
pem_encoder::encode_ec_public_pem(x_coordinate)
}
/// TODO
pub fn encode_ec_public_der(x_coordinate: &[u8]) -> Result<Vec<u8>> {
pem_encoder::encode_ec_public_der(x_coordinate)
2019-11-06 13:30:59 -05:00
}