rustglm 1.0.0

Production-oriented async Rust SDK for Zhipu GLM, Realtime, Batch, RAG, and MCP
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use base64::Engine;
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use hmac::{Hmac, KeyInit, Mac};
use reqwest::header::HeaderValue;
use serde::Serialize;
use sha2::Sha256;

use crate::{Result, SdkError};

pub const DEFAULT_JWT_TTL: Duration = Duration::from_secs(180);
pub const DEFAULT_JWT_REFRESH_BEFORE: Duration = Duration::from_secs(30);

#[derive(Clone)]
pub enum ZhipuAuthentication {
    Auto(String),
    Bearer(String),
    Jwt(JwtAuthentication),
}

impl ZhipuAuthentication {
    pub fn auto(value: impl Into<String>) -> Self {
        Self::Auto(value.into())
    }

    pub fn bearer(value: impl Into<String>) -> Self {
        Self::Bearer(value.into())
    }

    pub fn jwt(value: JwtAuthentication) -> Self {
        Self::Jwt(value)
    }
}

#[derive(Clone)]
pub struct JwtAuthentication {
    api_key: String,
    api_secret: String,
    token_ttl: Duration,
    refresh_before: Duration,
    cache_enabled: bool,
}

impl JwtAuthentication {
    pub fn new(api_key: impl Into<String>, api_secret: impl Into<String>) -> Self {
        Self {
            api_key: api_key.into(),
            api_secret: api_secret.into(),
            token_ttl: DEFAULT_JWT_TTL,
            refresh_before: DEFAULT_JWT_REFRESH_BEFORE,
            cache_enabled: true,
        }
    }

    pub fn from_api_key(value: impl AsRef<str>) -> Result<Self> {
        let value = value.as_ref().trim();
        let mut parts = value.split('.');
        let api_key = parts.next().unwrap_or_default();
        let api_secret = parts.next().unwrap_or_default();
        if api_key.is_empty() || api_secret.is_empty() || parts.next().is_some() {
            return Err(SdkError::Configuration(
                "JWT API key must contain exactly one dot separating key id and secret".into(),
            ));
        }
        Ok(Self::new(api_key, api_secret))
    }

    pub fn token_ttl(mut self, value: Duration) -> Self {
        self.token_ttl = value;
        self
    }

    pub fn refresh_before(mut self, value: Duration) -> Self {
        self.refresh_before = value;
        self
    }

    pub fn cache_enabled(mut self, value: bool) -> Self {
        self.cache_enabled = value;
        self
    }

    pub fn api_key(&self) -> &str {
        &self.api_key
    }

    pub fn token_ttl_value(&self) -> Duration {
        self.token_ttl
    }

    pub fn refresh_before_value(&self) -> Duration {
        self.refresh_before
    }

    pub fn is_cache_enabled(&self) -> bool {
        self.cache_enabled
    }

    pub fn generate_token(&self) -> Result<String> {
        self.validate()?;
        generate_token_at(self, unix_time_millis()?)
    }

    fn validate(&self) -> Result<()> {
        if self.api_key.trim().is_empty() || self.api_secret.is_empty() {
            return Err(SdkError::Configuration(
                "JWT key id and secret cannot be empty".into(),
            ));
        }
        if self.token_ttl.is_zero() {
            return Err(SdkError::Configuration(
                "JWT token TTL must be greater than zero".into(),
            ));
        }
        if self.refresh_before >= self.token_ttl {
            return Err(SdkError::Configuration(
                "JWT refresh window must be shorter than token TTL".into(),
            ));
        }
        Ok(())
    }
}

#[derive(Clone)]
pub(crate) struct AuthenticationProvider {
    inner: Arc<AuthenticationState>,
}

enum AuthenticationState {
    Static(String),
    Jwt {
        config: JwtAuthentication,
        cache: Mutex<Option<CachedToken>>,
    },
}

struct CachedToken {
    value: String,
    refresh_at_millis: u64,
}

impl AuthenticationProvider {
    pub(crate) fn zhipu(value: ZhipuAuthentication) -> Result<Self> {
        match value {
            ZhipuAuthentication::Auto(value) => match JwtAuthentication::from_api_key(&value) {
                Ok(config) => Self::jwt(config),
                Err(_) if !value.trim().is_empty() && !value.contains('.') => Self::bearer(value),
                Err(error) => Err(error),
            },
            ZhipuAuthentication::Bearer(value) => Self::bearer(value),
            ZhipuAuthentication::Jwt(config) => Self::jwt(config),
        }
    }

    pub(crate) fn bearer(value: impl Into<String>) -> Result<Self> {
        let value = value.into();
        if value.trim().is_empty() {
            return Err(SdkError::Configuration(
                "bearer token cannot be empty".into(),
            ));
        }
        Ok(Self {
            inner: Arc::new(AuthenticationState::Static(value)),
        })
    }

    pub(crate) fn jwt(config: JwtAuthentication) -> Result<Self> {
        config.validate()?;
        Ok(Self {
            inner: Arc::new(AuthenticationState::Jwt {
                config,
                cache: Mutex::new(None),
            }),
        })
    }

    pub(crate) fn header_value(&self) -> Result<HeaderValue> {
        let token = self.token()?;
        HeaderValue::from_str(&format!("Bearer {token}")).map_err(|_| {
            SdkError::Configuration("authentication value is not a valid header".into())
        })
    }

    fn token(&self) -> Result<String> {
        match self.inner.as_ref() {
            AuthenticationState::Static(value) => Ok(value.trim().to_owned()),
            AuthenticationState::Jwt { config, cache } => {
                let now = unix_time_millis()?;
                if config.cache_enabled {
                    let mut cache = cache.lock().map_err(|_| {
                        SdkError::Configuration("JWT cache lock is poisoned".into())
                    })?;
                    if let Some(token) =
                        cache.as_ref().filter(|token| now < token.refresh_at_millis)
                    {
                        return Ok(token.value.clone());
                    }
                    let value = generate_token_at(config, now)?;
                    let refresh_at_millis = now
                        .checked_add(duration_millis(config.token_ttl)?)
                        .and_then(|value| {
                            value.checked_sub(duration_millis(config.refresh_before).ok()?)
                        })
                        .ok_or_else(|| {
                            SdkError::Configuration("JWT refresh time overflow".into())
                        })?;
                    *cache = Some(CachedToken {
                        value: value.clone(),
                        refresh_at_millis,
                    });
                    Ok(value)
                } else {
                    generate_token_at(config, now)
                }
            }
        }
    }
}

#[derive(Serialize)]
struct JwtHeader<'a> {
    alg: &'a str,
    sign_type: &'a str,
}

#[derive(Serialize)]
struct JwtPayload<'a> {
    api_key: &'a str,
    exp: u64,
    timestamp: u64,
}

fn generate_token_at(config: &JwtAuthentication, now_millis: u64) -> Result<String> {
    config.validate()?;
    let exp = now_millis
        .checked_add(duration_millis(config.token_ttl)?)
        .ok_or_else(|| SdkError::Configuration("JWT expiration time overflow".into()))?;
    let header = encode_json(&JwtHeader {
        alg: "HS256",
        sign_type: "SIGN",
    })?;
    let payload = encode_json(&JwtPayload {
        api_key: &config.api_key,
        exp,
        timestamp: now_millis,
    })?;
    let signing_input = format!("{header}.{payload}");
    let mut mac = Hmac::<Sha256>::new_from_slice(config.api_secret.as_bytes())
        .map_err(|_| SdkError::Configuration("JWT secret is invalid".into()))?;
    mac.update(signing_input.as_bytes());
    let signature = URL_SAFE_NO_PAD.encode(mac.finalize().into_bytes());
    Ok(format!("{signing_input}.{signature}"))
}

fn encode_json<T: Serialize>(value: &T) -> Result<String> {
    let bytes = serde_json::to_vec(value)
        .map_err(|error| SdkError::Configuration(error.to_string().into()))?;
    Ok(URL_SAFE_NO_PAD.encode(bytes))
}

fn unix_time_millis() -> Result<u64> {
    let duration = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_err(|_| SdkError::Configuration("system clock is before Unix epoch".into()))?;
    duration_millis(duration)
}

fn duration_millis(value: Duration) -> Result<u64> {
    u64::try_from(value.as_millis())
        .map_err(|_| SdkError::Configuration("duration exceeds supported range".into()))
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::Value;

    fn decode_segment(value: &str) -> Value {
        let bytes = URL_SAFE_NO_PAD.decode(value).unwrap();
        serde_json::from_slice(&bytes).unwrap()
    }

    #[test]
    fn parses_combined_api_key() {
        let config = JwtAuthentication::from_api_key("key.secret").unwrap();
        assert_eq!(config.api_key(), "key");
        assert_eq!(config.token_ttl_value(), Duration::from_secs(180));
        assert_eq!(config.refresh_before_value(), Duration::from_secs(30));
    }

    #[test]
    fn rejects_invalid_combined_api_keys() {
        for value in ["", "key", ".secret", "key.", "a.b.c"] {
            assert!(JwtAuthentication::from_api_key(value).is_err());
        }
    }

    #[test]
    fn generates_official_jwt_shape() {
        let config = JwtAuthentication::new("key", "secret");
        let token = generate_token_at(&config, 1_700_000_000_000).unwrap();
        let parts: Vec<_> = token.split('.').collect();
        assert_eq!(parts.len(), 3);
        assert_eq!(
            decode_segment(parts[0]),
            serde_json::json!({"alg":"HS256","sign_type":"SIGN"})
        );
        assert_eq!(
            decode_segment(parts[1]),
            serde_json::json!({"api_key":"key","timestamp":1700000000000u64,"exp":1700000180000u64})
        );
        assert!(!parts[2].contains('='));
    }

    #[test]
    fn signature_is_hs256_and_verifiable() {
        let config = JwtAuthentication::new("key", "secret");
        let token = generate_token_at(&config, 42).unwrap();
        let parts: Vec<_> = token.split('.').collect();
        let mut mac = Hmac::<Sha256>::new_from_slice(b"secret").unwrap();
        mac.update(format!("{}.{}", parts[0], parts[1]).as_bytes());
        let signature = URL_SAFE_NO_PAD.decode(parts[2]).unwrap();
        mac.verify_slice(&signature).unwrap();
    }

    #[test]
    fn validates_ttl_and_refresh_window() {
        assert!(
            JwtAuthentication::new("key", "secret")
                .token_ttl(Duration::ZERO)
                .generate_token()
                .is_err()
        );
        assert!(
            JwtAuthentication::new("key", "secret")
                .token_ttl(Duration::from_secs(10))
                .refresh_before(Duration::from_secs(10))
                .generate_token()
                .is_err()
        );
    }

    #[test]
    fn auto_selects_jwt_for_combined_key() {
        let provider =
            AuthenticationProvider::zhipu(ZhipuAuthentication::auto("key.secret")).unwrap();
        let header = provider.header_value().unwrap();
        assert_eq!(header.to_str().unwrap().matches('.').count(), 2);
    }

    #[test]
    fn auto_keeps_non_combined_key_as_bearer() {
        let provider =
            AuthenticationProvider::zhipu(ZhipuAuthentication::auto("opaque-key")).unwrap();
        assert_eq!(provider.header_value().unwrap(), "Bearer opaque-key");
    }

    #[test]
    fn cached_provider_reuses_token() {
        let provider =
            AuthenticationProvider::jwt(JwtAuthentication::new("key", "secret")).unwrap();
        assert_eq!(provider.token().unwrap(), provider.token().unwrap());
    }

    #[test]
    fn disabled_cache_still_generates_valid_token() {
        let provider = AuthenticationProvider::jwt(
            JwtAuthentication::new("key", "secret").cache_enabled(false),
        )
        .unwrap();
        assert_eq!(provider.token().unwrap().split('.').count(), 3);
    }

    #[test]
    fn explicit_authentication_constructors_preserve_modes() {
        assert!(matches!(
            ZhipuAuthentication::auto("value"),
            ZhipuAuthentication::Auto(_)
        ));
        assert!(matches!(
            ZhipuAuthentication::bearer("value"),
            ZhipuAuthentication::Bearer(_)
        ));
        assert!(matches!(
            ZhipuAuthentication::jwt(JwtAuthentication::new("key", "secret")),
            ZhipuAuthentication::Jwt(_)
        ));
    }

    #[test]
    fn explicit_bearer_validates_empty_and_invalid_headers() {
        assert!(AuthenticationProvider::bearer("").is_err());
        let provider = AuthenticationProvider::bearer("invalid\nvalue").unwrap();
        assert!(provider.header_value().is_err());
    }

    #[test]
    fn custom_jwt_settings_are_observable() {
        let config = JwtAuthentication::new("key", "secret")
            .token_ttl(Duration::from_secs(60))
            .refresh_before(Duration::from_secs(5))
            .cache_enabled(false);
        assert_eq!(config.token_ttl_value(), Duration::from_secs(60));
        assert_eq!(config.refresh_before_value(), Duration::from_secs(5));
        assert!(!config.is_cache_enabled());
    }

    #[test]
    fn explicit_jwt_rejects_empty_credentials() {
        assert!(AuthenticationProvider::jwt(JwtAuthentication::new("", "secret")).is_err());
        assert!(AuthenticationProvider::jwt(JwtAuthentication::new("key", "")).is_err());
    }
}