Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapted the CLI consuming the HTTP/JSON network API #1178

Merged
merged 11 commits into from
May 8, 2024
186 changes: 180 additions & 6 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust/agama-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ tokio = { version = "1.33.0", features = ["macros", "rt-multi-thread"] }
async-trait = "0.1.77"
reqwest = { version = "0.11", features = ["json"] }
home = "0.5.9"
rpassword = "7.3.1"

[[bin]]
name = "agama"
Expand Down
27 changes: 20 additions & 7 deletions rust/agama-cli/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};

const DEFAULT_JWT_FILE: &str = ".agama/agama-jwt";
const DEFAULT_AGAMA_TOKEN_FILE: &str = "/run/agama/token";
const DEFAULT_AUTH_URL: &str = "http://localhost:3000/api/auth";
const DEFAULT_FILE_MODE: u32 = 0o600;

Expand All @@ -33,8 +34,19 @@ pub async fn run(subcommand: AuthCommands) -> anyhow::Result<()> {
}
}

/// Returns the stored Agama token.
pub fn agama_token() -> anyhow::Result<String> {
if let Some(file) = agama_token_file() {
if let Ok(token) = read_line_from_file(file.as_path()) {
return Ok(token);
}
}

Err(anyhow::anyhow!("Authentication token not available"))
}

/// Reads stored token and returns it
fn jwt() -> anyhow::Result<String> {
pub fn jwt() -> anyhow::Result<String> {
if let Some(file) = jwt_file() {
if let Ok(token) = read_line_from_file(file.as_path()) {
return Ok(token);
Expand Down Expand Up @@ -109,6 +121,10 @@ impl Credentials for MissingCredentials {
fn jwt_file() -> Option<PathBuf> {
Some(home::home_dir()?.join(DEFAULT_JWT_FILE))
}
/// Path to agama-live token file.
fn agama_token_file() -> Option<PathBuf> {
home::home_dir().map(|p| p.join(DEFAULT_AGAMA_TOKEN_FILE))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is nothing like a default Agama token file. Actually, the serve token allows to pass a path through the --generate-token option.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could ignore it then, I just followed the PBI suggestions which mentioned the /run/agama/token file

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the live media, I guess we will inject that token into root's home directory. Or we can decide that we are going to use always that file (we could even have a AGAMA_RUN_DIR environment variable).

}

/// Reads first line from given file
fn read_line_from_file(path: &Path) -> io::Result<String> {
Expand Down Expand Up @@ -137,12 +153,9 @@ fn read_line_from_file(path: &Path) -> io::Result<String> {

/// Asks user to provide a line of input. Displays a prompt.
fn read_credential(caption: String) -> io::Result<String> {
let mut cred = String::new();

println!("{}: ", caption);

io::stdin().read_line(&mut cred)?;
if cred.pop().is_none() || cred.is_empty() {
let caption = format!("{}: ", caption);
let cred = rpassword::prompt_password(caption.clone()).unwrap();
if cred.is_empty() {
return Err(io::Error::new(
io::ErrorKind::Other,
format!("Failed to read {}", caption),
Expand Down
Loading
Loading