apub_core/
signature.rs

1//! Signature-related types and traits
2
3use std::{rc::Rc, sync::Arc};
4
5/// Describe the operation of signing a string
6///
7/// This is used by downstream crates for creating HTTP Signatures.
8pub trait Sign {
9    /// Errors that signing the request can produce
10    type Error: Send;
11
12    /// Sign the given string, producing a String
13    fn sign(&self, signing_string: &str) -> Result<String, Self::Error>;
14}
15
16/// Describes creating signatures
17pub trait PrivateKey {
18    /// The type that actually signs requests
19    type Signer: Sign + Send + 'static;
20
21    /// Get the KeyId from the factory
22    fn key_id(&self) -> String;
23
24    /// Produce the type that actually signs the request
25    fn signer(&self) -> Self::Signer;
26}
27
28/// Describes building private keys
29pub trait PrivateKeyBuilder: PrivateKey {
30    /// Errors possible when constructing private keys
31    type Error: Send;
32
33    /// Build private key from pem pkcs8
34    fn build(key_id: String, private_key_pem: &str) -> Result<Self, Self::Error>
35    where
36        Self: Sized;
37
38    /// Retrieve the pem pkcs8 encoded private key from this builder
39    fn private_key_pem(&self) -> Result<String, Self::Error>;
40}
41
42/// Describes verifying signatures
43pub trait Verify {
44    /// Errors that verifying a signature can produce
45    type Error: Send;
46
47    /// Verify the signature matches the provided signing string
48    fn verify(&self, signing_string: &str, signature: &str) -> Result<bool, Self::Error>;
49}
50
51/// Describes a marker type that is associated with a specific verifier
52pub trait VerifyFactory {
53    /// The Verify type associated with this factory
54    type Verify: Verify + VerifyBuilder;
55}
56
57/// Describes creating Verifiers
58pub trait VerifyBuilder: Verify {
59    /// Build a verifier from a given public key in pem format
60    fn build(public_key_pem: &str) -> Result<Self, Self::Error>
61    where
62        Self: Sized;
63}
64
65impl<'a, T> Verify for &'a T
66where
67    T: Verify,
68{
69    type Error = T::Error;
70
71    fn verify(&self, signing_string: &str, signature: &str) -> Result<bool, Self::Error> {
72        T::verify(self, signing_string, signature)
73    }
74}
75
76impl<'a, T> Verify for &'a mut T
77where
78    T: Verify,
79{
80    type Error = T::Error;
81
82    fn verify(&self, signing_string: &str, signature: &str) -> Result<bool, Self::Error> {
83        T::verify(self, signing_string, signature)
84    }
85}
86
87impl<T> Verify for Box<T>
88where
89    T: Verify,
90{
91    type Error = T::Error;
92
93    fn verify(&self, signing_string: &str, signature: &str) -> Result<bool, Self::Error> {
94        T::verify(self, signing_string, signature)
95    }
96}
97
98impl<T> Verify for Rc<T>
99where
100    T: Verify,
101{
102    type Error = T::Error;
103
104    fn verify(&self, signing_string: &str, signature: &str) -> Result<bool, Self::Error> {
105        T::verify(self, signing_string, signature)
106    }
107}
108
109impl<T> Verify for Arc<T>
110where
111    T: Verify,
112{
113    type Error = T::Error;
114
115    fn verify(&self, signing_string: &str, signature: &str) -> Result<bool, Self::Error> {
116        T::verify(self, signing_string, signature)
117    }
118}
119
120impl<'a, T> Sign for &'a T
121where
122    T: Sign,
123{
124    type Error = T::Error;
125
126    fn sign(&self, signing_string: &str) -> Result<String, Self::Error> {
127        T::sign(self, signing_string)
128    }
129}
130
131impl<'a, T> Sign for &'a mut T
132where
133    T: Sign,
134{
135    type Error = T::Error;
136
137    fn sign(&self, signing_string: &str) -> Result<String, Self::Error> {
138        T::sign(self, signing_string)
139    }
140}
141
142impl<T> Sign for Box<T>
143where
144    T: Sign,
145{
146    type Error = T::Error;
147
148    fn sign(&self, signing_string: &str) -> Result<String, Self::Error> {
149        T::sign(self, signing_string)
150    }
151}
152
153impl<T> Sign for Rc<T>
154where
155    T: Sign,
156{
157    type Error = T::Error;
158
159    fn sign(&self, signing_string: &str) -> Result<String, Self::Error> {
160        T::sign(self, signing_string)
161    }
162}
163
164impl<T> Sign for Arc<T>
165where
166    T: Sign,
167{
168    type Error = T::Error;
169
170    fn sign(&self, signing_string: &str) -> Result<String, Self::Error> {
171        T::sign(self, signing_string)
172    }
173}
174
175impl<'a, T> PrivateKey for &'a T
176where
177    T: PrivateKey,
178{
179    type Signer = T::Signer;
180
181    fn key_id(&self) -> String {
182        T::key_id(self)
183    }
184
185    fn signer(&self) -> Self::Signer {
186        T::signer(self)
187    }
188}
189
190impl<'a, T> PrivateKey for &'a mut T
191where
192    T: PrivateKey,
193{
194    type Signer = T::Signer;
195
196    fn key_id(&self) -> String {
197        T::key_id(self)
198    }
199
200    fn signer(&self) -> Self::Signer {
201        T::signer(self)
202    }
203}
204
205impl<T> PrivateKey for Box<T>
206where
207    T: PrivateKey,
208{
209    type Signer = T::Signer;
210
211    fn key_id(&self) -> String {
212        T::key_id(self)
213    }
214
215    fn signer(&self) -> Self::Signer {
216        T::signer(self)
217    }
218}
219
220impl<T> PrivateKey for Rc<T>
221where
222    T: PrivateKey,
223{
224    type Signer = T::Signer;
225
226    fn key_id(&self) -> String {
227        T::key_id(self)
228    }
229
230    fn signer(&self) -> Self::Signer {
231        T::signer(self)
232    }
233}
234
235impl<T> PrivateKey for Arc<T>
236where
237    T: PrivateKey,
238{
239    type Signer = T::Signer;
240
241    fn key_id(&self) -> String {
242        T::key_id(self)
243    }
244
245    fn signer(&self) -> Self::Signer {
246        T::signer(self)
247    }
248}