mas_jose/jwt/
header.rs

1// Copyright 2024 New Vector Ltd.
2// Copyright 2022-2024 The Matrix.org Foundation C.I.C.
3//
4// SPDX-License-Identifier: AGPL-3.0-only
5// Please see LICENSE in the repository root for full details.
6
7use mas_iana::jose::JsonWebSignatureAlg;
8use serde::{Deserialize, Serialize};
9use serde_with::skip_serializing_none;
10use url::Url;
11
12use crate::{Base64, base64::Base64UrlNoPad, jwk::PublicJsonWebKey};
13
14#[skip_serializing_none]
15#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
16pub struct JsonWebSignatureHeader {
17    alg: JsonWebSignatureAlg,
18
19    #[serde(default)]
20    jku: Option<Url>,
21
22    #[serde(default)]
23    jwk: Option<Box<PublicJsonWebKey>>,
24
25    #[serde(default)]
26    kid: Option<String>,
27
28    #[serde(default)]
29    x5u: Option<Url>,
30
31    #[serde(default)]
32    x5c: Option<Vec<Base64>>,
33
34    #[serde(default)]
35    x5t: Option<Base64UrlNoPad>,
36
37    #[serde(default, rename = "x5t#S256")]
38    x5t_s256: Option<Base64UrlNoPad>,
39
40    #[serde(default)]
41    typ: Option<String>,
42
43    #[serde(default)]
44    cty: Option<String>,
45
46    #[serde(default)]
47    crit: Option<Vec<String>>,
48}
49
50impl JsonWebSignatureHeader {
51    #[must_use]
52    pub fn new(alg: JsonWebSignatureAlg) -> Self {
53        Self {
54            alg,
55            jku: None,
56            jwk: None,
57            kid: None,
58            x5u: None,
59            x5c: None,
60            x5t: None,
61            x5t_s256: None,
62            typ: None,
63            cty: None,
64            crit: None,
65        }
66    }
67
68    #[must_use]
69    pub const fn alg(&self) -> &JsonWebSignatureAlg {
70        &self.alg
71    }
72
73    #[must_use]
74    pub const fn jku(&self) -> Option<&Url> {
75        self.jku.as_ref()
76    }
77
78    #[must_use]
79    pub fn with_jku(mut self, jku: Url) -> Self {
80        self.jku = Some(jku);
81        self
82    }
83
84    #[must_use]
85    pub const fn jwk(&self) -> Option<&PublicJsonWebKey> {
86        // Can't use as_deref because it's not a const fn
87        match &self.jwk {
88            Some(jwk) => Some(jwk),
89            None => None,
90        }
91    }
92
93    #[must_use]
94    pub fn with_jwk(mut self, jwk: PublicJsonWebKey) -> Self {
95        self.jwk = Some(Box::new(jwk));
96        self
97    }
98
99    #[must_use]
100    pub fn kid(&self) -> Option<&str> {
101        self.kid.as_deref()
102    }
103
104    #[must_use]
105    pub fn with_kid(mut self, kid: impl Into<String>) -> Self {
106        self.kid = Some(kid.into());
107        self
108    }
109
110    #[must_use]
111    pub fn typ(&self) -> Option<&str> {
112        self.typ.as_deref()
113    }
114
115    #[must_use]
116    pub fn with_typ(mut self, typ: String) -> Self {
117        self.typ = Some(typ);
118        self
119    }
120
121    #[must_use]
122    pub fn crit(&self) -> Option<&[String]> {
123        self.crit.as_deref()
124    }
125
126    #[must_use]
127    pub fn with_crit(mut self, crit: Vec<String>) -> Self {
128        self.crit = Some(crit);
129        self
130    }
131}