Skip to main content

Key

Struct Key 

Source
pub struct Key(/* private fields */);
Expand description

An HS256 signing key, derived from SECRET_KEY_BASE.

Handlers do not build one: encode and decode derive it from the Worker secret. Use it with encode_with and decode_with in tests, tools and scripts.

§Examples

let key = ocre::jwt::Key::from_secret_key_base(&"x".repeat(64));
let token = ocre::jwt::encode_with(&key, &ocre::jwt::Claims::new("42", 60));
assert_eq!(ocre::jwt::decode_with(&key, &token, ocre::now())?.sub, "42");

Implementations§

Source§

impl Key

Source

pub fn from_secret_key_base(secret: &str) -> Self

Derives the key from a SECRET_KEY_BASE value: HMAC-SHA256 of a fixed label.

The label makes the JWT key differ from the session cookie key derived from the same secret. Any length is accepted here; the Worker secret read by encode and decode must be 64 characters or more.

§Examples
use ocre::jwt::{Claims, Key, decode_with, encode_with};

let key = Key::from_secret_key_base(&"x".repeat(64));
let token = encode_with(&key, &Claims::new("42", 60));
assert_eq!(decode_with(&key, &token, ocre::now())?.sub, "42");
// Another secret, another key: the token no longer verifies.
assert!(decode_with(&Key::from_secret_key_base(&"y".repeat(64)), &token, ocre::now()).is_err());

Auto Trait Implementations§

§

impl Freeze for Key

§

impl RefUnwindSafe for Key

§

impl Send for Key

§

impl Sync for Key

§

impl Unpin for Key

§

impl UnsafeUnpin for Key

§

impl UnwindSafe for Key

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V