Skip to content

Commit 248f122

Browse files
committed
Add create-api-key command
1 parent 245ba46 commit 248f122

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/command/admin.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,36 @@ use serde_json::json;
66
pub struct LoginArgs {
77
pub username: String,
88
pub password: String,
9-
pub token_label: String,
9+
pub label: String,
1010
}
1111

1212
pub fn login(args: &LoginArgs) -> Result<()> {
1313
let res = rpc::call(
14-
"create_auth_token",
15-
json!({"username": args.username, "password": args.password, "token_label": args.token_label}),
14+
"create_api_key",
15+
json!({"username": args.username, "password": args.password, "label": args.label}),
1616
)?;
1717
let res = res.result.unwrap();
18-
let token = res["token"].as_str().unwrap();
19-
settings::put_str("password", token)?;
18+
let api_key = res["api_key"].as_str().unwrap();
19+
settings::put_str("password", api_key)?;
2020
println!("You are now logged in as {}", args.username);
2121
Ok(())
2222
}
2323

24+
#[derive(Args)]
25+
pub struct CreateApiKeyArgs {
26+
pub username: String,
27+
pub password: String,
28+
pub label: String,
29+
}
30+
31+
pub fn create_api_key(args: &CreateApiKeyArgs) -> Result<()> {
32+
rpc::call(
33+
"create_api_key",
34+
json!({"username": args.username, "password": args.password, "label": args.label}),
35+
)?
36+
.print()
37+
}
38+
2439
#[derive(Args)]
2540
pub struct AddAdminArgs {
2641
pub new_admin_name: String,

src/main.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ enum Commands {
5858
// Auth - https://github.com/teambtcmap/btcmap-api/blob/master/docs/rpc-api/auth.md
5959
/// Change admin password. Knowledge of an old password is required
6060
ChangePassword(command::admin::ChangePasswordArgs),
61+
/// Create API key. You need to provide your username and password, as well as a key label
62+
CreateApiKey(command::admin::CreateApiKeyArgs),
6163
/// Login with your username and password and get an auth token
6264
Login(command::admin::LoginArgs),
6365
/// Create a new admin user. New admins have no permissions by default, use add-admin-action to allow certain acitons
@@ -118,6 +120,10 @@ fn main() -> Result<()> {
118120
return command::admin::login(args);
119121
}
120122

123+
if let Some(Commands::CreateApiKey(args)) = &cli.command {
124+
return command::admin::create_api_key(args);
125+
}
126+
121127
if let Some(Commands::State(args)) = &cli.command {
122128
return command::setup::state(args);
123129
}
@@ -135,6 +141,7 @@ fn main() -> Result<()> {
135141
// Setup
136142
Commands::SetServer(_) => Err("supposed to be unreachable".into()),
137143
Commands::Login(_) => Err("supposed to be unreachable".into()),
144+
Commands::CreateApiKey(_) => Err("supposed to be unreachable".into()),
138145
Commands::State(_) => Err("supposed to be unreachable".into()),
139146
Commands::ChangePassword(_) => Err("supposed to be unreachable".into()),
140147
// Element

0 commit comments

Comments
 (0)