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
use Serialize;
use crateAlgorithm;
use crateError;
use crate*;
/// Encodes a Json Web Token
///
/// For example, to encode and sign a token with a symmetric secret:
/// ```rust
/// # use serde_json::json;
/// # use serde_json::value::Value;
/// # use jsonwebtokens as jwt;
/// # use jwt::{Algorithm, AlgorithmID, Verifier};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let alg = Algorithm::new_hmac(AlgorithmID::HS256, "secret")?;
/// let header = json!({ "alg": alg.name() });
/// let claims = json!({ "foo": "bar" });
/// let token = jwt::encode(&header, &claims, &alg)?;
/// # Ok(())
/// # }
/// ```
///
/// Or to encode and sign a token with an RSA private key:
/// ```rust
/// # use serde_json::json;
/// # use serde_json::value::Value;
/// # use jsonwebtokens as jwt;
/// # use jwt::{Algorithm, AlgorithmID, Verifier};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let pem_data = include_bytes!("../tests/rsa/private_rsa_key_pkcs1.pem");
/// let alg = Algorithm::new_rsa_pem_signer(AlgorithmID::RS256, pem_data)?;
/// let header = json!({ "alg": alg.name() });
/// let claims = json!({ "foo": "bar" });
/// let token = jwt::encode(&header, &claims, &alg)?;
/// # Ok(())
/// # }
/// ```