Added key-based authentication
This commit is contained in:
@@ -7,6 +7,24 @@ use std::path::PathBuf;
|
||||
pub struct Endpoint {
|
||||
pub name: String,
|
||||
pub url: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub private_key: Option<String>,
|
||||
}
|
||||
|
||||
impl Endpoint {
|
||||
/// Creates a new endpoint with a freshly generated Ed25519 key pair.
|
||||
pub fn new(name: String, url: String) -> Self {
|
||||
Self {
|
||||
name,
|
||||
url,
|
||||
private_key: Some(crate::identity::Identity::generate().to_b64()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn identity(&self) -> Option<crate::identity::Identity> {
|
||||
self.private_key.as_deref()
|
||||
.and_then(|k| crate::identity::Identity::from_b64(k).ok())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
@@ -36,9 +54,7 @@ fn default_selected_bg() -> String {
|
||||
}
|
||||
|
||||
fn default_theme() -> Theme {
|
||||
Theme {
|
||||
selected_bg: default_selected_bg(),
|
||||
}
|
||||
Theme { selected_bg: default_selected_bg() }
|
||||
}
|
||||
|
||||
pub fn config_path() -> PathBuf {
|
||||
@@ -61,6 +77,13 @@ impl Config {
|
||||
&self.active_endpoint
|
||||
}
|
||||
|
||||
/// Returns the identity for the currently active endpoint, if any.
|
||||
pub fn active_identity(&self) -> Option<crate::identity::Identity> {
|
||||
self.endpoints.iter()
|
||||
.find(|ep| ep.url == self.active_endpoint)
|
||||
.and_then(|ep| ep.identity())
|
||||
}
|
||||
|
||||
pub fn load() -> Result<Self> {
|
||||
let path = config_path();
|
||||
let content = fs::read_to_string(&path)?;
|
||||
@@ -69,10 +92,7 @@ impl Config {
|
||||
if cfg.endpoints.is_empty() {
|
||||
if let Some(url) = cfg.server_url.take() {
|
||||
cfg.active_endpoint = url.clone();
|
||||
cfg.endpoints.push(Endpoint {
|
||||
name: "Default".to_string(),
|
||||
url,
|
||||
});
|
||||
cfg.endpoints.push(Endpoint { name: "Default".to_string(), url, private_key: None });
|
||||
}
|
||||
}
|
||||
if cfg.active_endpoint.is_empty() {
|
||||
@@ -87,10 +107,7 @@ impl Config {
|
||||
let path = config_path();
|
||||
let config = Config {
|
||||
server_url: None,
|
||||
endpoints: vec![Endpoint {
|
||||
name: name.to_string(),
|
||||
url: url.to_string(),
|
||||
}],
|
||||
endpoints: vec![Endpoint::new(name.to_string(), url.to_string())],
|
||||
active_endpoint: url.to_string(),
|
||||
timeout_sec: 10,
|
||||
theme: default_theme(),
|
||||
@@ -127,6 +144,9 @@ impl Config {
|
||||
escape_toml(&ep.name),
|
||||
escape_toml(&ep.url),
|
||||
));
|
||||
if let Some(key) = &ep.private_key {
|
||||
s.push_str(&format!("private_key = \"{}\"\n", escape_toml(key)));
|
||||
}
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user