Conversation
|
@Keats |
|
For EncodingKey not really, we could remove the lifetime. For DecodingKey we do not want to clone the secret/keys everytime we verify a token so they are needed. |
|
DecodingKey is the bigger problem for me. To avoid "lifetime baggage", I'll probably end up creating DecodingKey on every request (passing it a pem String), and that's not putting me in a much better place than cloning on every decode. |
|
Why not instantiate it in a lazy_static or similar? The whole point of that API is to allow re-using the Encoding/Decoding keys to make it more performant. |
|
@Keats /// If you are loading a public RSA key in a PEM format, use this.
pub fn from_rsa_pem(key: String) -> Result<Self> {
let pem_key = PemEncodedKey::new(key.as_bytes())?;
let content = pem_key.as_rsa_key()?;
Ok(DecodingKey { kind: DecodingKeyKind::SecretOrDer(Cow::Owned(content.to_vec())) })
} an example of the lazy_static: lazy_static! {
static ref JWT_DECODING_KEY: DecodingKey<'static> =
DecodingKey::from_rsa_pem(
fs::read_to_string(
env::var("JWT_PUBKEY")
.unwrap_or_else(|_| panic!("Failed to load JWT public key envvar"))
).unwrap()
).unwrap_or_else(|_| panic!("Failed to create JWT DecodingKey"));
}and preferably make the same key param change to String for |
|
What do you think about modifying the from_rsa_pem functions accordingly? |
|
I would prefer keeping lazy_static! {
static ref SOME_STRING: String = env::var("JWT_PUBKEY").unwrap();
// And there you can have your DecodingKey with &[u8] I think
static ref OTHER: &'static [u8] = SOME_STRING.as_bytes();
} |
|
@Keats I think I'm creating PEM files in an unsupported format. what command are you using to generate your PEM files? I was using this but the format is failing: |
|
x509 is not supported Something like: |
|
@Keats this PR seems sufficient for my needs.. no further issues/concerns to report |
|
@Keats essentially, what are we achieving with these changes? |
|
You ensure the key is of the same type as the algorithm so you can't misuse it. Eg you want to decode a RSA token but put a HMAC key instead and it will fail immediately. It's not prevented at compile time sadly but that's better than nothing. I don't think we can have type safety at compile time without have one encode/decode function per algorithm family |
Add EncodingKey & DecodingKey
No description provided.