instapi360_cloud/
signing.rs1use hmac::{Hmac, Mac};
20use sha2::Sha256;
21
22use crate::config::ClientConfig;
23use crate::error::{Error, Result};
24
25type HmacSha256 = Hmac<Sha256>;
26
27#[derive(Debug, Clone)]
29pub struct SignParams {
30 pub client_key: String,
31 pub nonce: String,
32 pub timestamp: String,
33}
34
35#[derive(Debug, Clone)]
38pub struct Signature {
39 pub signature: String,
40 pub nonce: String,
41 pub timestamp: String,
42}
43
44#[derive(Debug, Clone)]
46pub struct Signer {
47 app_key: String,
48 sign_key: String,
49 can_sign: bool,
50}
51
52impl Signer {
53 pub fn new(cfg: &ClientConfig) -> Self {
54 Signer {
55 app_key: cfg.app_key.clone(),
56 sign_key: cfg.sign_key.clone(),
57 can_sign: cfg.can_sign(),
58 }
59 }
60
61 fn canonical(&self, p: &SignParams) -> String {
66 format!(
68 "share_type=pc&client_key={}&nonce_str={}×tamp={}",
69 p.client_key, p.nonce, p.timestamp
70 )
71 }
72
73 pub fn sign(&self, nonce: &str, timestamp: &str) -> Result<Signature> {
78 if !self.can_sign {
79 return Err(Error::Signing(
80 "app_key/sign_key not configured — recover them via capture (plan Steps 2–3)"
81 .into(),
82 ));
83 }
84 let params = SignParams {
85 client_key: self.app_key.clone(),
86 nonce: nonce.to_string(),
87 timestamp: timestamp.to_string(),
88 };
89 let canonical = self.canonical(¶ms);
90 let mut mac = HmacSha256::new_from_slice(self.sign_key.as_bytes())
91 .map_err(|e| Error::Signing(format!("bad hmac key: {e}")))?;
92 mac.update(canonical.as_bytes());
93 let sig = hex::encode(mac.finalize().into_bytes());
94 Ok(Signature {
95 signature: sig,
96 nonce: nonce.to_string(),
97 timestamp: timestamp.to_string(),
98 })
99 }
100
101 pub fn is_ready(&self) -> bool {
103 self.can_sign
104 }
105}
106
107#[allow(dead_code)]
111pub fn make_nonce(seed: u64) -> String {
112 let mut x = seed.wrapping_add(0x9E37_79B9_7F4A_7C15);
114 x = (x ^ (x >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
115 x = (x ^ (x >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
116 x ^= x >> 31;
117 format!("{x:016x}")
118}
119
120#[cfg(test)]
121mod tests {
122 use super::*;
123
124 fn cfg_with(app_key: &str, sign_key: &str) -> ClientConfig {
125 let mut c = ClientConfig::windows(crate::config::Region::Global);
126 c.app_key = app_key.to_string();
127 c.sign_key = sign_key.to_string();
128 c
129 }
130
131 #[test]
132 fn unconfigured_signer_refuses() {
133 let signer = Signer::new(&ClientConfig::windows(crate::config::Region::Global));
134 assert!(!signer.is_ready());
135 assert!(signer.sign("abc", "123").is_err());
136 }
137
138 #[test]
139 fn hmac_is_deterministic() {
140 let signer = Signer::new(&cfg_with("APPKEY", "secret"));
141 let a = signer.sign("nonce1", "1700000000").unwrap();
142 let b = signer.sign("nonce1", "1700000000").unwrap();
143 assert_eq!(a.signature, b.signature);
144 let c = signer.sign("nonce2", "1700000000").unwrap();
146 assert_ne!(a.signature, c.signature);
147 }
148
149 #[test]
153 #[ignore = "needs captured request fixture (plan Step 2)"]
154 fn reproduces_captured_signature() {
155 }
159}