identity_storage/storage/
signature_options.rs

1// Copyright 2020-2023 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use identity_core::common::Object;
5use identity_core::common::Url;
6
7/// Options for creating a JSON Web Signature.
8#[non_exhaustive]
9#[derive(Debug, Default, serde::Serialize, serde::Deserialize, Eq, PartialEq, Clone)]
10#[serde(rename_all = "camelCase")]
11#[serde(default)]
12pub struct JwsSignatureOptions {
13  /// Whether to attach the public key in the corresponding method
14  /// to the JWS header.
15  pub attach_jwk: bool,
16
17  /// Whether to Base64url encode the payload or not.
18  ///
19  /// [More Info](https://tools.ietf.org/html/rfc7797#section-3)
20  #[serde(skip_serializing_if = "Option::is_none")]
21  pub b64: Option<bool>,
22
23  /// The Type value to be placed in the protected header.
24  ///
25  /// [More Info](https://tools.ietf.org/html/rfc7515#section-4.1.9)
26  #[serde(skip_serializing_if = "Option::is_none")]
27  pub typ: Option<String>,
28
29  /// Content Type to be placed in the protected header.
30  ///
31  /// [More Info](https://tools.ietf.org/html/rfc7515#section-4.1.10)
32  #[serde(skip_serializing_if = "Option::is_none")]
33  pub cty: Option<String>,
34
35  /// The URL to be placed in the protected header.
36  ///
37  /// [More Info](https://tools.ietf.org/html/rfc8555#section-6.4.1)
38  #[serde(skip_serializing_if = "Option::is_none")]
39  pub url: Option<Url>,
40
41  /// The nonce to be placed in the protected header.
42  ///
43  /// [More Info](https://tools.ietf.org/html/rfc8555#section-6.5.2)
44  #[serde(skip_serializing_if = "Option::is_none")]
45  pub nonce: Option<String>,
46
47  /// The kid to set in the protected header.
48  ///
49  /// If unset, the kid of the JWK with which the JWS is produced is used.
50  ///
51  /// [More Info](https://www.rfc-editor.org/rfc/rfc7515#section-4.1.4)
52  #[serde(skip_serializing_if = "Option::is_none")]
53  pub kid: Option<String>,
54
55  /// Whether the payload should be detached from the JWS.
56  ///
57  /// [More Info](https://www.rfc-editor.org/rfc/rfc7515#appendix-F).
58  pub detached_payload: bool,
59
60  /// Additional header parameters.
61  #[serde(skip_serializing_if = "Option::is_none")]
62  pub custom_header_parameters: Option<Object>,
63}
64
65impl JwsSignatureOptions {
66  /// Creates a new [`JwsSignatureOptions`].
67  pub fn new() -> Self {
68    Self::default()
69  }
70
71  /// Replace the value of the `attach_jwk` field.
72  pub fn attach_jwk_to_header(mut self, value: bool) -> Self {
73    self.attach_jwk = value;
74    self
75  }
76
77  /// Replace the value of the `b64` field.
78  pub fn b64(mut self, value: bool) -> Self {
79    self.b64 = Some(value);
80    self
81  }
82
83  /// Replace the value of the `typ` field.
84  pub fn typ(mut self, value: impl Into<String>) -> Self {
85    self.typ = Some(value.into());
86    self
87  }
88
89  /// Replace the value of the `cty` field.
90  pub fn cty(mut self, value: impl Into<String>) -> Self {
91    self.cty = Some(value.into());
92    self
93  }
94
95  /// Replace the value of the `url` field.
96  pub fn url(mut self, value: Url) -> Self {
97    self.url = Some(value);
98    self
99  }
100
101  /// Replace the value of the `nonce` field.
102  pub fn nonce(mut self, value: impl Into<String>) -> Self {
103    self.nonce = Some(value.into());
104    self
105  }
106
107  /// Replace the value of the `kid` field.
108  pub fn kid(mut self, value: impl Into<String>) -> Self {
109    self.kid = Some(value.into());
110    self
111  }
112
113  /// Replace the value of the `detached_payload` field.
114  pub fn detached_payload(mut self, value: bool) -> Self {
115    self.detached_payload = value;
116    self
117  }
118
119  /// Adds additional header parameters.
120  pub fn custom_header_parameters(mut self, value: Object) -> Self {
121    self.custom_header_parameters = Some(value);
122    self
123  }
124}