From: Michael Tremer Date: Sat, 21 Jun 2025 11:48:00 +0000 (+0000) Subject: cli: Add a command to test user authentication against the API X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3558ece3fd430d55bcb7aa719ad58530d19387eb;p=pakfire.git cli: Add a command to test user authentication against the API Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index e484b4f7..36c22e02 100644 --- a/Makefile.am +++ b/Makefile.am @@ -486,6 +486,8 @@ noinst_LTLIBRARIES += \ libcli_la_SOURCES = \ src/cli/lib/assert.c \ src/cli/lib/assert.h \ + src/cli/lib/auth.c \ + src/cli/lib/auth.h \ src/cli/lib/build.c \ src/cli/lib/build.h \ src/cli/lib/check.c \ diff --git a/src/cli/lib/auth.c b/src/cli/lib/auth.c new file mode 100644 index 00000000..031a0913 --- /dev/null +++ b/src/cli/lib/auth.c @@ -0,0 +1,84 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2023 Pakfire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#include + +#include "auth.h" +#include "command.h" +#include "pakfire.h" + +static const char* args_doc = "USERNAME [OPTIONS...]"; + +static const char* doc = "Authenticate a user against the build service"; + +struct cli_local_args { + const char* username; + const char* password; +}; + +static error_t parse(int key, char* arg, struct argp_state* state, void* data) { + struct cli_local_args* args = data; + + switch (key) { + case ARGP_KEY_ARG: + if (!args->username) + args->username = arg; + + else if (!args->password) + args->password = arg; + + else + argp_usage(state); + break; + + default: + return ARGP_ERR_UNKNOWN; + } + + return 0; +} + +int cli_auth(void* data, int argc, char* argv[]) { + struct cli_global_args* global_args = data; + struct cli_local_args local_args = {}; + struct pakfire_buildservice* service = NULL; + int r; + + // Parse the command line + r = cli_parse(NULL, NULL, args_doc, doc, parse, 0, argc, argv, &local_args); + if (r) + goto ERROR; + + // Connect to the build service + r = cli_setup_buildservice(&service, global_args); + if (r < 0) + goto ERROR; + + // Authenticate + r = pakfire_buildservice_auth_user(service, local_args.username, local_args.password); + if (r < 0) + goto ERROR; + +ERROR: + if (service) + pakfire_buildservice_unref(service); + + return r; +} diff --git a/src/cli/lib/auth.h b/src/cli/lib/auth.h new file mode 100644 index 00000000..b059bd08 --- /dev/null +++ b/src/cli/lib/auth.h @@ -0,0 +1,26 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2025 Pakfire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#ifndef PAKFIRE_CLI_AUTH_H +#define PAKFIRE_CLI_AUTH_H + +int cli_auth(void* data, int argc, char* argv[]); + +#endif /* PAKFIRE_CLI_AUTH_H */ diff --git a/src/cli/pakfire-client.c b/src/cli/pakfire-client.c index ec848d84..9899fff2 100644 --- a/src/cli/pakfire-client.c +++ b/src/cli/pakfire-client.c @@ -23,6 +23,7 @@ #include +#include "lib/auth.h" #include "lib/client-build.h" #include "lib/command.h" #include "lib/config.h" @@ -34,6 +35,7 @@ const char* argp_program_version = PACKAGE_FULLVERSION; static const struct command commands[] = { + { "auth", cli_auth, 2, 2, 0 }, { "build", cli_client_build, 1, -1, 0 }, { "repo", cli_repo_client, -1, -1, 0 }, { "upload", cli_upload, -1, -1, 0 }, @@ -41,6 +43,7 @@ static const struct command commands[] = { }; static const char* args_doc = + "auth ...\n" "repo ...\n" "upload ...";