identity_jose/jwt/
claims.rs

1// Copyright 2020-2023 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use identity_core::common::OneOrMany;
5
6/// JSON Web Token Claims
7///
8/// [More Info](https://tools.ietf.org/html/rfc7519#section-4)
9#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
10pub struct JwtClaims<T = ()> {
11  /// Identifies the principal that issued the JWT
12  ///
13  /// [More Info](https://tools.ietf.org/html/rfc7519#section-4.1.1)
14  #[serde(skip_serializing_if = "Option::is_none")]
15  iss: Option<String>, // Issuer
16  /// Identifies the principal that is the subject of the JWT.
17  ///
18  /// [More Info](https://tools.ietf.org/html/rfc7519#section-4.1.2)
19  #[serde(skip_serializing_if = "Option::is_none")]
20  sub: Option<String>, // Subject
21  /// Identifies the recipients that the JWT is intended for.
22  ///
23  /// [More Info](https://tools.ietf.org/html/rfc7519#section-4.1.3)
24  #[serde(skip_serializing_if = "Option::is_none")]
25  aud: Option<OneOrMany<String>>, // Audience
26  /// Identifies the expiration time on or after which the JWT MUST NOT be
27  /// accepted for processing.
28  ///
29  /// [More Info](https://tools.ietf.org/html/rfc7519#section-4.1.4)
30  #[serde(skip_serializing_if = "Option::is_none")]
31  exp: Option<i64>, // Expiration Time
32  /// Identifies the time before which the JWT MUST NOT be accepted for
33  /// processing.
34  ///
35  /// [More Info](https://tools.ietf.org/html/rfc7519#section-4.1.5)
36  #[serde(skip_serializing_if = "Option::is_none")]
37  nbf: Option<i64>, // Not Before
38  /// Identifies the time at which the JWT was issued.
39  ///
40  /// [More Info](https://tools.ietf.org/html/rfc7519#section-4.1.6)
41  #[serde(skip_serializing_if = "Option::is_none")]
42  iat: Option<i64>, // Issued At
43  /// Provides a unique identifier for the JWT.
44  ///
45  /// [More Info](https://tools.ietf.org/html/rfc7519#section-4.1.7)
46  #[serde(skip_serializing_if = "Option::is_none")]
47  jti: Option<String>, // JWT ID
48  /// Public/Private Claim Names
49  ///
50  /// [More Info](https://tools.ietf.org/html/rfc7519#section-4.2)
51  #[serde(flatten, skip_serializing_if = "Option::is_none")]
52  custom: Option<T>,
53}
54
55impl<T> JwtClaims<T> {
56  /// Create a new `JwtClaims` set.
57  pub const fn new() -> Self {
58    Self {
59      iss: None,
60      sub: None,
61      aud: None,
62      exp: None,
63      nbf: None,
64      iat: None,
65      jti: None,
66      custom: None,
67    }
68  }
69
70  /// Returns the value for the issuer claim (iss).
71  pub fn iss(&self) -> Option<&str> {
72    self.iss.as_deref()
73  }
74
75  /// Sets a value for the issuer claim (iss).
76  pub fn set_iss(&mut self, value: impl Into<String>) {
77    self.iss = Some(value.into());
78  }
79
80  /// Returns the value for the subject claim (sub).
81  pub fn sub(&self) -> Option<&str> {
82    self.sub.as_deref()
83  }
84
85  /// Sets a value for the subject claim (sub).
86  pub fn set_sub(&mut self, value: impl Into<String>) {
87    self.sub = Some(value.into());
88  }
89
90  /// Returns the values for the audience claim (aud).
91  pub fn aud(&self) -> Option<&[String]> {
92    self.aud.as_deref()
93  }
94
95  /// Sets values for the audience claim (aud).
96  pub fn set_aud(&mut self, value: impl IntoIterator<Item = impl Into<String>>) {
97    self.aud = Some(value.into_iter().map(Into::into).collect());
98  }
99
100  /// Returns the time for the expires at claim (exp).
101  pub fn exp(&self) -> Option<i64> {
102    self.exp
103  }
104
105  /// Sets a time for the expires at claim (exp).
106  pub fn set_exp(&mut self, value: impl Into<i64>) {
107    self.exp = Some(value.into());
108  }
109
110  /// Returns the time for the not before claim (nbf).
111  pub fn nbf(&self) -> Option<i64> {
112    self.nbf
113  }
114
115  /// Sets a time for the not before claim (nbf).
116  pub fn set_nbf(&mut self, value: impl Into<i64>) {
117    self.nbf = Some(value.into());
118  }
119
120  /// Returns the time for the issued at claim (iat).
121  pub fn iat(&self) -> Option<i64> {
122    self.iat
123  }
124
125  /// Sets a time for the issued at claim (iat).
126  pub fn set_iat(&mut self, value: impl Into<i64>) {
127    self.iat = Some(value.into());
128  }
129
130  /// Returns the value for the JWT ID claim (jti).
131  pub fn jti(&self) -> Option<&str> {
132    self.jti.as_deref()
133  }
134
135  /// Sets a value for the JWT ID claim (jti).
136  pub fn set_jti(&mut self, value: impl Into<String>) {
137    self.jti = Some(value.into());
138  }
139
140  /// Returns a reference to the custom JWT claims.
141  pub fn custom(&self) -> Option<&T> {
142    self.custom.as_ref()
143  }
144
145  /// Returns a mutable reference to the custom JWT claims.
146  pub fn custom_mut(&mut self) -> Option<&mut T> {
147    self.custom.as_mut()
148  }
149
150  /// Sets the value of the custom JWT claims.
151  pub fn set_custom(&mut self, value: impl Into<T>) {
152    self.custom = Some(value.into());
153  }
154}