object_storage/oss/util/
authorization.rs

1use super::crypto::auth_sh1;
2use crate::oss::{Auth, Content};
3use anyhow::{anyhow, Result};
4pub async fn create_authorization(
5    auth: &Auth,
6    verb: &str,
7    content: Option<&Content>,
8    datetime: &str,
9    caon_header: Option<&str>,
10    caon_resource: &str,
11) -> Result<String> {
12    let (md5, content_type) = match content {
13        Some(content) => (content.content_md5.as_str(), content.content_type.as_str()),
14        None => ("", ""),
15    };
16    let caon_header = caon_header.unwrap_or("");
17    let data = format!(
18        "{}\n{}\n{}\n{}\n{}{}",
19        verb, md5, content_type, datetime, caon_header, caon_resource
20    );
21    let hash = auth_sh1(auth.accesskeysecret.as_bytes(), data.as_bytes())
22        .await
23        .map_err(|e| anyhow!("Failed to compute auth hash: {}", e))?;
24    Ok(format!("OSS {}:{}", auth.accesskeyid, hash))
25}