Improve use of github token (#1769)

* Add new dep zeroize

* Use Zeroizing to avoid leaking the token

* Optimize gh-auth-token

Spawn it as a task, and only await it
when using GhApiClient

* Fix binstalk-git-repo-api unit tests
This commit is contained in:
Jiahao XU 2024-06-15 15:42:09 +10:00 committed by GitHub
parent e3c8c40806
commit fff6aa8122
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 128 additions and 54 deletions

View file

@ -1,9 +1,9 @@
use std::{env, fs, path::PathBuf};
use compact_str::CompactString;
use dirs::home_dir;
use zeroize::Zeroizing;
pub fn try_from_home() -> Option<CompactString> {
pub fn try_from_home() -> Option<Zeroizing<Box<str>>> {
if let Some(mut home) = home_dir() {
home.push(".git-credentials");
if let Some(cred) = from_file(home) {
@ -23,12 +23,12 @@ pub fn try_from_home() -> Option<CompactString> {
None
}
fn from_file(path: PathBuf) -> Option<CompactString> {
fs::read_to_string(path)
.ok()?
fn from_file(path: PathBuf) -> Option<Zeroizing<Box<str>>> {
Zeroizing::new(fs::read_to_string(path).ok()?)
.lines()
.find_map(from_line)
.map(CompactString::from)
.map(Box::<str>::from)
.map(Zeroizing::new)
}
fn from_line(line: &str) -> Option<&str> {