Skip to content

Commit 6c0fb4c

Browse files
committed
Cache access tokens instead of user passwords
1 parent a861f14 commit 6c0fb4c

3 files changed

Lines changed: 23 additions & 13 deletions

File tree

src/command/admin.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
1-
use crate::{rpc, Result};
1+
use crate::{rpc, settings, Result};
22
use clap::Args;
33
use serde_json::json;
44

5+
#[derive(Args)]
6+
pub struct LoginArgs {
7+
pub username: String,
8+
pub password: String,
9+
pub token_label: String,
10+
}
11+
12+
pub fn login(args: &LoginArgs) -> Result<()> {
13+
let res = rpc::call(
14+
"create_auth_token",
15+
json!({"username": args.username, "password": args.password, "token_label": args.token_label}),
16+
)?;
17+
let res = res.result.unwrap();
18+
let token = res["token"].as_str().unwrap();
19+
settings::put_str("password", token)?;
20+
println!("You are now logged in as {}", args.username);
21+
Ok(())
22+
}
23+
524
#[derive(Args)]
625
pub struct AddAdminArgs {
726
pub new_admin_name: String,

src/command/setup.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,6 @@ pub fn set_server(args: &SetServerArgs) -> Result<()> {
2222
Ok(())
2323
}
2424

25-
#[derive(Args)]
26-
pub struct LoginArgs {
27-
pub password: String,
28-
}
29-
30-
pub fn login(args: &LoginArgs) -> Result<()> {
31-
settings::put_str("password", &args.password)
32-
}
33-
3425
#[derive(Args)]
3526
pub struct StateArgs {}
3627

src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ struct Cli {
2323
enum Commands {
2424
/// Set a JSON RPC API URL
2525
SetServer(command::setup::SetServerArgs),
26-
/// Try to login and save password for future calls, if successful
27-
Login(command::setup::LoginArgs),
2826
/// Show all locally cached data
2927
State(command::setup::StateArgs),
3028

@@ -57,6 +55,8 @@ enum Commands {
5755
/// Generate category tags for a specific element id range
5856
GenerateElementCategories(GenerateElementCategoriesArgs),
5957

58+
/// Login with your username and password and get an auth token
59+
Login(command::admin::LoginArgs),
6060
/// Create a new admin user. New admins have no permissions by default, use add-admin-action to allow certain acitons
6161
AddAdmin(command::admin::AddAdminArgs),
6262
/// Change admin password. Knowledge of an old password is required
@@ -110,7 +110,7 @@ fn main() -> Result<()> {
110110
}
111111

112112
if let Some(Commands::Login(args)) = &cli.command {
113-
return command::setup::login(args);
113+
return command::admin::login(args);
114114
}
115115

116116
if let Some(Commands::State(args)) = &cli.command {

0 commit comments

Comments
 (0)