ascend-tools-core 1.1.0

SDK for the Ascend Instance web API
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
416
417
use base64::Engine;
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use serde_json::Value;
use std::sync::Mutex;
use std::time::{SystemTime, UNIX_EPOCH};
use ureq::Agent;
use zeroize::Zeroizing;

use crate::error::{Error, JsonResultExt, Result, UreqResultExt};

/// Manages authentication for the Ascend API.
///
/// Signs Ed25519 JWTs and exchanges them for instance tokens
/// via the Instance API's /api/v1/auth/token endpoint.
pub struct Auth {
    service_account_id: String,
    key_bytes: Zeroizing<Vec<u8>>,
    instance_api_url: String,
    agent: Agent,
    cloud_api_domain: Mutex<Option<String>>,
    cached_token: Mutex<Option<CachedToken>>,
}

struct CachedToken {
    token: String,
    expires_at: u64,
}

impl Auth {
    /// Create a new Auth instance.
    ///
    /// `key_b64` accepts both URL-safe no-pad (RFC 4648 ยง5) and standard base64 encodings.
    pub fn new(
        service_account_id: String,
        key_b64: &str,
        instance_api_url: String,
        agent: Agent,
    ) -> Result<Self> {
        let key_bytes = Zeroizing::new(
            URL_SAFE_NO_PAD
                .decode(key_b64.trim())
                .or_else(|_| base64::engine::general_purpose::STANDARD.decode(key_b64.trim()))
                .map_err(|_| Error::InvalidServiceAccountKeyEncoding)?,
        );

        if key_bytes.len() != 32 {
            return Err(Error::InvalidServiceAccountKeyLength {
                got: key_bytes.len(),
            });
        }

        Ok(Self {
            service_account_id,
            key_bytes,
            instance_api_url,
            agent,
            cloud_api_domain: Mutex::new(None),
            cached_token: Mutex::new(None),
        })
    }

    /// Returns the service account ID.
    pub fn service_account_id(&self) -> &str {
        &self.service_account_id
    }

    /// Get a valid instance token, refreshing if needed.
    pub fn get_token(&self) -> Result<String> {
        let mut guard = self.cached_token.lock().map_err(|_| Error::MutexPoisoned {
            name: "token cache",
        })?;

        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map_err(|source| Error::SystemClockBeforeUnixEpoch { source })?
            .as_secs();

        // Return cached token if still valid (with 5-minute buffer)
        if let Some(ref cached) = *guard
            && cached.expires_at > now + 300
        {
            return Ok(cached.token.clone());
        }

        // Refresh while holding the lock to prevent thundering herd
        let domain = self.get_cloud_api_domain()?;
        let sa_jwt = self.sign_jwt(now, &domain)?;
        let (instance_token, expires_at) = self.exchange_token(&sa_jwt)?;

        *guard = Some(CachedToken {
            token: instance_token.clone(),
            expires_at,
        });

        Ok(instance_token)
    }

    /// Fetch the Cloud API domain from the Instance API's auth config endpoint.
    /// Cached after the first call.
    fn get_cloud_api_domain(&self) -> Result<String> {
        let mut guard = self
            .cloud_api_domain
            .lock()
            .map_err(|_| Error::MutexPoisoned {
                name: "cloud_api_domain",
            })?;

        if let Some(ref domain) = *guard {
            return Ok(domain.clone());
        }

        let url = format!("{}/api/v1/auth/config", self.instance_api_url);
        let mut resp = self
            .agent
            .get(&url)
            .call()
            .with_request_context(format!("failed to fetch auth config ({url})"))?;

        let status = resp.status().as_u16();
        let body: String = resp
            .body_mut()
            .read_to_string()
            .with_response_read_context("auth config response")?;

        if !(200..300).contains(&status) {
            return Err(Error::ApiError {
                status,
                message: body,
            });
        }

        let json: Value = serde_json::from_str(&body).with_json_parse_context("auth config")?;
        let domain = json
            .get("cloud_api_domain")
            .and_then(|v| v.as_str())
            .ok_or_else(|| Error::MissingField {
                context: "auth config",
                field: "cloud_api_domain",
            })?
            .to_string();

        *guard = Some(domain.clone());
        Ok(domain)
    }

    /// Sign a JWT with the Ed25519 private key.
    fn sign_jwt(&self, now: u64, cloud_api_domain: &str) -> Result<String> {
        let header = jsonwebtoken::Header::new(jsonwebtoken::Algorithm::EdDSA);
        let claims = serde_json::json!({
            "sub": self.service_account_id,
            "aud": format!("https://{cloud_api_domain}/auth/token"),
            "exp": now + 300,
            "iat": now,
            "name": self.service_account_id,
            "service_account": self.service_account_id,
        });
        let der_key = ed25519_seed_to_pkcs8_der(&self.key_bytes)?;
        let key = jsonwebtoken::EncodingKey::from_ed_der(&der_key);
        jsonwebtoken::encode(&header, &claims, &key)
            .map_err(|source| Error::JwtSignFailed { source })
    }

    /// Exchange the service account JWT for an instance token
    /// via the Instance API's /api/v1/auth/token endpoint.
    fn exchange_token(&self, sa_jwt: &str) -> Result<(String, u64)> {
        let url = format!("{}/api/v1/auth/token", self.instance_api_url);
        let mut resp = self
            .agent
            .post(&url)
            .header("Authorization", &format!("Bearer {sa_jwt}"))
            .send_empty()
            .with_request_context(format!("failed to exchange token ({url})"))?;

        let status = resp.status().as_u16();
        let resp_body: String = resp
            .body_mut()
            .read_to_string()
            .with_response_read_context("token exchange response")?;

        if !(200..300).contains(&status) {
            return Err(Error::ApiError {
                status,
                message: resp_body,
            });
        }

        let json: Value =
            serde_json::from_str(&resp_body).with_json_parse_context("token exchange response")?;

        let token = json
            .get("access_token")
            .and_then(|v| v.as_str())
            .ok_or_else(|| Error::MissingField {
                context: "token exchange response",
                field: "access_token",
            })?
            .to_string();

        let expires_at = json
            .get("expiration")
            .and_then(|v| v.as_u64())
            .ok_or_else(|| Error::MissingField {
                context: "token exchange response",
                field: "expiration",
            })?;

        Ok((token, expires_at))
    }
}

impl std::fmt::Debug for Auth {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Auth")
            .field("service_account_id", &self.service_account_id)
            .field("instance_api_url", &self.instance_api_url)
            .field("service_account_key", &"[REDACTED]")
            .finish()
    }
}

/// Wrap a raw 32-byte Ed25519 seed into PKCS#8 v1 DER format (RFC 8410 ยง7).
///
/// We hand-roll the DER rather than using the `pkcs8` crate because:
/// - The encoding is fixed-size (48 bytes) with no conditional branches
/// - Ed25519 PKCS#8 has a single canonical form (no parameters, no public key)
/// - Adding `pkcs8` as a direct dep would require coordinating features (`alloc`)
///   across three separate Cargo.lock files (core, cli, py)
///
/// Structure:
///   SEQUENCE {
///     INTEGER 0                          -- version (v1)
///     SEQUENCE { OID 1.3.101.112 }       -- algorithm (Ed25519)
///     OCTET STRING { OCTET STRING seed } -- privateKey (CurvePrivateKey)
///   }
fn ed25519_seed_to_pkcs8_der(seed: &[u8]) -> Result<Zeroizing<Vec<u8>>> {
    if seed.len() != 32 {
        return Err(Error::InvalidEd25519SeedLength { got: seed.len() });
    }
    let prefix: &[u8] = &[
        0x30, 0x2e, // SEQUENCE (46 bytes total)
        0x02, 0x01, 0x00, // INTEGER 0 (version)
        0x30, 0x05, // SEQUENCE (5 bytes)
        0x06, 0x03, 0x2b, 0x65, 0x70, // OID 1.3.101.112 (Ed25519)
        0x04, 0x22, // OCTET STRING (34 bytes)
        0x04, 0x20, // OCTET STRING (32 bytes) โ€” the seed
    ];
    let mut der = Zeroizing::new(Vec::with_capacity(prefix.len() + 32));
    der.extend_from_slice(prefix);
    der.extend_from_slice(seed);
    Ok(der)
}

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

    fn test_agent() -> Agent {
        crate::new_agent()
    }

    fn test_seed() -> [u8; 32] {
        [42u8; 32]
    }

    fn test_auth(seed: &[u8; 32]) -> Auth {
        let b64 = URL_SAFE_NO_PAD.encode(seed);
        Auth::new(
            "asc-sa-test".into(),
            &b64,
            "https://ascend.io".into(),
            test_agent(),
        )
        .unwrap()
    }

    // -- PKCS#8 DER encoding --

    #[test]
    fn pkcs8_der_output_is_48_bytes() {
        let der = ed25519_seed_to_pkcs8_der(&[0u8; 32]).unwrap();
        assert_eq!(der.len(), 48);
    }

    #[test]
    fn pkcs8_der_has_correct_prefix() {
        let der = ed25519_seed_to_pkcs8_der(&[0u8; 32]).unwrap();
        let expected_prefix: &[u8] = &[
            0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x04, 0x22,
            0x04, 0x20,
        ];
        assert_eq!(&der[..16], expected_prefix);
    }

    #[test]
    fn pkcs8_der_embeds_seed() {
        let seed: Vec<u8> = (0..32).collect();
        let der = ed25519_seed_to_pkcs8_der(&seed).unwrap();
        assert_eq!(&der[16..], &seed[..]);
    }

    #[test]
    fn pkcs8_der_rejects_wrong_length() {
        assert!(ed25519_seed_to_pkcs8_der(&[]).is_err());
        assert!(ed25519_seed_to_pkcs8_der(&[0u8; 16]).is_err());
        assert!(ed25519_seed_to_pkcs8_der(&[0u8; 64]).is_err());
    }

    #[test]
    fn pkcs8_der_roundtrip_with_jsonwebtoken() {
        let der = ed25519_seed_to_pkcs8_der(&test_seed()).unwrap();
        let key = jsonwebtoken::EncodingKey::from_ed_der(&der);
        let header = jsonwebtoken::Header::new(jsonwebtoken::Algorithm::EdDSA);
        let claims = serde_json::json!({"sub": "test"});
        assert!(jsonwebtoken::encode(&header, &claims, &key).is_ok());
    }

    // -- Base64 decoding --

    #[test]
    fn auth_new_accepts_url_safe_base64() {
        let b64 = URL_SAFE_NO_PAD.encode(test_seed());
        let auth = Auth::new("sa".into(), &b64, "https://ascend.io".into(), test_agent());
        assert!(auth.is_ok());
    }

    #[test]
    fn auth_new_accepts_standard_base64() {
        // 0xFF bytes produce +/ in STANDARD but -_ in URL_SAFE
        let b64 = base64::engine::general_purpose::STANDARD.encode([0xFF_u8; 32]);
        let auth = Auth::new("sa".into(), &b64, "https://ascend.io".into(), test_agent());
        assert!(auth.is_ok());
    }

    #[test]
    fn auth_new_rejects_wrong_key_length() {
        let b64_short = URL_SAFE_NO_PAD.encode([0u8; 16]);
        let auth = Auth::new(
            "sa".into(),
            &b64_short,
            "https://ascend.io".into(),
            test_agent(),
        );
        assert!(auth.is_err());
        assert!(auth.unwrap_err().to_string().contains("32 bytes"));

        let b64_long = URL_SAFE_NO_PAD.encode([0u8; 64]);
        let auth = Auth::new(
            "sa".into(),
            &b64_long,
            "https://ascend.io".into(),
            test_agent(),
        );
        assert!(auth.is_err());
    }

    #[test]
    fn auth_new_rejects_invalid_base64() {
        let auth = Auth::new(
            "sa".into(),
            "!!!invalid!!!",
            "https://ascend.io".into(),
            test_agent(),
        );
        assert!(auth.is_err());
    }

    #[test]
    fn auth_new_trims_whitespace() {
        let b64 = format!("  {}  \n", URL_SAFE_NO_PAD.encode(test_seed()));
        let auth = Auth::new("sa".into(), &b64, "https://ascend.io".into(), test_agent());
        assert!(auth.is_ok());
    }

    // -- JWT signing --

    #[test]
    fn sign_jwt_produces_three_part_token() {
        let auth = test_auth(&test_seed());
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs();
        let jwt = auth.sign_jwt(now, "api.cloud.ascend.io").unwrap();
        assert_eq!(jwt.split('.').count(), 3);
    }

    #[test]
    fn sign_jwt_has_correct_claims() {
        let auth = test_auth(&test_seed());
        let now = 1_700_000_000u64;
        let jwt = auth.sign_jwt(now, "api.cloud.ascend.io").unwrap();

        // Decode the payload (second segment) without signature verification
        let payload_b64 = jwt.split('.').nth(1).unwrap();
        // jsonwebtoken uses URL_SAFE_NO_PAD for JWT segments
        let payload_bytes = URL_SAFE_NO_PAD.decode(payload_b64).unwrap();
        let claims: Value = serde_json::from_slice(&payload_bytes).unwrap();

        assert_eq!(claims["sub"], "asc-sa-test");
        assert_eq!(claims["aud"], "https://api.cloud.ascend.io/auth/token");
        assert_eq!(claims["exp"], now + 300);
        assert_eq!(claims["iat"], now);
        assert_eq!(claims["name"], "asc-sa-test");
        assert_eq!(claims["service_account"], "asc-sa-test");
    }

    // -- Debug redaction --

    #[test]
    fn debug_redacts_key() {
        let seed = test_seed();
        let auth = test_auth(&seed);
        let debug = format!("{auth:?}");
        assert!(debug.contains("[REDACTED]"));
        assert!(!debug.contains(&URL_SAFE_NO_PAD.encode(seed)));
    }
}