spikes 0.3.2

Drop-in feedback collection for static HTML mockups
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
//! Authentication token management with secure storage
//!
//! This module handles:
//! - Platform-appropriate config directory paths (XDG-compliant)
//! - Secure token storage with 0600 file permissions
//! - SPIKES_TOKEN environment variable override
//! - Token lifecycle (load, save, delete)
//! - SPIKES_API_URL environment variable for API base URL override

use std::fs;
use std::io;
use std::path::PathBuf;

use serde::{Deserialize, Serialize};

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

/// Default API base URL for spikes.sh hosted service
const DEFAULT_API_BASE: &str = "https://spikes.sh";

/// Environment variable name for API URL override
const SPIKES_API_URL_ENV: &str = "SPIKES_API_URL";

/// Get the API base URL, checking SPIKES_API_URL env var first.
/// Falls back to https://spikes.sh if not set.
///
/// This enables:
/// - Testing against localhost:8787 (wrangler dev)
/// - Self-hosted deployments
/// - Development/staging environments
pub fn get_api_base() -> String {
    std::env::var(SPIKES_API_URL_ENV)
        .unwrap_or_else(|_| DEFAULT_API_BASE.to_string())
}

/// Auth configuration stored in platform-appropriate config directory
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct AuthConfig {
    #[serde(default)]
    pub auth: AuthSection,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct AuthSection {
    /// Bearer token for API authentication
    pub token: Option<String>,
    /// API key (sk_spikes_ prefixed) stored separately from bearer token
    pub api_key: Option<String>,
}

impl AuthConfig {
    /// Load auth config from platform-appropriate location.
    /// Returns default (empty) config if file doesn't exist.
    pub fn load() -> Result<Self> {
        // Check SPIKES_TOKEN environment variable first
        if let Ok(token) = std::env::var("SPIKES_TOKEN") {
            if !token.is_empty() {
                return Ok(AuthConfig {
                    auth: AuthSection {
                        token: Some(token),
                        api_key: None,
                    },
                });
            }
        }

        let auth_path = auth_path()?;

        if !auth_path.exists() {
            return Ok(Self::default());
        }

        let content = fs::read_to_string(&auth_path)?;
        let config: AuthConfig = toml::from_str(&content).map_err(|e| {
            Error::Io(io::Error::new(
                io::ErrorKind::InvalidData,
                format!("Invalid auth.toml at {}: {}\nHint: Delete the file and run `spikes login` again.", auth_path.display(), e),
            ))
        })?;

        Ok(config)
    }

    /// Save auth config to platform-appropriate location with 0600 permissions
    pub fn save(&self) -> Result<()> {
        let auth_path = auth_path()?;

        // Create parent directories if needed
        if let Some(parent) = auth_path.parent() {
            fs::create_dir_all(parent)?;
        }

        // Serialize to TOML
        let content = toml::to_string_pretty(self).map_err(|e| {
            Error::Io(io::Error::new(
                io::ErrorKind::InvalidData,
                format!("Failed to serialize auth config: {}", e),
            ))
        })?;

        // Write to file
        fs::write(&auth_path, content)?;

        // Set file permissions to 0600 (owner read/write only)
        set_secure_permissions(&auth_path)?;

        Ok(())
    }

    /// Delete the auth config file
    pub fn delete() -> Result<()> {
        let auth_path = auth_path()?;

        if auth_path.exists() {
            fs::remove_file(&auth_path)?;
        }

        Ok(())
    }

    /// Check if a token is available (either from env var or file)
    pub fn has_token() -> bool {
        // Check env var first
        if let Ok(token) = std::env::var("SPIKES_TOKEN") {
            if !token.is_empty() {
                return true;
            }
        }

        // Check file
        Self::load()
            .map(|c| c.auth.token.is_some())
            .unwrap_or(false)
    }

    /// Get the effective token (env var takes precedence over file)
    pub fn token() -> Result<Option<String>> {
        // Check env var first
        if let Ok(token) = std::env::var("SPIKES_TOKEN") {
            if !token.is_empty() {
                return Ok(Some(token));
            }
        }

        // Fall back to file
        let config = Self::load()?;
        Ok(config.auth.token)
    }

    /// Save a new token to the auth file, preserving existing api_key
    pub fn save_token(token: &str) -> Result<()> {
        // Load existing config to preserve api_key
        let existing = Self::load_from_file().unwrap_or_default();
        let config = AuthConfig {
            auth: AuthSection {
                token: Some(token.to_string()),
                api_key: existing.auth.api_key,
            },
        };
        config.save()
    }

    /// Save a new API key to the auth file, preserving existing bearer token
    pub fn save_api_key(key: &str) -> Result<()> {
        // Load existing config to preserve token
        let existing = Self::load_from_file().unwrap_or_default();
        let config = AuthConfig {
            auth: AuthSection {
                token: existing.auth.token,
                api_key: Some(key.to_string()),
            },
        };
        config.save()
    }

    /// Load the API key from the auth file (does not check env var)
    pub fn load_api_key() -> Option<String> {
        Self::load_from_file()
            .ok()
            .and_then(|c| c.auth.api_key)
    }

    /// Load auth config from file only (no env var override).
    /// Used internally to preserve fields when saving.
    fn load_from_file() -> Result<Self> {
        let auth_path = auth_path()?;

        if !auth_path.exists() {
            return Ok(Self::default());
        }

        let content = fs::read_to_string(&auth_path)?;
        let config: AuthConfig = toml::from_str(&content).map_err(|e| {
            Error::Io(io::Error::new(
                io::ErrorKind::InvalidData,
                format!("Invalid auth.toml at {}: {}\nHint: Delete the file and run `spikes login` again.", auth_path.display(), e),
            ))
        })?;

        Ok(config)
    }

    /// Clear the stored token (delete auth file)
    pub fn clear_token() -> Result<()> {
        Self::delete()
    }
}

/// Get the platform-appropriate auth file path.
///
/// XDG-compliant on Linux, standard config directories on macOS and Windows:
/// - Linux: ~/.config/spikes/auth.toml (respects XDG_CONFIG_HOME)
/// - macOS: ~/Library/Application Support/spikes/auth.toml
/// - Windows: %APPDATA%/spikes/auth.toml
pub fn auth_path() -> Result<PathBuf> {
    let config_dir = dirs::config_dir()
        .ok_or_else(|| {
            Error::Io(io::Error::new(
                io::ErrorKind::NotFound,
                "Could not determine config directory",
            ))
        })?
        .join("spikes");

    Ok(config_dir.join("auth.toml"))
}

/// Set file permissions to 0600 (owner read/write only)
#[cfg(unix)]
fn set_secure_permissions(path: &PathBuf) -> Result<()> {
    use std::os::unix::fs::PermissionsExt;

    let mut perms = fs::metadata(path)?.permissions();
    // 0o600 = owner read + write
    perms.set_mode(0o600);
    fs::set_permissions(path, perms)?;

    Ok(())
}

/// Set file permissions to owner-only on non-Unix systems
#[cfg(not(unix))]
fn set_secure_permissions(path: &PathBuf) -> Result<()> {
    // On Windows, we can't set Unix-style permissions directly
    // The file will be accessible only to the user who created it
    // by default on NTFS with proper ACL inheritance
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;
    use serial_test::serial;

    fn setup_temp_config_dir() -> TempDir {
        tempfile::tempdir().expect("Failed to create temp dir")
    }

    #[test]
    fn test_auth_config_default() {
        let config = AuthConfig::default();
        assert!(config.auth.token.is_none());
    }

    #[test]
    fn test_auth_config_save_and_load() {
        let temp_dir = setup_temp_config_dir();
        let auth_path = temp_dir.path().join("spikes").join("auth.toml");

        // Save a config
        let config = AuthConfig {
            auth: AuthSection {
                token: Some("test-token-123".to_string()),
                api_key: None,
            },
        };

        // Manually write to temp path
        if let Some(parent) = auth_path.parent() {
            fs::create_dir_all(parent).unwrap();
        }
        let content = toml::to_string_pretty(&config).unwrap();
        fs::write(&auth_path, content).unwrap();

        // Verify content
        let loaded_content = fs::read_to_string(&auth_path).unwrap();
        assert!(loaded_content.contains("test-token-123"));
    }

    #[test]
    fn test_auth_config_load_missing_file() {
        let temp_dir = tempfile::tempdir().unwrap();
        let _missing_path = temp_dir.path().join("nonexistent/auth.toml");

        // Should return default for missing file
        let config = AuthConfig::default();
        assert!(config.auth.token.is_none());
    }

    #[test]
    fn test_auth_config_invalid_toml() {
        let temp_dir = tempfile::tempdir().unwrap();
        let auth_path = temp_dir.path().join("auth.toml");

        fs::write(&auth_path, "this is not valid toml [[[[").unwrap();

        let content = fs::read_to_string(&auth_path).unwrap();
        let result: Result<AuthConfig> = toml::from_str(&content)
            .map_err(|e| {
                Error::Io(io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!("Invalid auth.toml at {}: {}\nHint: Delete the file and run `spikes login` again.", auth_path.display(), e),
                ))
            });

        assert!(result.is_err());
    }

    #[test]
    #[serial(spike_token)]
    fn test_spike_token_env_override() {
        // Save current value
        let original = std::env::var("SPIKES_TOKEN").ok();

        // Set env var
        std::env::set_var("SPIKES_TOKEN", "env-token-override");

        // Create a new config - env var should populate it
        let config = AuthConfig::load().unwrap();
        assert_eq!(config.auth.token, Some("env-token-override".to_string()));

        // Restore original value
        if let Some(val) = original {
            std::env::set_var("SPIKES_TOKEN", val);
        } else {
            std::env::remove_var("SPIKES_TOKEN");
        }
    }

    #[test]
    #[serial(spike_token)]
    fn test_spike_token_env_empty_ignored() {
        // Save current values
        let original = std::env::var("SPIKES_TOKEN").ok();
        let original_home = std::env::var("HOME").ok();
        let original_xdg = std::env::var("XDG_CONFIG_HOME").ok();

        // Isolate from real auth.toml
        let temp_dir = tempfile::tempdir().unwrap();
        std::env::set_var("HOME", temp_dir.path());
        std::env::set_var("XDG_CONFIG_HOME", temp_dir.path().join(".config"));

        // Set empty env var - should be ignored
        std::env::set_var("SPIKES_TOKEN", "");

        // Empty env var should be ignored, fall back to file (or None)
        let config = AuthConfig::load().unwrap();
        // Token should be None since env var is empty and no auth.toml exists
        assert!(config.auth.token.is_none());

        // Restore original values
        if let Some(val) = original {
            std::env::set_var("SPIKES_TOKEN", val);
        } else {
            std::env::remove_var("SPIKES_TOKEN");
        }
        if let Some(val) = original_home {
            std::env::set_var("HOME", val);
        } else {
            std::env::remove_var("HOME");
        }
        if let Some(val) = original_xdg {
            std::env::set_var("XDG_CONFIG_HOME", val);
        } else {
            std::env::remove_var("XDG_CONFIG_HOME");
        }
    }

    #[test]
    fn test_save_token_creates_parent_dirs() {
        let temp_dir = tempfile::tempdir().unwrap();
        let custom_path = temp_dir.path().join("nested/dir/auth.toml");

        // Create a config and write it manually
        let config = AuthConfig {
            auth: AuthSection {
                token: Some("test-token".to_string()),
                api_key: None,
            },
        };

        if let Some(parent) = custom_path.parent() {
            fs::create_dir_all(parent).unwrap();
        }
        let content = toml::to_string_pretty(&config).unwrap();
        fs::write(&custom_path, content).unwrap();

        assert!(custom_path.exists());
    }

    #[test]
    #[cfg(unix)]
    fn test_secure_permissions_on_new_file() {
        use std::os::unix::fs::PermissionsExt;

        let temp_dir = tempfile::tempdir().unwrap();
        let auth_path = temp_dir.path().join("auth.toml");

        // Write content
        fs::write(&auth_path, "test").unwrap();

        // Set permissions
        set_secure_permissions(&auth_path).unwrap();

        // Verify permissions
        let perms = fs::metadata(&auth_path).unwrap().permissions();
        let mode = perms.mode();

        // Should be 0o600 or more restrictive
        assert_eq!(mode & 0o777, 0o600);
    }

    #[test]
    fn test_toml_serialization() {
        let config = AuthConfig {
            auth: AuthSection {
                token: Some("secret-token-xyz".to_string()),
                api_key: None,
            },
        };

        let toml_str = toml::to_string_pretty(&config).unwrap();

        assert!(toml_str.contains("[auth]"));
        assert!(toml_str.contains("secret-token-xyz"));
    }

    #[test]
    fn test_toml_deserialization() {
        let toml_str = r#"
[auth]
token = "deserialized-token"
"#;

        let config: AuthConfig = toml::from_str(toml_str).unwrap();
        assert_eq!(config.auth.token, Some("deserialized-token".to_string()));
    }

    #[test]
    fn test_empty_auth_section() {
        let toml_str = "";

        let config: AuthConfig = toml::from_str(toml_str).unwrap();
        assert!(config.auth.token.is_none());
        assert!(config.auth.api_key.is_none());
    }

    #[test]
    #[serial(api_url)]
    fn test_get_api_base_default() {
        // Save current value
        let original = std::env::var(SPIKES_API_URL_ENV).ok();

        // Clear env var
        std::env::remove_var(SPIKES_API_URL_ENV);

        // Should return default
        let base = get_api_base();
        assert_eq!(base, DEFAULT_API_BASE);

        // Restore original value
        if let Some(val) = original {
            std::env::set_var(SPIKES_API_URL_ENV, val);
        }
    }

    #[test]
    #[serial(api_url)]
    fn test_get_api_base_env_override() {
        // Save current value
        let original = std::env::var(SPIKES_API_URL_ENV).ok();

        // Set custom API URL
        std::env::set_var(SPIKES_API_URL_ENV, "http://localhost:8787");

        // Should return env var value
        let base = get_api_base();
        assert_eq!(base, "http://localhost:8787");

        // Restore original value
        if let Some(val) = original {
            std::env::set_var(SPIKES_API_URL_ENV, val);
        } else {
            std::env::remove_var(SPIKES_API_URL_ENV);
        }
    }

    #[test]
    #[serial(api_url)]
    fn test_get_api_base_custom_host() {
        // Save current value
        let original = std::env::var(SPIKES_API_URL_ENV).ok();

        // Set custom API URL for self-hosted
        std::env::set_var(SPIKES_API_URL_ENV, "https://spikes.example.com");

        // Should return custom host
        let base = get_api_base();
        assert_eq!(base, "https://spikes.example.com");

        // Restore original value
        if let Some(val) = original {
            std::env::set_var(SPIKES_API_URL_ENV, val);
        } else {
            std::env::remove_var(SPIKES_API_URL_ENV);
        }
    }

    #[test]
    fn test_auth_config_default_has_no_api_key() {
        let config = AuthConfig::default();
        assert!(config.auth.api_key.is_none());
    }

    #[test]
    fn test_toml_with_api_key() {
        let toml_str = r#"
[auth]
token = "bearer-token-123"
api_key = "sk_spikes_testkey"
"#;

        let config: AuthConfig = toml::from_str(toml_str).unwrap();
        assert_eq!(config.auth.token, Some("bearer-token-123".to_string()));
        assert_eq!(config.auth.api_key, Some("sk_spikes_testkey".to_string()));
    }

    #[test]
    fn test_toml_with_api_key_only() {
        let toml_str = r#"
[auth]
api_key = "sk_spikes_onlykey"
"#;

        let config: AuthConfig = toml::from_str(toml_str).unwrap();
        assert!(config.auth.token.is_none());
        assert_eq!(config.auth.api_key, Some("sk_spikes_onlykey".to_string()));
    }

    #[test]
    fn test_toml_serialization_with_api_key() {
        let config = AuthConfig {
            auth: AuthSection {
                token: Some("my-token".to_string()),
                api_key: Some("sk_spikes_mykey".to_string()),
            },
        };

        let toml_str = toml::to_string_pretty(&config).unwrap();
        assert!(toml_str.contains("[auth]"));
        assert!(toml_str.contains("my-token"));
        assert!(toml_str.contains("sk_spikes_mykey"));
    }

    #[test]
    fn test_toml_backwards_compat_without_api_key() {
        // Old format without api_key should still deserialize
        let toml_str = r#"
[auth]
token = "old-format-token"
"#;

        let config: AuthConfig = toml::from_str(toml_str).unwrap();
        assert_eq!(config.auth.token, Some("old-format-token".to_string()));
        assert!(config.auth.api_key.is_none());
    }
}