Skip to main content

bios_basic/process/
ci_processor.rs

1//! CI (Interface Console) Processor
2//!
3//! CI (接口控制台) 处理器
4//!
5//! The CI type interface is mostly used for calls between systems, and the interface is authenticated by Ak/Sk to ensure the security of the interface.
6//!
7//! CI类型的接口多用于系统之间的调用,通过Ak/Sk进行签名认证,保证接口的安全性。
8use serde::{Deserialize, Serialize};
9use tardis::{basic::result::TardisResult, chrono::Utc, TardisFuns};
10
11use crate::helper::request_helper::sort_query;
12
13/// Application key configuration
14#[derive(Debug, Serialize, Deserialize, Clone)]
15#[serde(default)]
16pub struct AppKeyConfig {
17    pub head_key_date_flag: String,
18    pub ak: String,
19    pub sk: String,
20}
21
22impl Default for AppKeyConfig {
23    fn default() -> Self {
24        AppKeyConfig {
25            head_key_date_flag: "Bios-Date".to_string(),
26            ak: "".to_string(),
27            sk: "".to_string(),
28        }
29    }
30}
31
32/// Generate signature
33///
34/// Generate a signature for the request and return the request header with the signature
35pub fn signature(app_key_config: &AppKeyConfig, method: &str, path: &str, query: &str, mut header: Vec<(String, String)>) -> TardisResult<Vec<(String, String)>> {
36    let sorted_req_query = sort_query(query);
37    let date = Utc::now().format("%a, %d %b %Y %T GMT").to_string();
38    let signature =
39        TardisFuns::crypto.base64.encode(TardisFuns::crypto.digest.hmac_sha256(format!("{method}\n{date}\n{path}\n{sorted_req_query}").to_lowercase(), &app_key_config.sk)?);
40    header.push(("Authorization".to_string(), format!("{}:{signature}", app_key_config.ak)));
41    header.push((app_key_config.head_key_date_flag.to_string(), date));
42    Ok(header)
43}