Struct rustwt::Decoder
[−]
[src]
pub struct Decoder { /* fields omitted */ }
Basic structure for decoding JWT.
Example public key signature
use rustwt::{Payload,Encoder,Algorithm,Decoder,Value}; // you can use RSA keys as well. Just adjust the algorithm. let ec_private_key: &str = include_str!("../test/ec_x9_62_prime256v1.private.key.pem"); let ec_public_key: &str = include_str!("../test/ec_x9_62_prime256v1.public.key.pem"); let mut p1 = Payload::new(); p1.insert("key12".to_string(), Value::String("val1".to_string())); p1.insert("key22".to_string(), Value::String("val2".to_string())); p1.insert("key33".to_string(), Value::String("val3".to_string())); let encoder = Encoder::from_raw_private_key(ec_private_key, Algorithm::ES256).unwrap(); let decoder = Decoder::from_pem(ec_public_key).unwrap(); let jwt1 = encoder.encode(p1.clone()).expect("could not encode token"); let maybe_res = decoder.decode(jwt1);
Example hmac
use rustwt::{Payload,Encoder,Algorithm,Decoder,Value}; let secret: &str = "secret123"; let mut p1 = Payload::new(); p1.insert("key12".to_string(), Value::String("val1".to_string())); p1.insert("key22".to_string(), Value::String("val2".to_string())); p1.insert("key33".to_string(), Value::String("val3".to_string())); let encoder = Encoder::from_raw_private_key(secret, Algorithm::HS256).unwrap(); let decoder = Decoder::from_hmac_secret(secret).unwrap(); let jwt1 = encoder.encode(p1.clone()).expect("could not encode token"); let maybe_res = decoder.decode(jwt1);
Methods
impl Decoder
[src]
fn from_hmac_secret<T: ?Sized + AsRef<[u8]>>(
secret: &T
) -> Result<Decoder, Error>
[src]
secret: &T
) -> Result<Decoder, Error>
fn from_pem<T: ?Sized + AsRef<[u8]>>(raw_key: &T) -> Result<Decoder, Error>
[src]
fn from_public_key(key: PKey) -> Decoder
[src]
fn decode<T: AsRef<str>>(&self, token: T) -> Result<(Header, Payload), Error>
[src]
This function decodes a valid base64 encoded token. If the token is invalid, an appropriate error will be returned.