]> git.ipfire.org Git - pakfire.git/commitdiff
cli: Add a command to test user authentication against the API
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 21 Jun 2025 11:48:00 +0000 (11:48 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 21 Jun 2025 11:48:00 +0000 (11:48 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/cli/lib/auth.c [new file with mode: 0644]
src/cli/lib/auth.h [new file with mode: 0644]
src/cli/pakfire-client.c

index e484b4f73d7cd3f1dd9939a40d6905ad859ac35e..36c22e029d6cc3a45282d54867e05535b806950b 100644 (file)
@@ -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 (file)
index 0000000..031a091
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <pakfire/buildservice.h>
+
+#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 (file)
index 0000000..b059bd0
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef PAKFIRE_CLI_AUTH_H
+#define PAKFIRE_CLI_AUTH_H
+
+int cli_auth(void* data, int argc, char* argv[]);
+
+#endif /* PAKFIRE_CLI_AUTH_H */
index ec848d84e7f80efb5a09947db1f73cd10191a673..9899fff200b07861b237dfc45796a84fa6133b19 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <pakfire/ctx.h>
 
+#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 ...";