2019-09-11 05:17:30 +00:00
|
|
|
use chrono::{DateTime, Utc};
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
ALGORITHM_FIELD, ALGORITHM_VALUE, CREATED_FIELD, EXPIRES_FIELD, HEADERS_FIELD, KEY_ID_FIELD,
|
|
|
|
SIGNATURE_FIELD,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub struct Signed {
|
|
|
|
signature: String,
|
|
|
|
sig_headers: Vec<String>,
|
|
|
|
created: DateTime<Utc>,
|
|
|
|
expires: DateTime<Utc>,
|
|
|
|
key_id: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Unsigned {
|
|
|
|
pub(crate) signing_string: String,
|
|
|
|
pub(crate) sig_headers: Vec<String>,
|
|
|
|
pub(crate) created: DateTime<Utc>,
|
|
|
|
pub(crate) expires: DateTime<Utc>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Signed {
|
2019-09-11 05:24:04 +00:00
|
|
|
pub fn signature_header(self) -> String {
|
|
|
|
format!("Signature {}", self.into_header())
|
2019-09-11 05:17:30 +00:00
|
|
|
}
|
|
|
|
|
2019-09-11 05:24:04 +00:00
|
|
|
pub fn authorization_header(self) -> String {
|
|
|
|
self.into_header()
|
2019-09-11 05:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn into_header(self) -> String {
|
|
|
|
let header_parts = [
|
|
|
|
(KEY_ID_FIELD, self.key_id),
|
|
|
|
(ALGORITHM_FIELD, ALGORITHM_VALUE.to_owned()),
|
|
|
|
(CREATED_FIELD, self.created.timestamp().to_string()),
|
|
|
|
(EXPIRES_FIELD, self.expires.timestamp().to_string()),
|
|
|
|
(HEADERS_FIELD, self.sig_headers.join(" ")),
|
|
|
|
(SIGNATURE_FIELD, self.signature),
|
|
|
|
];
|
|
|
|
|
|
|
|
header_parts
|
|
|
|
.iter()
|
|
|
|
.map(|(k, v)| format!("{}=\"{}\"", k, v))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(",")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Unsigned {
|
|
|
|
pub fn sign<F, E>(self, key_id: String, f: F) -> Result<Signed, E>
|
|
|
|
where
|
|
|
|
F: FnOnce(&str) -> Result<Vec<u8>, E>,
|
|
|
|
{
|
|
|
|
(f)(&self.signing_string).map(|v| Signed {
|
|
|
|
signature: base64::encode(&v),
|
|
|
|
sig_headers: self.sig_headers,
|
|
|
|
created: self.created,
|
|
|
|
expires: self.expires,
|
|
|
|
key_id,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|