cloud_terrastodon_credentials 0.35.0

Helpers for getting Azure PAT and stuff for Cloud Terrastodon
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 chrono::{DateTime, Utc};
// use eyre::eyre;
// use facet::Facet;
// use facet_json::from_str;
// use std::collections::HashMap;
// use windows::Win32::Security::Cryptography::CRYPT_INTEGER_BLOB;
// use windows::Win32::Security::Cryptography::CRYPTPROTECT_UI_FORBIDDEN;
// use windows::Win32::Security::Cryptography::CryptUnprotectData;

// /// Represents the complete MSAL token cache structure
// #[derive(Facet, Debug)]
// pub struct MsalTokenCache {
//     #[facet(rename = "AccessToken")]
//     pub access_tokens: HashMap<String, AccessToken>,
//     #[facet(rename = "Account")]
//     pub accounts: HashMap<String, Account>,
//     #[facet(rename = "IdToken")]
//     pub id_tokens: HashMap<String, IdToken>,
//     #[facet(rename = "AppMetadata")]
//     pub app_metadata: HashMap<String, AppMetadata>,
// }

// /// Represents an access token entry
// #[derive(Facet, Debug)]
// pub struct AccessToken {
//     pub credential_type: String,
//     #[facet(sensitive)]
//     pub secret: String,
//     pub home_account_id: String,
//     pub environment: String,
//     pub client_id: String,
//     pub target: String,
//     pub realm: String,
//     pub token_type: String,
//     /// Unix timestamp when the token was cached
//     pub cached_at: String,
//     /// Unix timestamp when the token expires
//     pub expires_on: String,
//     /// Unix timestamp for extended expiration
//     pub extended_expires_on: String,
// }

// /// Represents an account entry
// #[derive(Facet, Debug)]
// pub struct Account {
//     pub home_account_id: String,
//     pub environment: String,
//     pub realm: String,
//     pub local_account_id: String,
//     pub username: String,
//     pub authority_type: String,
//     pub account_source: String,
// }

// /// Represents an ID token entry
// #[derive(Facet, Debug)]
// pub struct IdToken {
//     pub credential_type: String,
//     #[facet(sensitive)]
//     pub secret: String,
//     pub home_account_id: String,
//     pub environment: String,
//     pub realm: String,
//     pub client_id: String,
// }

// /// Represents app metadata
// #[derive(Facet, Debug)]
// pub struct AppMetadata {
//     pub client_id: String,
//     pub environment: String,
// }

// impl AccessToken {
//     /// Parse the cached_at timestamp as a DateTime
//     pub fn cached_at_datetime(&self) -> eyre::Result<DateTime<Utc>> {
//         let timestamp = self.cached_at.parse::<i64>()?;
//         Ok(DateTime::from_timestamp(timestamp, 0).unwrap_or_default())
//     }

//     /// Parse the expires_on timestamp as a DateTime
//     pub fn expires_on_datetime(&self) -> eyre::Result<DateTime<Utc>> {
//         let timestamp = self.expires_on.parse::<i64>()?;
//         Ok(DateTime::from_timestamp(timestamp, 0).unwrap_or_default())
//     }

//     /// Parse the extended_expires_on timestamp as a DateTime
//     pub fn extended_expires_on_datetime(&self) -> eyre::Result<DateTime<Utc>> {
//         let timestamp = self.extended_expires_on.parse::<i64>()?;
//         Ok(DateTime::from_timestamp(timestamp, 0).unwrap_or_default())
//     }

//     /// Check if the token is expired
//     pub fn is_expired(&self) -> bool {
//         if let Ok(expires_on) = self.expires_on_datetime() {
//             expires_on < Utc::now()
//         } else {
//             true // Consider invalid timestamps as expired
//         }
//     }
// }

// /// Decrypt the MSAL token cache bytes
// ///
// /// This function implements the decryption logic to handle the Windows Data Protection API (DPAPI) encrypted cache file
// pub fn decrypt_msal_token_cache(cache_bytes: &[u8], global_args: &GlobalArgs) -> eyre::Result<()> {
//     tracing::info!(
//         "Attempting to decrypt MSAL token cache ({} bytes)",
//         cache_bytes.len()
//     );

//     // Create input data blob for DPAPI
//     let input_blob = CRYPT_INTEGER_BLOB {
//         cbData: cache_bytes.len() as u32,
//         pbData: cache_bytes.as_ptr() as *mut u8,
//     };

//     let mut output_blob = CRYPT_INTEGER_BLOB::default();

//     // Call Windows DPAPI to decrypt the data
//     let result = unsafe {
//         CryptUnprotectData(
//             &input_blob,               // Input encrypted data
//             None,                      // Description (not needed)
//             None,                      // Optional entropy
//             None,                      // Reserved
//             None,                      // Prompt struct
//             CRYPTPROTECT_UI_FORBIDDEN, // Flags
//             &mut output_blob,          // Output decrypted data
//         )
//     };

//     if result.is_err() {
//         return Err(eyre::eyre!(
//             "Failed to decrypt MSAL token cache: DPAPI decryption failed"
//         ));
//     }

//     // Extract the decrypted data
//     let decrypted_data =
//         unsafe { std::slice::from_raw_parts(output_blob.pbData, output_blob.cbData as usize) };

//     tracing::info!("Successfully decrypted {} bytes", decrypted_data.len());

//     // Convert to string and handle sensitive data display
//     match std::str::from_utf8(decrypted_data) {
//         Ok(json_str) => {
//             if global_args.show_sensitive {
//                 println!("Decrypted token cache content:\n{}", json_str);
//             } else {
//                 println!(
//                     "Decrypted token cache content: <SENSITIVE DATA - use --show-sensitive to view>"
//                 );
//                 tracing::debug!("Content length: {} characters", json_str.len());

//                 // Try to parse as MSAL token cache structure
//                 match from_str::<MsalTokenCache>(json_str) {
//                     Ok(token_cache) => {
//                         display_token_cache_summary(&token_cache);
//                     }
//                     Err(e) => {
//                         tracing::warn!("Failed to parse token cache as MSAL structure: {}", e);
//                         println!("Raw JSON structure (first 500 chars): {}",
//                             &json_str[..json_str.len().min(500)]);
//                     }
//                 }
//             }
//         }
//         Err(_) => {
//             tracing::warn!("Decrypted data is not valid UTF-8, showing as hex dump");
//             if global_args.show_sensitive {
//                 println!(
//                     "Decrypted binary data (hex): {}",
//                     hex::encode(decrypted_data)
//                 );
//             } else {
//                 println!(
//                     "Decrypted binary data: <SENSITIVE DATA - use --show-sensitive to view>"
//                 );
//                 tracing::debug!("Binary data length: {} bytes", decrypted_data.len());
//             }
//         }
//     }

//     // Free the allocated memory from DPAPI
//     // Note: In practice, Windows manages this memory automatically
//     // unsafe {
//     //     windows::Win32::System::Memory::LocalFree(HLOCAL(output_blob.pbData as _));
//     // }

//     Ok(())
// }

// /// Decrypt MSAL token cache and return the JSON string
// fn decrypt_msal_cache_to_string(cache_bytes: &[u8]) -> eyre::Result<String> {
//     tracing::debug!(
//         "Attempting to decrypt MSAL token cache ({} bytes)",
//         cache_bytes.len()
//     );

//     // Create input data blob for DPAPI
//     let input_blob = CRYPT_INTEGER_BLOB {
//         cbData: cache_bytes.len() as u32,
//         pbData: cache_bytes.as_ptr() as *mut u8,
//     };

//     let mut output_blob = CRYPT_INTEGER_BLOB::default();

//     // Call Windows DPAPI to decrypt the data
//     let result = unsafe {
//         CryptUnprotectData(
//             &input_blob,               // Input encrypted data
//             None,                      // Description (not needed)
//             None,                      // Optional entropy
//             None,                      // Reserved
//             None,                      // Prompt struct
//             CRYPTPROTECT_UI_FORBIDDEN, // Flags
//             &mut output_blob,          // Output decrypted data
//         )
//     };

//     if result.is_err() {
//         return Err(eyre::eyre!(
//             "Failed to decrypt MSAL token cache: DPAPI decryption failed"
//         ));
//     }

//     // Extract the decrypted data
//     let decrypted_data =
//         unsafe { std::slice::from_raw_parts(output_blob.pbData, output_blob.cbData as usize) };

//     // Convert to string
//     match std::str::from_utf8(decrypted_data) {
//         Ok(json_str) => Ok(json_str.to_string()),
//         Err(_) => Err(eyre::eyre!("Decrypted data is not valid UTF-8")),
//     }
// }

// /// Display a summary of the token cache without sensitive information
// fn display_token_cache_summary(cache: &MsalTokenCache) {
//     println!("\n=== MSAL Token Cache Summary ===");

//     // Access Tokens Summary
//     println!("\n๐Ÿ“‹ Access Tokens: {}", cache.access_tokens.len());
//     for (key, token) in &cache.access_tokens {
//         println!("  ๐Ÿ”‘ Object Key (not the token): {}", truncate_key(key));
//         println!("     Client ID: {}", token.client_id);
//         println!("     Environment: {}", token.environment);
//         println!("     Target: {}", truncate_target(&token.target));
//         println!("     Token Type: {}", token.token_type);

//         // Parse and display timestamps
//         if let Ok(cached_at) = token.cached_at_datetime() {
//             println!("     Cached At: {}", cached_at.format("%Y-%m-%d %H:%M:%S UTC"));
//         }
//         if let Ok(expires_on) = token.expires_on_datetime() {
//             let status = if token.is_expired() { "โŒ EXPIRED" } else { "โœ… VALID" };
//             println!("     Expires On: {} UTC ({})", expires_on.format("%Y-%m-%d %H:%M:%S"), status);
//         }
//         println!();
//     }

//     // Accounts Summary
//     println!("\n๐Ÿ‘ค Accounts: {}", cache.accounts.len());
//     for (key, account) in &cache.accounts {
//         println!("  ๐Ÿ”‘ Object Key (not the token): {}", truncate_key(key));
//         println!("     Username: {}", account.username);
//         println!("     Environment: {}", account.environment);
//         println!("     Authority Type: {}", account.authority_type);
//         println!("     Account Source: {}", account.account_source);
//         println!();
//     }

//     // ID Tokens Summary
//     println!("\n๐Ÿ†” ID Tokens: {}", cache.id_tokens.len());
//     for (key, token) in &cache.id_tokens {
//         println!("  ๐Ÿ”‘ Object Key (not the token): {}", truncate_key(key));
//         println!("     Client ID: {}", token.client_id);
//         println!("     Environment: {}", token.environment);
//         println!();
//     }

//     // App Metadata Summary
//     println!("\n๐Ÿ“ฑ App Metadata: {}", cache.app_metadata.len());
//     for (key, metadata) in &cache.app_metadata {
//         println!("  ๐Ÿ”‘ Object Key (not the token): {}", truncate_key(key));
//         println!("     Client ID: {}", metadata.client_id);
//         println!("     Environment: {}", metadata.environment);
//         println!();
//     }
// }

// /// Truncate long keys for display
// fn truncate_key(key: &str) -> String {
//     if key.len() > 60 {
//         format!("{}...{}", &key[..30], &key[key.len()-15..])
//     } else {
//         key.to_string()
//     }
// }

// /// Truncate long target strings for display
// fn truncate_target(target: &str) -> String {
//     if target.len() > 80 {
//         format!("{}...", &target[..77])
//     } else {
//         target.to_string()
//     }
// }

// /// Test Azure credentials by making API calls to Microsoft Graph
// pub fn test_azure_credentials(cache_bytes: &[u8], global_args: &GlobalArgs) -> eyre::Result<()> {
//     // Decrypt the cache
//     let decrypted_data = decrypt_msal_cache_to_string(cache_bytes)?;

//     // Parse the decrypted JSON
//     let cache: MsalTokenCache = from_str(&decrypted_data).map_err(|e| eyre!("Failed to deserialize: {e:?}"))?;

//     // Find valid access tokens
//     let mut tested_tokens = 0;
//     let mut successful_tests = 0;

//     for (key, access_token) in &cache.access_tokens {
//         // Skip expired tokens
//         if let Ok(expires_on) = access_token.expires_on.parse::<i64>() {
//             let expiry_time = DateTime::from_timestamp(expires_on, 0)
//                 .unwrap_or_else(|| DateTime::from_timestamp(0, 0).unwrap());
//             if expiry_time <= Utc::now() {
//                 tracing::debug!("Skipping expired token for key: {}", truncate_key(key));
//                 continue;
//             }
//         }

//         tested_tokens += 1;

//         println!("\n๐Ÿงช Testing token for:");
//         println!("   Target: {}", truncate_target(&access_token.target));
//         println!("   Environment: {}", access_token.environment);
//         println!("   Username: {}", get_username_for_token(&cache, &access_token.home_account_id));

//         // Test the token against Microsoft Graph /me endpoint
//         match test_microsoft_graph_token(&access_token.secret, global_args) {
//             Ok(user_info) => {
//                 successful_tests += 1;
//                 println!("   โœ… Success! User: {}", user_info.display_name.unwrap_or_else(|| user_info.user_principal_name.clone()));
//             }
//             Err(e) => {
//                 println!("   โŒ Failed: {}", e);
//             }
//         }
//     }

//     println!("\n๐Ÿ“Š Test Summary:");
//     println!("   Tokens tested: {}", tested_tokens);
//     println!("   Successful: {}", successful_tests);
//     println!("   Failed: {}", tested_tokens - successful_tests);

//     if tested_tokens == 0 {
//         println!("   โ„น๏ธ  No valid tokens found to test");
//     }

//     Ok(())
// }

// /// Test a token against Microsoft Graph API
// fn test_microsoft_graph_token(token: &str, global_args: &GlobalArgs) -> eyre::Result<GraphUserInfo> {
//     let client = reqwest::blocking::Client::new();

//     let response = client
//         .get("https://graph.microsoft.com/v1.0/me")
//         .header("Authorization", format!("Bearer {}", token))
//         .header("Accept", "application/json")
//         .send()?;

//     if response.status().is_success() {
//         let json_text = response.text()?;

//         let user_info: GraphUserInfo = from_str(&json_text).map_err(|e| eyre!("Failed to deserialize: {e:?}"))?;
//         Ok(user_info)
//     } else {
//         let status = response.status();
//         let error_text = response.text()?;

//         if global_args.debug {
//             tracing::debug!("HTTP {}: {}", status, error_text);
//         }

//         Err(eyre::eyre!("HTTP {} - {}", status,
//             if error_text.len() > 100 {
//                 format!("{}...", &error_text[..97])
//             } else {
//                 error_text
//             }))
//     }
// }

// /// Microsoft Graph user information response
// #[derive(Facet, Debug)]
// struct GraphUserInfo {
//     #[facet(rename = "displayName")]
//     display_name: Option<String>,
//     #[facet(rename = "userPrincipalName")]
//     user_principal_name: String,
//     #[facet(rename = "id")]
//     id: String,
// }

// /// Get username for a token based on home account ID
// fn get_username_for_token(cache: &MsalTokenCache, home_account_id: &str) -> String {
//     cache.accounts
//         .values()
//         .find(|account| account.home_account_id == home_account_id)
//         .map(|account| account.username.clone())
//         .unwrap_or_else(|| format!("Unknown ({})", &home_account_id[..8]))
// }