OXYGENHUB
# fddf64baa3d2 oxygen / crates / oxy-core / src / credentials.rs
19.5 KiB · Rust · last changed Sign in once per hub: oxy auth login, status and logout (closes #47) 1 month ago source blame history raw
ff1f62fe Andy McCall ↶ 1
//! Per-user credentials for hubs: one sign-in serves every repository on a
2
//! hub, and nothing secret is written into a checkout.
3
//!
4
//! The store is `credentials.toml` in the user's oxy directory (see
5
//! [`crate::repo::default_home`]), one section per hub:
6
//!
7
//! ```text
8
//! [https://oxygenhub.io]
9
//! account = "andymccall"
10
//! token = "oxyhub_…"
11
//! ```
12
//!
13
//! A hub is a scheme, host and port: a token is only ever sent to the hub
14
//! it was stored for. The file is written readable by its owner only, and
15
//! is ignored with a warning when anyone else can read it. With
16
//! `auth.helper` set in the user's `config.toml`, tokens go to that helper
17
//! instead (Git's credential-helper protocol, so `libsecret`,
18
//! `osxkeychain` and `manager` keep them in the system keyring) and the
19
//! file keeps only what is not secret.
20
//!
21
//! A token is sent over `https`, or over plain `http` to this machine only
22
//! ([`token_allowed`]); hub transports drop it on any redirect.
23
24
use crate::protocol::RemoteOptions;
25
use std::collections::{BTreeMap, HashMap};
26
use std::io::Write;
27
use std::path::{Path, PathBuf};
28
use std::process::{Command, Stdio};
29
use std::sync::{Mutex, OnceLock};
30
31
/// The user name oxy's tokens are stored under in a credential helper, so
32
/// they stay apart from Git credentials for the same host.
33
const HELPER_USER: &str = "oxy-token";
34
35
/// The hub of a URL: `scheme://host[:port]`, lower case, for `http` and
36
/// `https` URLs only.
37
pub fn hub_of(url: &str) -> Option<String> {
38
    let (scheme, rest) = url.split_once("://")?;
39
    let scheme = scheme.to_ascii_lowercase();
40
    if scheme != "http" && scheme != "https" {
41
        return None;
42
    }
43
    let host = rest.split(['/', '?', '#']).next()?;
44
    // Never key a hub on credentials embedded in the URL.
45
    let host = host.rsplit('@').next()?;
46
    if host.is_empty() {
47
        return None;
48
    }
49
    Some(format!("{scheme}://{}", host.to_ascii_lowercase()))
50
}
51
52
/// Whether a token may be sent to `url`: always over `https`, and over
53
/// plain `http` only to this machine (a hub under test, or a runner's own).
54
pub fn token_allowed(url: &str) -> bool {
55
    let Some(hub) = hub_of(url) else {
56
        return true;
57
    };
58
    match hub.split_once("://") {
59
        Some(("https", _)) => true,
60
        Some((_, host)) => {
61
            let name = match host.strip_prefix('[') {
62
                Some(v6) => v6.split(']').next().unwrap_or(""),
63
                None => host.rsplit_once(':').map_or(host, |(h, _)| h),
64
            };
65
            name == "localhost"
66
                || name == "::1"
67
                || name
68
                    .parse::<std::net::Ipv4Addr>()
69
                    .is_ok_and(|a| a.is_loopback())
70
        }
71
        None => false,
72
    }
73
}
74
75
/// What the store holds for one hub.
76
#[derive(Clone, Debug, Default, PartialEq, Eq)]
77
pub struct HubEntry {
78
    /// `scheme://host[:port]`.
79
    pub hub: String,
80
    /// The account the token signs in as, recorded at sign-in.
81
    pub account: Option<String>,
82
    /// The token, when it is in the file (not with a helper).
83
    pub token: Option<String>,
84
    /// Extra roots to trust for this hub (a self-signed hub).
85
    pub ca: Option<PathBuf>,
86
}
87
88
/// Where a saved token went.
89
#[derive(Clone, Debug, PartialEq, Eq)]
90
pub enum Stored {
91
    /// The credentials file, owner-only.
92
    File(PathBuf),
93
    /// A credential helper, by its `auth.helper` value.
94
    Helper(String),
95
}
96
97
/// The credential store of one oxy home directory.
98
#[derive(Clone, Debug)]
99
pub struct Credentials {
100
    home: PathBuf,
101
}
102
103
impl Credentials {
104
    /// The store in `home` (an oxy home directory).
105
    pub fn at(home: impl Into<PathBuf>) -> Credentials {
106
        Credentials { home: home.into() }
107
    }
108
109
    /// The store in the user's oxy home directory.
110
    pub fn user() -> Credentials {
111
        Credentials::at(crate::repo::default_home())
112
    }
113
114
    /// `credentials.toml` in the home directory.
115
    pub fn path(&self) -> PathBuf {
116
        self.home.join("credentials.toml")
117
    }
118
119
    /// The configured credential helper (`auth.helper` in the user file).
120
    pub fn helper(&self) -> Option<String> {
121
        crate::repo::read_config_file(&self.home.join("config.toml"))
122
            .ok()?
123
            .remove("auth.helper")
124
            .filter(|h| !h.trim().is_empty())
125
    }
126
127
    /// Every hub in the file, tokens as stored there (none with a helper).
128
    pub fn entries(&self) -> Result<Vec<HubEntry>, String> {
129
        Ok(self.read()?.into_values().collect())
130
    }
131
132
    /// Whether the file can be read by anyone but its owner, in which case
133
    /// lookups ignore it.
134
    pub fn too_open(&self) -> bool {
135
        #[cfg(unix)]
136
        {
137
            use std::os::unix::fs::PermissionsExt;
138
            if let Ok(m) = std::fs::metadata(self.path()) {
139
                return m.permissions().mode() & 0o077 != 0;
140
            }
141
        }
142
        false
143
    }
144
145
    /// The entry for `hub`, its token fetched from the helper if one is set.
146
    pub fn get(&self, hub: &str) -> Result<Option<HubEntry>, String> {
147
        let entry = self.read()?.remove(hub);
148
        let helper = self.helper();
149
        let mut entry = match (entry, &helper) {
150
            (Some(e), _) => e,
151
            // A helper may hold a token stored before the file was cleared.
152
            (None, Some(_)) => HubEntry {
153
                hub: hub.to_string(),
154
                ..HubEntry::default()
155
            },
156
            (None, None) => return Ok(None),
157
        };
158
        if entry.token.is_none() {
159
            if let Some(h) = &helper {
160
                entry.token = run_helper(h, "get", hub, None)?;
161
            }
162
        }
163
        if entry.token.is_none() && entry.account.is_none() && entry.ca.is_none() {
164
            return Ok(None);
165
        }
166
        Ok(Some(entry))
167
    }
168
169
    /// Save a token (and what is known about it) for `hub`.
170
    pub fn save(
171
        &self,
172
        hub: &str,
173
        token: &str,
174
        account: Option<&str>,
175
        ca: Option<&Path>,
176
    ) -> Result<Stored, String> {
177
        let mut all = self.read()?;
178
        let helper = self.helper();
179
        let entry = all.entry(hub.to_string()).or_insert_with(|| HubEntry {
180
            hub: hub.to_string(),
181
            ..HubEntry::default()
182
        });
183
        entry.account = account.map(String::from).or(entry.account.take());
184
        if let Some(ca) = ca {
185
            entry.ca = Some(ca.to_path_buf());
186
        }
187
        let stored = match &helper {
188
            Some(h) => {
189
                run_helper(h, "store", hub, Some(token))?;
190
                entry.token = None;
191
                Stored::Helper(h.clone())
192
            }
193
            None => {
194
                entry.token = Some(token.to_string());
195
                Stored::File(self.path())
196
            }
197
        };
198
        self.write(&all)?;
199
        forget_cached();
200
        Ok(stored)
201
    }
202
203
    /// Remove `hub` from the file and the helper; returns what it held,
204
    /// with the token, so the caller can revoke it.
205
    pub fn remove(&self, hub: &str) -> Result<Option<HubEntry>, String> {
206
        let found = self.get(hub)?;
207
        let mut all = self.read()?;
208
        if all.remove(hub).is_some() {
209
            self.write(&all)?;
210
        }
211
        if let Some(h) = self.helper() {
212
            run_helper(&h, "erase", hub, None)?;
213
        }
214
        forget_cached();
215
        Ok(found)
216
    }
217
218
    fn read(&self) -> Result<BTreeMap<String, HubEntry>, String> {
219
        let path = self.path();
220
        let text = match std::fs::read_to_string(&path) {
221
            Ok(t) => t,
222
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(BTreeMap::new()),
223
            Err(e) => return Err(format!("reading {}: {e}", path.display())),
224
        };
225
        let mut out = BTreeMap::new();
226
        let mut current: Option<HubEntry> = None;
227
        for (n, line) in text.lines().enumerate() {
228
            let line = line.trim();
229
            if line.is_empty() || line.starts_with('#') {
230
                continue;
231
            }
232
            if let Some(hub) = line.strip_prefix('[').and_then(|l| l.strip_suffix(']')) {
233
                if let Some(e) = current.take() {
234
                    out.insert(e.hub.clone(), e);
235
                }
236
                current = Some(HubEntry {
237
                    hub: hub.trim().to_string(),
238
                    ..HubEntry::default()
239
                });
240
                continue;
241
            }
242
            let bad = || {
243
                format!(
244
                    "{} line {}: expected key = \"value\"",
245
                    path.display(),
246
                    n + 1
247
                )
248
            };
249
            let (k, v) = line.split_once('=').ok_or_else(bad)?;
250
            let v = unquote(v.trim()).ok_or_else(bad)?;
251
            let Some(e) = current.as_mut() else {
252
                return Err(format!(
253
                    "{} line {}: a key before any [hub] section",
254
                    path.display(),
255
                    n + 1
256
                ));
257
            };
258
            match k.trim() {
259
                "account" => e.account = Some(v),
260
                "token" => e.token = Some(v),
261
                "ca" => e.ca = Some(PathBuf::from(v)),
262
                // Unknown keys are kept out of lookups, not fatal.
263
                _ => {}
264
            }
265
        }
266
        if let Some(e) = current {
267
            out.insert(e.hub.clone(), e);
268
        }
269
        Ok(out)
270
    }
271
272
    fn write(&self, all: &BTreeMap<String, HubEntry>) -> Result<(), String> {
273
        let mut text =
274
            String::from("# oxy credentials: a token for each hub. Keep this file private.\n");
275
        for e in all.values() {
276
            text.push_str(&format!("\n[{}]\n", e.hub));
277
            if let Some(a) = &e.account {
278
                text.push_str(&format!("account = {}\n", quote(a)));
279
            }
280
            if let Some(t) = &e.token {
281
                text.push_str(&format!("token = {}\n", quote(t)));
282
            }
283
            if let Some(c) = &e.ca {
284
                text.push_str(&format!("ca = {}\n", quote(&c.to_string_lossy())));
285
            }
286
        }
287
        let path = self.path();
288
        std::fs::create_dir_all(&self.home)
289
            .map_err(|e| format!("creating {}: {e}", self.home.display()))?;
290
        crate::durable::write_atomic_private(&path, text.as_bytes())
291
            .map_err(|e| format!("writing {}: {e}", path.display()))
292
    }
293
294
    /// The stored token and CA for `url`'s hub, if any: memoized, so a
295
    /// helper runs once per hub in a process. A file anyone else can read
296
    /// is ignored, with a warning.
297
    pub fn lookup(&self, url: &str) -> (Option<String>, Option<PathBuf>) {
298
        let Some(hub) = hub_of(url) else {
299
            return (None, None);
300
        };
301
        let key = (self.home.clone(), hub.clone());
302
        if let Some(hit) = cache().lock().unwrap().get(&key) {
303
            return hit.clone();
304
        }
305
        let found = if self.too_open() {
306
            warn_once(&format!(
307
                "warning: {} can be read by other users, so oxy ignores it; run chmod 600 on it",
308
                self.path().display()
309
            ));
310
            (None, None)
311
        } else {
312
            match self.get(&hub) {
313
                Ok(Some(e)) => (e.token, e.ca),
314
                Ok(None) => (None, None),
315
                Err(e) => {
316
                    warn_once(&format!("warning: credentials for {hub}: {e}"));
317
                    (None, None)
318
                }
319
            }
320
        };
321
        cache().lock().unwrap().insert(key, found.clone());
322
        found
323
    }
324
325
    /// Options for reaching `url` without a configured remote: `$OXY_TOKEN`
326
    /// and `$OXY_CA`, else what this store holds for its hub.
327
    pub fn options_for(&self, url: &str) -> RemoteOptions {
328
        let env_token = std::env::var("OXY_TOKEN").ok().filter(|t| !t.is_empty());
329
        let env_ca = std::env::var("OXY_CA").ok().map(PathBuf::from);
330
        let (token, ca) = match (&env_token, &env_ca) {
331
            (Some(_), Some(_)) => (None, None),
332
            _ => self.lookup(url),
333
        };
334
        RemoteOptions {
335
            token: env_token.or(token),
336
            ca: env_ca.or(ca),
337
            repo_id: None,
338
        }
339
    }
340
}
341
342
type Cache = Mutex<HashMap<(PathBuf, String), (Option<String>, Option<PathBuf>)>>;
343
344
fn cache() -> &'static Cache {
345
    static CACHE: OnceLock<Cache> = OnceLock::new();
346
    CACHE.get_or_init(Default::default)
347
}
348
349
fn forget_cached() {
350
    cache().lock().unwrap().clear();
351
}
352
353
fn warn_once(message: &str) {
354
    static SEEN: OnceLock<Mutex<std::collections::HashSet<String>>> = OnceLock::new();
355
    if SEEN
356
        .get_or_init(Default::default)
357
        .lock()
358
        .unwrap()
359
        .insert(message.to_string())
360
    {
361
        eprintln!("{message}");
362
    }
363
}
364
365
fn quote(v: &str) -> String {
366
    let mut out = String::from('"');
367
    for c in v.chars() {
368
        match c {
369
            '"' => out.push_str("\\\""),
370
            '\\' => out.push_str("\\\\"),
371
            '\n' => out.push_str("\\n"),
372
            c => out.push(c),
373
        }
374
    }
375
    out.push('"');
376
    out
377
}
378
379
fn unquote(v: &str) -> Option<String> {
380
    let inner = v.strip_prefix('"')?.strip_suffix('"')?;
381
    let mut out = String::new();
382
    let mut chars = inner.chars();
383
    while let Some(c) = chars.next() {
384
        if c == '\\' {
385
            match chars.next()? {
386
                'n' => out.push('\n'),
387
                c => out.push(c),
388
            }
389
        } else {
390
            out.push(c);
391
        }
392
    }
393
    Some(out)
394
}
395
396
/// Run a credential helper (Git's protocol): `get`, `store` or `erase` for
397
/// `hub`. `get` returns the password the helper answered with.
398
///
399
/// `auth.helper` is a helper name (run as `git credential-NAME`), an
400
/// absolute path, or a shell command after `!`, as in Git.
401
fn run_helper(
402
    helper: &str,
403
    action: &str,
404
    hub: &str,
405
    token: Option<&str>,
406
) -> Result<Option<String>, String> {
407
    let (protocol, host) = hub
408
        .split_once("://")
409
        .ok_or_else(|| format!("{hub} is not a hub URL"))?;
410
    let mut cmd = if let Some(shell) = helper.strip_prefix('!') {
411
        let mut c = Command::new("sh");
412
        c.arg("-c").arg(format!("{shell} {action}"));
413
        c
414
    } else if Path::new(helper).is_absolute() {
415
        let mut c = Command::new(helper);
416
        c.arg(action);
417
        c
418
    } else {
419
        let mut c = Command::new("git");
420
        c.arg(format!("credential-{helper}")).arg(action);
421
        c
422
    };
423
    let mut input = format!("protocol={protocol}\nhost={host}\nusername={HELPER_USER}\n");
424
    if let Some(t) = token {
425
        input.push_str(&format!("password={t}\n"));
426
    }
427
    input.push('\n');
428
    let mut child = cmd
429
        .stdin(Stdio::piped())
430
        .stdout(Stdio::piped())
431
        .stderr(Stdio::inherit())
432
        .spawn()
433
        .map_err(|e| format!("running credential helper {helper:?}: {e}"))?;
434
    child
435
        .stdin
436
        .take()
437
        .expect("piped")
438
        .write_all(input.as_bytes())
439
        .map_err(|e| format!("credential helper {helper:?}: {e}"))?;
440
    let out = child
441
        .wait_with_output()
442
        .map_err(|e| format!("credential helper {helper:?}: {e}"))?;
443
    if !out.status.success() {
444
        return Err(format!("credential helper {helper:?} {action} failed"));
445
    }
446
    if action != "get" {
447
        return Ok(None);
448
    }
449
    Ok(String::from_utf8_lossy(&out.stdout)
450
        .lines()
451
        .find_map(|l| l.strip_prefix("password="))
452
        .map(String::from)
453
        .filter(|p| !p.is_empty()))
454
}
455
456
#[cfg(test)]
457
mod tests {
458
    use super::*;
459
460
    #[test]
461
    fn hubs_are_scheme_host_and_port() {
462
        assert_eq!(
463
            hub_of("https://OxygenHub.io/doomsdayone/metalgear").as_deref(),
464
            Some("https://oxygenhub.io")
465
        );
466
        assert_eq!(
467
            hub_of("http://127.0.0.1:7300/a/b").as_deref(),
468
            Some("http://127.0.0.1:7300")
469
        );
470
        assert_eq!(
471
            hub_of("https://user:[email protected]/x").as_deref(),
472
            Some("https://hub.example")
473
        );
474
        assert_eq!(hub_of("/srv/repo"), None);
475
        assert_eq!(hub_of("file:///srv/repo"), None);
476
    }
477
478
    #[test]
479
    fn tokens_go_over_https_or_to_this_machine() {
480
        assert!(token_allowed("https://oxygenhub.io/a/b"));
481
        assert!(token_allowed("http://127.0.0.1:7300/a/b"));
482
        assert!(token_allowed("http://localhost/a"));
483
        assert!(token_allowed("http://[::1]:8080/a"));
484
        assert!(!token_allowed("http://oxygenhub.io/a/b"));
485
        assert!(!token_allowed("http://10.0.0.1/a"));
486
        assert!(!token_allowed("http://127.0.0.1.evil.example/a"));
487
    }
488
489
    #[test]
490
    fn save_get_and_remove_round_trip_privately() {
491
        let dir = tempfile::tempdir().unwrap();
492
        let store = Credentials::at(dir.path());
493
        assert_eq!(store.get("https://hub.example").unwrap(), None);
494
        let stored = store
495
            .save("https://hub.example", "tok\"en", Some("andy"), None)
496
            .unwrap();
497
        assert_eq!(stored, Stored::File(store.path()));
498
        store
499
            .save(
500
                "http://127.0.0.1:7300",
501
                "local",
502
                None,
503
                Some(Path::new("/ca.pem")),
504
            )
505
            .unwrap();
506
        let e = store.get("https://hub.example").unwrap().unwrap();
507
        assert_eq!(e.token.as_deref(), Some("tok\"en"));
508
        assert_eq!(e.account.as_deref(), Some("andy"));
509
        assert!(!store.too_open());
510
        assert_eq!(
511
            store.lookup("https://hub.example/o/r"),
512
            (Some("tok\"en".into()), None)
513
        );
514
        // Another host, scheme or port gets nothing.
515
        assert_eq!(store.lookup("https://other.example/o/r"), (None, None));
516
        assert_eq!(store.lookup("http://hub.example/o/r"), (None, None));
517
        assert_eq!(store.lookup("https://hub.example:8443/o/r"), (None, None));
518
        let gone = store.remove("https://hub.example").unwrap().unwrap();
519
        assert_eq!(gone.token.as_deref(), Some("tok\"en"));
520
        assert_eq!(store.lookup("https://hub.example/o/r"), (None, None));
521
        assert_eq!(store.entries().unwrap().len(), 1);
522
    }
523
524
    #[cfg(unix)]
525
    #[test]
526
    fn a_file_others_can_read_is_ignored() {
527
        use std::os::unix::fs::PermissionsExt;
528
        let dir = tempfile::tempdir().unwrap();
529
        let store = Credentials::at(dir.path());
530
        store.save("https://open.example", "t", None, None).unwrap();
531
        let mode = std::fs::metadata(store.path())
532
            .unwrap()
533
            .permissions()
534
            .mode();
535
        assert_eq!(mode & 0o777, 0o600);
536
        std::fs::set_permissions(store.path(), std::fs::Permissions::from_mode(0o644)).unwrap();
537
        assert!(store.too_open());
538
        assert_eq!(store.lookup("https://open.example/o/r"), (None, None));
539
    }
540
541
    #[cfg(unix)]
542
    #[test]
543
    fn a_helper_keeps_the_token_out_of_the_file() {
544
        let dir = tempfile::tempdir().unwrap();
545
        let vault = dir.path().join("vault");
546
        // A helper that keeps one password in a file, as a keyring would.
547
        let helper = format!(
548
            "!f() {{ case $1 in get) [ -f {v} ] && echo password=$(cat {v});; store) sed -n 's/^password=//p' > {v};; erase) rm -f {v};; esac; }}; f",
549
            v = vault.display()
550
        );
551
        std::fs::write(
552
            dir.path().join("config.toml"),
553
            format!("auth.helper = \"{helper}\"\n"),
554
        )
555
        .unwrap();
556
        let store = Credentials::at(dir.path());
557
        assert!(store.helper().is_some());
558
        let stored = store
559
            .save("https://hub.example", "secret", Some("andy"), None)
560
            .unwrap();
561
        assert!(matches!(stored, Stored::Helper(_)));
562
        let text = std::fs::read_to_string(store.path()).unwrap();
563
        assert!(!text.contains("secret"), "{text}");
564
        assert_eq!(
565
            store
566
                .get("https://hub.example")
567
                .unwrap()
568
                .unwrap()
569
                .token
570
                .as_deref(),
571
            Some("secret")
572
        );
573
        store.remove("https://hub.example").unwrap();
574
        assert!(!vault.exists());
575
    }
576
}