diff options
| author | sigoden <sigoden@gmail.com> | 2023-03-22 21:02:09 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-22 21:02:09 +0800 |
| commit | f257931b0b7065bfc123f13d30047f8973a7f655 (patch) | |
| tree | c9e40facb3b10a87f5a5ca78b6b2d184e13573f4 | |
| parent | c36eab543377c52e230d5be08d675b330f59d566 (diff) | |
| download | aichat-f257931b0b7065bfc123f13d30047f8973a7f655.tar.gz | |
fix: mask api_key/organization_id in info text (#93)
| -rw-r--r-- | src/config/mod.rs | 7 | ||||
| -rw-r--r-- | src/utils/mod.rs | 20 |
2 files changed, 25 insertions, 2 deletions
diff --git a/src/config/mod.rs b/src/config/mod.rs index 8b6a6ac..19f21a0 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -7,7 +7,7 @@ use self::message::Message; use self::role::Role; use crate::config::message::num_tokens_from_messages; -use crate::utils::now; +use crate::utils::{mask_text, now}; use anyhow::{anyhow, bail, Context, Result}; use inquire::{Confirm, Text}; @@ -333,7 +333,10 @@ impl Config { .map(|v| v.to_string()) .unwrap_or("-".into()); let (api_key, organization_id) = self.get_api_key(); - let organization_id = organization_id.unwrap_or("-".into()); + let api_key = mask_text(&api_key, 3, 4); + let organization_id = organization_id + .map(|v| mask_text(&v, 3, 4)) + .unwrap_or("-".into()); let items = vec![ ("config_file", file_info(&Config::config_file()?)), ("roles_file", file_info(&Config::roles_file()?)), diff --git a/src/utils/mod.rs b/src/utils/mod.rs index fd178ba..64fc01c 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -27,3 +27,23 @@ pub fn now() -> String { pub fn emphasis(text: &str) -> String { text.stylize().with(Color::White).to_string() } + +pub fn mask_text(text: &str, head: usize, tail: usize) -> String { + if text.len() <= head + tail { + return text.to_string(); + } + format!("{}...{}", &text[0..head], &text[text.len() - tail..]) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_mask_text() { + assert_eq!(mask_text("123456", 3, 4), "123456"); + assert_eq!(mask_text("1234567", 3, 4), "1234567"); + assert_eq!(mask_text("12345678", 3, 4), "123...5678"); + assert_eq!(mask_text("12345678", 4, 3), "1234...678"); + } +} |
