#[non_exhaustive]pub struct JwtClaims {
pub iss: Option<String>,
pub sub: Option<String>,
pub aud: Option<OneOrMany<String>>,
pub exp: Option<i64>,
pub nbf: Option<i64>,
pub iat: Option<i64>,
pub jti: Option<String>,
pub additional: IndexMap<String, Value>,
}Expand description
A JWT claims set: the registered claims of RFC 7519 §4.1 as fields, every other claim
(vc, vp, nonce, cnf, …) in additional.
Times are NumericDate values (RFC 7519 §2), whole seconds since the Unix epoch; a fraction
is dropped on deserialization. With the vc feature, JwtClaims::numeric_date,
JwtClaims::timestamp and the with_exp, with_nbf and with_iat setters convert from
and to chrono times.
Decoding a token’s payload with CompactJws::claims checks
nothing; validating the registered claims is the validators’ job.
§Examples
use identity_kit::jose::JwtClaims;
let claims: JwtClaims = serde_json::from_str(
r#"{"iss":"did:example:123","aud":["a","b"],"exp":1573029723,"nonce":"660!6345FSer"}"#,
)?;
assert_eq!(claims.iss.as_deref(), Some("did:example:123"));
assert_eq!(claims.audiences().collect::<Vec<_>>(), ["a", "b"]);
assert_eq!(claims.exp, Some(1573029723));
assert_eq!(claims.additional["nonce"], "660!6345FSer");
let built = JwtClaims::new()
.with_iss("did:example:123")
.with_aud("did:example:verifier")
.with_claim("nonce", "n-0S6_WzA2Mj");
assert_eq!(
serde_json::to_string(&built)?,
r#"{"iss":"did:example:123","aud":"did:example:verifier","nonce":"n-0S6_WzA2Mj"}"#
);Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.iss: Option<String>iss: the issuer of the token.
sub: Option<String>sub: the subject of the token.
aud: Option<OneOrMany<String>>aud: the intended recipients.
exp: Option<i64>exp: when the token expires.
nbf: Option<i64>nbf: when the token becomes valid.
iat: Option<i64>iat: when the token was issued.
jti: Option<String>jti: the identifier of the token.
additional: IndexMap<String, Value>Every other claim, in insertion order.
Implementations§
Source§impl JwtClaims
impl JwtClaims
Sourcepub const REGISTERED: [&'static str; 7]
pub const REGISTERED: [&'static str; 7]
The registered claim names of RFC 7519 §4.1, which have fields of their own.
Sourcepub fn with_iss(self, issuer: impl Into<String>) -> Self
pub fn with_iss(self, issuer: impl Into<String>) -> Self
This claims set with iss set to issuer.
Sourcepub fn with_sub(self, subject: impl Into<String>) -> Self
pub fn with_sub(self, subject: impl Into<String>) -> Self
This claims set with sub set to subject.
Sourcepub fn audiences(&self) -> impl Iterator<Item = &str>
pub fn audiences(&self) -> impl Iterator<Item = &str>
The audiences, in order; empty when aud is absent.
Sourcepub fn with_aud(self, audience: impl Into<String>) -> Self
pub fn with_aud(self, audience: impl Into<String>) -> Self
This claims set with aud set to the one audience audience.
Sourcepub fn with_claim(
self,
name: impl Into<String>,
value: impl Into<Value>,
) -> Self
pub fn with_claim( self, name: impl Into<String>, value: impl Into<Value>, ) -> Self
This claims set with the claim name set to value.
name should not be one of REGISTERED, which have fields of their
own: the member would be serialized twice, and vc::Jwt::sign refuses such a claims set.
Source§impl JwtClaims
The conversions between NumericDate values and chrono times, which come with the vc
feature.
impl JwtClaims
The conversions between NumericDate values and chrono times, which come with the vc
feature.
Sourcepub fn numeric_date(time: DateTime<Utc>) -> i64
Available on crate feature vc only.
pub fn numeric_date(time: DateTime<Utc>) -> i64
vc only.time as a NumericDate: whole seconds since the Unix epoch, fractions dropped.
Sourcepub fn timestamp(numeric_date: i64) -> Option<DateTime<Utc>>
Available on crate feature vc only.
pub fn timestamp(numeric_date: i64) -> Option<DateTime<Utc>>
vc only.The time a NumericDate names, if it is in the range chrono represents.
Sourcepub fn with_exp(self, time: DateTime<Utc>) -> Self
Available on crate feature vc only.
pub fn with_exp(self, time: DateTime<Utc>) -> Self
vc only.This claims set with exp set to time.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for JwtClaims
impl<'de> Deserialize<'de> for JwtClaims
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Eq for JwtClaims
impl StructuralPartialEq for JwtClaims
Auto Trait Implementations§
impl Freeze for JwtClaims
impl RefUnwindSafe for JwtClaims
impl Send for JwtClaims
impl Sync for JwtClaims
impl Unpin for JwtClaims
impl UnsafeUnpin for JwtClaims
impl UnwindSafe for JwtClaims
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.