identity_jose/jwt/
header.rs1use identity_core::common::Url;
5
6use crate::jose::JoseHeader;
7use crate::jwk::Jwk;
8
9#[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
14pub struct JwtHeader {
15 #[serde(skip_serializing_if = "Option::is_none")]
22 jku: Option<Url>,
23 #[serde(skip_serializing_if = "Option::is_none")]
29 jwk: Option<Jwk>,
30 #[serde(skip_serializing_if = "Option::is_none")]
36 kid: Option<String>,
37 #[serde(skip_serializing_if = "Option::is_none")]
44 x5u: Option<Url>,
45 #[serde(skip_serializing_if = "Option::is_none")]
52 x5c: Option<Vec<String>>,
53 #[serde(skip_serializing_if = "Option::is_none")]
60 x5t: Option<String>,
61 #[serde(rename = "x5t#S256", skip_serializing_if = "Option::is_none")]
68 x5t_s256: Option<String>,
69 #[serde(skip_serializing_if = "Option::is_none")]
75 typ: Option<String>,
76 #[serde(skip_serializing_if = "Option::is_none")]
82 cty: Option<String>,
83 #[serde(skip_serializing_if = "Option::is_none")]
90 crit: Option<Vec<String>>,
91 #[serde(skip_serializing_if = "Option::is_none")]
97 url: Option<Url>,
98 #[serde(skip_serializing_if = "Option::is_none")]
105 nonce: Option<String>,
106}
107
108impl Default for JwtHeader {
109 fn default() -> Self {
110 Self::new()
111 }
112}
113
114impl JwtHeader {
115 pub const fn new() -> Self {
117 Self {
118 jku: None,
119 jwk: None,
120 kid: None,
121 x5u: None,
122 x5c: None,
123 x5t: None,
124 x5t_s256: None,
125 typ: None,
126 cty: None,
127 crit: None,
128 url: None,
129 nonce: None,
130 }
131 }
132
133 pub fn jku(&self) -> Option<&Url> {
135 self.jku.as_ref()
136 }
137
138 pub fn set_jku(&mut self, value: impl Into<Url>) {
140 self.jku = Some(value.into());
141 }
142
143 pub fn jwk(&self) -> Option<&Jwk> {
145 self.jwk.as_ref()
146 }
147
148 pub fn set_jwk(&mut self, value: impl Into<Jwk>) {
150 self.jwk = Some(value.into());
151 }
152
153 pub fn kid(&self) -> Option<&str> {
155 self.kid.as_deref()
156 }
157
158 pub fn set_kid(&mut self, value: impl Into<String>) {
160 self.kid = Some(value.into());
161 }
162
163 pub fn x5u(&self) -> Option<&Url> {
165 self.x5u.as_ref()
166 }
167
168 pub fn set_x5u(&mut self, value: impl Into<Url>) {
170 self.x5u = Some(value.into());
171 }
172
173 pub fn x5c(&self) -> Option<&[String]> {
175 self.x5c.as_deref()
176 }
177
178 pub fn set_x5c(&mut self, value: impl IntoIterator<Item = impl Into<String>>) {
180 self.x5c = Some(value.into_iter().map(Into::into).collect());
181 }
182
183 pub fn x5t(&self) -> Option<&str> {
185 self.x5t.as_deref()
186 }
187
188 pub fn set_x5t(&mut self, value: impl Into<String>) {
190 self.x5t = Some(value.into());
191 }
192
193 pub fn x5t_s256(&self) -> Option<&str> {
196 self.x5t_s256.as_deref()
197 }
198
199 pub fn set_x5t_s256(&mut self, value: impl Into<String>) {
202 self.x5t_s256 = Some(value.into());
203 }
204
205 pub fn typ(&self) -> Option<&str> {
207 self.typ.as_deref()
208 }
209
210 pub fn set_typ(&mut self, value: impl Into<String>) {
212 self.typ = Some(value.into());
213 }
214
215 pub fn cty(&self) -> Option<&str> {
217 self.cty.as_deref()
218 }
219
220 pub fn set_cty(&mut self, value: impl Into<String>) {
222 self.cty = Some(value.into());
223 }
224
225 pub fn crit(&self) -> Option<&[String]> {
227 self.crit.as_deref()
228 }
229
230 pub fn set_crit(&mut self, value: impl IntoIterator<Item = impl Into<String>>) {
232 self.crit = Some(value.into_iter().map(Into::into).collect());
233 }
234
235 pub fn url(&self) -> Option<&Url> {
237 self.url.as_ref()
238 }
239
240 pub fn set_url(&mut self, value: impl Into<Url>) {
242 self.url = Some(value.into());
243 }
244
245 pub fn nonce(&self) -> Option<&str> {
247 self.nonce.as_deref()
248 }
249
250 pub fn set_nonce(&mut self, value: impl Into<String>) {
252 self.nonce = Some(value.into());
253 }
254
255 pub fn has(&self, claim: &str) -> bool {
257 match claim {
258 "jku" => self.jku().is_some(),
259 "jwk" => self.jwk().is_some(),
260 "kid" => self.kid().is_some(),
261 "x5u" => self.x5u().is_some(),
262 "x5c" => self.x5c().is_some(),
263 "x5t" => self.x5t().is_some(),
264 "x5t#S256" => self.x5t_s256().is_some(),
265 "typ" => self.typ().is_some(),
266 "cty" => self.cty().is_some(),
267 "crit" => self.crit().is_some(),
268 "url" => self.url().is_some(),
269 "nonce" => self.nonce().is_some(),
270 _ => false,
271 }
272 }
273
274 pub fn is_disjoint(&self, other: &JwtHeader) -> bool {
276 let has_duplicate: bool = self.jku.is_some() && other.jku.is_some()
277 || self.jwk.is_some() && other.jwk.is_some()
278 || self.kid.is_some() && other.kid.is_some()
279 || self.x5u.is_some() && other.x5u.is_some()
280 || self.x5c.is_some() && other.x5c.is_some()
281 || self.x5t.is_some() && other.x5t.is_some()
282 || self.x5t_s256.is_some() && other.x5t_s256.is_some()
283 || self.typ.is_some() && other.typ.is_some()
284 || self.cty.is_some() && other.cty.is_some()
285 || self.crit.is_some() && other.crit.is_some()
286 || self.url.is_some() && other.url.is_some()
287 || self.nonce.is_some() && other.nonce.is_some();
288
289 !has_duplicate
290 }
291}
292
293impl JoseHeader for JwtHeader {
294 fn common(&self) -> &JwtHeader {
295 self
296 }
297
298 fn has_claim(&self, claim: &str) -> bool {
299 self.has(claim)
300 }
301}