Skip to content

Commit

Permalink
Merge pull request #13 from gierens/feat-9-cli-password-auth
Browse files Browse the repository at this point in the history
Add Password Authentication to CLI

This adds password authentication to as alternative to API key authentication
to the CLI program. It also adds corresponding documentation in the README.

It furthermore adds Debug derivation on all parser and (sub)command structs
and enums, and the authentication-strategy list command.

Resolves #9
  • Loading branch information
gierens authored Dec 4, 2023
2 parents 89762ba + 35078ac commit 530ee75
Show file tree
Hide file tree
Showing 14 changed files with 144 additions and 22 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,17 @@ environment variables. For the latter option use something like this:
export WIKI_JS_BASE_URL=https://wiki.mydomain.com
export WIKI_JS_API_KEY=MY-SUPER-SECRET-API-KEY
```
Then you can for example create a page named `test`, list pages and edit
it with:
or for password authentication use:
```bash
export WIKI_JS_USERNAME="USERNAME or EMAIL"
export WIKI_JS_PASSWORD="PASSWORD"
```
Note, in case you're using something else then the default authentication
provider like LDAP, you have to set the variable `WIKI_JS_AUTH_PROVIDER` to
its UUID.

Then you can for example create a page named `test`, list pages and edit it
with:
```bash
wikijs page create test
wikijs page list
Expand Down
2 changes: 1 addition & 1 deletion src/cli/analytics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clap::Subcommand;
use std::error::Error;
use tabled::{builder::Builder, settings::Style};

#[derive(Subcommand)]
#[derive(Subcommand, Debug)]
pub(crate) enum AnalyticsProviderCommand {
#[clap(about = "List analytics providers")]
List {},
Expand Down
4 changes: 2 additions & 2 deletions src/cli/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::fs::File;
use std::io::Write;
use tabled::{builder::Builder, settings::Style};

#[derive(Subcommand)]
#[derive(Subcommand, Debug)]
pub(crate) enum AssetCommand {
#[clap(about = "List assets")]
List {
Expand Down Expand Up @@ -36,7 +36,7 @@ pub(crate) enum AssetCommand {
},
}

#[derive(Subcommand)]
#[derive(Subcommand, Debug)]
pub(crate) enum AssetFolderCommand {
#[clap(about = "List asset folders")]
List {
Expand Down
61 changes: 61 additions & 0 deletions src/cli/authentication.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use crate::common::Execute;
use clap::Subcommand;
use std::error::Error;
use tabled::{builder::Builder, settings::Style};

#[derive(Subcommand, Debug)]
pub(crate) enum AuthenticationStrategyCommand {
#[clap(about = "List authentication strategies")]
List {},
}

impl Execute for AuthenticationStrategyCommand {
fn execute(&self, api: wikijs::Api) -> Result<(), Box<dyn Error>> {
match self {
AuthenticationStrategyCommand::List {} => {
authentication_strategy_list(api)
}
}
}
}

fn authentication_strategy_list(
api: wikijs::Api,
) -> Result<(), Box<dyn Error>> {
let providers = api.authentication_strategy_list()?;
let mut builder = Builder::new();
builder.push_record([
"key",
// "props",
"title",
// "description",
"is_available",
// "use_form",
// "username_type",
// "logo",
// "color",
// "website",
// "icon",
]);
for provider in providers {
builder.push_record([
provider.key.as_str(),
// provider.props.as_str(),
provider.title.as_str(),
// provider.description.as_str(),
match provider.is_available {
Some(true) => "true",
Some(false) => "false",
None => "",
},
// provider.use_form.to_string().as_str(),
// provider.username_type.as_str(),
// provider.logo.as_str(),
// provider.color.as_str(),
// provider.website.as_str(),
// provider.icon.as_str(),
]);
}
println!("{}", builder.build().with(Style::rounded()));
Ok(())
}
2 changes: 1 addition & 1 deletion src/cli/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clap::Subcommand;
use std::error::Error;
use tabled::{builder::Builder, settings::Style};

#[derive(Subcommand)]
#[derive(Subcommand, Debug)]
pub(crate) enum CommentCommand {
#[clap(about = "List comments")]
List {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/contribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clap::Subcommand;
use std::error::Error;
use tabled::{builder::Builder, settings::Style};

#[derive(Subcommand)]
#[derive(Subcommand, Debug)]
pub(crate) enum ContributorCommand {
#[clap(about = "List contributors")]
List {},
Expand Down
2 changes: 1 addition & 1 deletion src/cli/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clap::Subcommand;
use std::error::Error;
use tabled::{builder::Builder, settings::Style};

#[derive(Subcommand)]
#[derive(Subcommand, Debug)]
pub(crate) enum GroupCommand {
#[clap(about = "List groups")]
List {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/localization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clap::Subcommand;
use std::error::Error;
use tabled::{builder::Builder, settings::Style};

#[derive(Subcommand)]
#[derive(Subcommand, Debug)]
pub(crate) enum LocaleCommand {
#[clap(about = "List locales")]
List,
Expand Down
2 changes: 1 addition & 1 deletion src/cli/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clap::Subcommand;
use std::error::Error;
use tabled::{builder::Builder, settings::Style};

#[derive(Subcommand)]
#[derive(Subcommand, Debug)]
pub(crate) enum LoggerCommand {
#[clap(about = "List loggers")]
List {
Expand Down
64 changes: 58 additions & 6 deletions src/cli/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use clap::{Parser, Subcommand};
use clap::{Args, Parser, Subcommand};
use colored::Colorize;
use wikijs::{Api, Credentials};

mod analytics;
mod asset;
mod authentication;
mod comment;
mod common;
mod contribute;
Expand All @@ -17,7 +18,43 @@ mod user;

use crate::common::Execute;

#[derive(Parser)]
#[derive(Args, Debug)]
#[group(required = true, multiple = true)]
struct CredentialArgs {
#[clap(short, long, help = "Wiki.js API key", env = "WIKI_JS_API_KEY")]
key: Option<String>,

#[clap(
short = 'U',
long,
help = "Wiki.js username",
env = "WIKI_JS_USERNAME",
requires = "password",
conflicts_with = "key"
)]
username: Option<String>,

#[clap(
short = 'P',
long,
help = "Wiki.js password",
env = "WIKI_JS_PASSWORD",
requires = "username",
conflicts_with = "key"
)]
password: Option<String>,

#[clap(
short,
long,
help = "Wiki.js authentication provider ID",
env = "WIKI_JS_AUTH_PROVIDER",
default_value = "local"
)]
provider: Option<String>,
}

#[derive(Parser, Debug)]
#[command(name = "wikijs-cli")]
#[command(author = "Sandro-Alessio Gierens <sandro@gierens.de>")]
#[command(version = "0.1.1")]
Expand All @@ -26,14 +63,14 @@ struct Cli {
#[clap(short, long, help = "Wiki.js base URL", env = "WIKI_JS_BASE_URL")]
url: String,

#[clap(short, long, help = "Wiki.js API key", env = "WIKI_JS_API_KEY")]
key: String,
#[clap(flatten)]
credentials: CredentialArgs,

#[clap(subcommand)]
command: Command,
}

#[derive(Subcommand)]
#[derive(Subcommand, Debug)]
enum Command {
#[clap(about = "Asset commands")]
Asset {
Expand All @@ -47,6 +84,12 @@ enum Command {
command: asset::AssetFolderCommand,
},

#[clap(about = "Authentication strategy commands")]
AuthenticationStrategy {
#[clap(subcommand)]
command: authentication::AuthenticationStrategyCommand,
},

#[clap(about = "Page commands")]
Page {
#[clap(subcommand)]
Expand Down Expand Up @@ -122,7 +165,15 @@ enum Command {

fn main() {
let cli = Cli::parse();
let credentials = Credentials::Key(cli.key.clone());
let credentials = match cli.credentials.key {
Some(key) => Credentials::Key(key),
None => {
let username = cli.credentials.username.unwrap();
let password = cli.credentials.password.unwrap();
let provider = cli.credentials.provider.unwrap();
Credentials::UsernamePassword(username, password, provider)
}
};
let api = Api::new(cli.url.clone(), credentials).unwrap_or_else(|e| {
eprintln!("{}: {}", "error".bold().red(), e);
std::process::exit(1);
Expand All @@ -134,6 +185,7 @@ fn main() {
match match cli.command {
Command::Asset { ref command } => command.execute(api),
Command::AssetFolder { ref command } => command.execute(api),
Command::AuthenticationStrategy { ref command } => command.execute(api),
Command::Page { ref command } => command.execute(api),
Command::Contributor { ref command } => command.execute(api),
Command::AnalyticsProvider { command } => command.execute(api),
Expand Down
2 changes: 1 addition & 1 deletion src/cli/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::io::Write;
use tabled::{builder::Builder, settings::Style};
use tempfile::Builder as TempFileBuilder;

#[derive(Subcommand)]
#[derive(Subcommand, Debug)]
pub(crate) enum PageCommand {
#[clap(about = "Get a page")]
Get {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clap::Subcommand;
use std::error::Error;
use tabled::{builder::Builder, settings::Style};

#[derive(Subcommand)]
#[derive(Subcommand, Debug)]
pub(crate) enum SystemFlagCommand {
#[clap(about = "List system flags")]
List {},
Expand Down
2 changes: 1 addition & 1 deletion src/cli/theming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clap::Subcommand;
use std::error::Error;
use tabled::{builder::Builder, settings::Style};

#[derive(Subcommand)]
#[derive(Subcommand, Debug)]
pub(crate) enum ThemeCommand {
#[clap(about = "List themes")]
List {},
Expand Down
6 changes: 3 additions & 3 deletions src/cli/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use colored::Colorize;
use std::error::Error;
use tabled::{builder::Builder, settings::Style};

#[derive(Subcommand)]
#[derive(Subcommand, Debug)]
pub(crate) enum UserCommand {
#[clap(about = "Get a user")]
Get {
Expand Down Expand Up @@ -148,7 +148,7 @@ pub(crate) enum UserCommand {
},
}

#[derive(Subcommand)]
#[derive(Subcommand, Debug)]
pub(crate) enum ProfileCommand {
#[clap(about = "Get your own user profile")]
Get {},
Expand All @@ -175,7 +175,7 @@ pub(crate) enum ProfileCommand {
},
}

#[derive(Subcommand)]
#[derive(Subcommand, Debug)]
pub(crate) enum PasswordCommand {
#[clap(about = "Change your password")]
Change {
Expand Down

0 comments on commit 530ee75

Please sign in to comment.