-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75deeeb
commit 7c30980
Showing
5 changed files
with
136 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ default-members = [".", "artiaa_auth"] | |
[package] | ||
name = "foreman" | ||
description = "Toolchain manager for simple binary tools" | ||
version = "1.5.0" | ||
version = "1.6.0" | ||
authors = [ | ||
"Lucien Greathouse <[email protected]>", | ||
"Matt Hargett <[email protected]>", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use crate::error::ForemanError; | ||
use crate::{error::ForemanResult, fs}; | ||
use artiaa_auth::{error::ArtifactoryAuthError, Credentials}; | ||
use serde::{Deserialize, Serialize}; | ||
use std::collections::HashMap; | ||
|
||
use std::{ | ||
ops::{Deref, DerefMut}, | ||
path::Path, | ||
}; | ||
/// Contains stored user tokens that Foreman can use to download tools. | ||
#[derive(Debug, Default, Serialize, Deserialize)] | ||
pub struct ArtifactoryAuthStore { | ||
tokens: HashMap<String, Credentials>, | ||
} | ||
|
||
impl Deref for ArtifactoryAuthStore { | ||
type Target = HashMap<String, Credentials>; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.tokens | ||
} | ||
} | ||
|
||
impl DerefMut for ArtifactoryAuthStore { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.tokens | ||
} | ||
} | ||
|
||
impl ArtifactoryAuthStore { | ||
pub fn set_token(auth_file: &Path, key: &str, token: &str) -> ForemanResult<()> { | ||
let contents = fs::try_read_to_string(auth_file)?; | ||
|
||
let mut store: ArtifactoryAuthStore = if let Some(contents) = contents { | ||
serde_json::from_str(&contents).map_err(|err: serde_json::Error| { | ||
ForemanError::ArtiAAError { | ||
error: ArtifactoryAuthError::auth_parsing(auth_file, err.to_string()), | ||
} | ||
})? | ||
} else { | ||
ArtifactoryAuthStore::default() | ||
}; | ||
|
||
store.insert( | ||
key.to_owned(), | ||
Credentials { | ||
username: "".to_owned(), | ||
token: token.to_owned(), | ||
}, | ||
); | ||
|
||
let serialized = | ||
serde_json::to_string_pretty(&store).map_err(|err: serde_json::Error| { | ||
ForemanError::ArtiAAError { | ||
error: ArtifactoryAuthError::auth_parsing(auth_file, err.to_string()), | ||
} | ||
})?; | ||
|
||
if let Some(dir) = auth_file.parent() { | ||
fs::create_dir_all(dir)?; | ||
fs::write(auth_file, serialized) | ||
} else { | ||
Err(ForemanError::ArtiAAError { | ||
error: ArtifactoryAuthError::auth_parsing( | ||
auth_file, | ||
"Could not find parent directory of auth file".to_owned(), | ||
), | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters