mirror of
https://github.com/chirpstack/chirpstack.git
synced 2025-02-01 08:17:58 +00:00
Add create-api-key sub-command.
This makes is possible to create a global API key (programmatically) using the CLI. Fixes https://github.com/chirpstack/chirpstack-rest-api/issues/12.
This commit is contained in:
parent
9d543603d5
commit
a975cf3223
25
chirpstack/src/cmd/create_api_key.rs
Normal file
25
chirpstack/src/cmd/create_api_key.rs
Normal file
@ -0,0 +1,25 @@
|
||||
use anyhow::Result;
|
||||
|
||||
use crate::api::auth::claims;
|
||||
use crate::config;
|
||||
use crate::storage::api_key;
|
||||
|
||||
pub async fn run(name: &str) -> Result<()> {
|
||||
let conf = config::get();
|
||||
|
||||
crate::storage::setup().await?;
|
||||
|
||||
let key = api_key::create(api_key::ApiKey {
|
||||
name: name.to_string(),
|
||||
is_admin: true,
|
||||
..Default::default()
|
||||
})
|
||||
.await?;
|
||||
|
||||
let token = claims::AuthClaim::new_for_api_key(&key.id).encode(conf.api.secret.as_ref())?;
|
||||
|
||||
println!("id: {}", key.id);
|
||||
println!("token: {}", token);
|
||||
|
||||
Ok(())
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
pub mod configfile;
|
||||
pub mod create_api_key;
|
||||
pub mod import_legacy_lorawan_devices_repository;
|
||||
pub mod print_ds;
|
||||
pub mod root;
|
||||
|
@ -14,7 +14,6 @@ extern crate diesel;
|
||||
extern crate anyhow;
|
||||
|
||||
use std::path::Path;
|
||||
use std::process;
|
||||
use std::str::FromStr;
|
||||
|
||||
use anyhow::Result;
|
||||
@ -78,6 +77,13 @@ enum Commands {
|
||||
#[arg(short, long, value_name = "DIR")]
|
||||
dir: String,
|
||||
},
|
||||
|
||||
/// Create global API key.
|
||||
CreateApiKey {
|
||||
/// Name.
|
||||
#[arg(short, long, value_name = "NAME")]
|
||||
name: String,
|
||||
},
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
@ -104,25 +110,20 @@ async fn main() -> Result<()> {
|
||||
.init();
|
||||
}
|
||||
|
||||
if let Some(Commands::Configfile {}) = &cli.command {
|
||||
cmd::configfile::run();
|
||||
process::exit(0);
|
||||
match &cli.command {
|
||||
Some(Commands::Configfile {}) => cmd::configfile::run(),
|
||||
Some(Commands::PrintDs { dev_eui }) => {
|
||||
let dev_eui = EUI64::from_str(dev_eui).unwrap();
|
||||
cmd::print_ds::run(&dev_eui).await.unwrap();
|
||||
}
|
||||
Some(Commands::ImportLegacyLorawanDevicesRepository { dir }) => {
|
||||
cmd::import_legacy_lorawan_devices_repository::run(Path::new(&dir))
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
Some(Commands::CreateApiKey { name }) => cmd::create_api_key::run(&name).await?,
|
||||
None => cmd::root::run().await?,
|
||||
}
|
||||
|
||||
if let Some(Commands::PrintDs { dev_eui }) = &cli.command {
|
||||
let dev_eui = EUI64::from_str(dev_eui).unwrap();
|
||||
cmd::print_ds::run(&dev_eui).await.unwrap();
|
||||
process::exit(0);
|
||||
}
|
||||
|
||||
if let Some(Commands::ImportLegacyLorawanDevicesRepository { dir }) = &cli.command {
|
||||
cmd::import_legacy_lorawan_devices_repository::run(Path::new(&dir))
|
||||
.await
|
||||
.unwrap();
|
||||
process::exit(0);
|
||||
}
|
||||
|
||||
cmd::root::run().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user