Closed tjwilson90 closed 2 years ago
The intent was indeed to use it in a lazy_static way, something like (pseudo-code)
lazy_static! {
let decoding_key = {
let bytes = include_bytes!("public.pem");
DecodingKey::from_rsa_pem(bytes).unwrap()
}
}
What would you change?
That works, but only if I embed my public key in my program (which would require me to recompile and redeploy if I change my key, and would make it impossible for a user of my pre-compiled program to generate and use their own keys).
Since the resulting DecodingKey doesn't need to maintain a borrow on the key, the key should have a distinct lifetime, i.e., the type of key should be &[u8]
, not &'a [u8]
.
Just ran into this as well. It'd be way more versatile if DecodingKey's methods just borrowed or consumed the key, depending on internal implementation, rather than requiring an external guarantee about the lifetime.
I've pushed a stop-gap change in 7.1.0, more changes can be done later if necessary as breaking change
Right now my setup is something like this:
lazy_static! {
pub static ref JWT_ENCODING_KEY:EncodingKey = {
let secret = std::env::var("JWT_SECRET").expect("must have JWT_SECRET set");
EncodingKey::from_secret(secret.as_ref())
};
pub static ref JWT_DECODING_KEY:DecodingKey<'static> = {
let secret = std::env::var("JWT_SECRET").expect("must have JWT_SECRET set");
DecodingKey::from_secret(secret.as_ref())
};
It fails to compile of course.
EncodingKey is fine, but DecodingKey fails since secret
is dropped right away.
imho DecodingKey should really just own its data. There's really only two scenarios iirc:
Am I missing something?
https://github.com/Keats/jsonwebtoken/pull/128 should make it easier but that's a breaking change.
How about adding this to that breaking change as well?
impl DecodingKey<'static> {
pub fn from_secret_clone(secret: &[u8]) -> Self {
DecodingKey {
family: AlgorithmFamily::Hmac,
kind: DecodingKeyKind::SecretOrDer(Cow::Owned(secret.to_vec())),
}
}
}
Can we work on a PR with breaking changes for the future 8.0? You can go wild
cc @neoeinstein
As well as any other issues for EncodingKey or really anything else.
Anyone up to do a PR on https://github.com/Keats/jsonwebtoken/pull/160 ?
Can people try https://github.com/Keats/jsonwebtoken/pull/160 to see if it solves all the issues they had?
It's also on crates.io as v8 beta 0
Can people try #160 to see if it solves all the issues they had?
Wooo, thank you. It seems to work so far.
Oh nice! I was hoping to get back to the project where I use this library some time this week, I'll give it a whirl then.
I had the same issue, works perfectly in 8.0.0-beta.2
. Thanks !
is there any ETA on this change making it into a release? I'd like to use this as well once a formal new release exists.
...and if there is anything I can help with towards that, please let me know
I need to release a new v8 beta soonish after merging https://github.com/Keats/jsonwebtoken/pull/202 After that figure out a way forward for https://github.com/Keats/jsonwebtoken/issues/190 and it should be the definite v8 release
I had the same issue, works perfectly in
8.0.0-beta.2
. Thanks !
Me too, thanks!
This constructs a DecodingKey that owns all of its state, but the signature suggests that it borrows the input key. As a result, the only way to construct a DecodingKey<'static> for lazy_static or once_cell is to leak the input key (or unsafely transmute the lifetime of the input key and hope that DecodingKey never changes its implementation to actually.borrow the input).