Keats / jsonwebtoken

JWT lib in rust
MIT License
1.69k stars 271 forks source link

Add from_ methods that take Algorithm as a param #132

Closed jkoudys closed 2 years ago

jkoudys commented 4 years ago

It's very easy to get the Algorithm from my config file, since you've kindly provided a serde::de::Deserialize trait on it. While it's beautiful being able to write a json config like "https://myissuer.com": { algo: "RS256", pubkey: "../mykey.pub" }, building the actual EncodingKey out of that gets ugly. it'd be lovely if there were something like:

 pub fn from_pem(algo: Algorithm, key: &[u8]) -> Result<Self> {
   match algo.family() {
     AlgorithmFamily::Rsa => Ok(Self::from_rsa_pem(key)),
     AlgorithmFamily::Ec => Ok(Self::from_ec_pem(key)),
     _ => Err(NoPemForThis),
  }
}

As it stands I don't even have access to family(), so I can't get it to do this automatically.

Keats commented 4 years ago

I thought about that a while back but I didn't like that you could end up with errors that could be avoided by the current functions; the NoPemForThis in your example.