pub struct JwtKeys { /* private fields */ }Expand description
The verification keys a server holds, shared and replaceable.
Cloning shares the set. JwtKeys::replace swaps the whole set at once
for every holder, which is what a JWKS refresher needs: a request takes
JwtKeys::current once and verifies against that snapshot, so a swap
mid-request changes nothing about the request already running.
A plain RwLock around an Arc: the lock is held only to clone the Arc
out, never across a verification, so readers do not wait on each other in
any way that matters and a writer waits for nothing but those clones.
Each set, the first and every replacement, also gets a generation, under
the same lock as the set itself so the two can never disagree. A
crate::JwtCache remembers the generation its entries were verified
under and forgets them all when a later one appears; see
JwtKeys::generation.
Implementations§
Source§impl JwtKeys
impl JwtKeys
Sourcepub fn from_config(
secret: Option<&str>,
secret_is_base64: bool,
) -> Result<Option<Self>, JwtKeyError>
pub fn from_config( secret: Option<&str>, secret_is_base64: bool, ) -> Result<Option<Self>, JwtKeyError>
The keys for PGRST_JWT_SECRET, or None when no secret is set.
Call it where the configuration is read, and treat an error as a reason not to start.
Sourcepub fn generation(&self) -> u64
pub fn generation(&self) -> u64
Which set this is: a number no earlier set, of this handle or any other in the process, was given, and greater than all of theirs.
Read before a token is verified and compared after, it says whether
the set the token was verified against is still the one in force.
Increasing rather than merely different, so that a request which read
it before a replacement is recognisably behind one that read it after,
and cannot put a cache back on the old set. Process-wide rather than
per handle, so that a cache that outlives one JwtKeys – a Worker
isolate’s, when its configuration changes – sees the next one’s sets
as later too.
Sourcepub fn replace(&self, set: KeySet)
pub fn replace(&self, set: KeySet)
Replace the set, for every holder of this handle.
A crate::JwtCache that sees the change forgets every token it
holds, so a token verified by a key that has since been withdrawn is
refused from the next request on – not answered from the cache until
its entry expires. Replacing a set with an identical one forgets them
too, which costs one verification per token; a JWKS refresher that
fetched the same set again does not call this.