hasl/hasl

Add the EXTERNAL mechanism

15 months ago, Gary Kramlich
cc961ca359d4
Parents 0479a46f9e76
Children eb3ed23e2731
Add the EXTERNAL mechanism
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/hasl/haslmechanismexternal.c Tue Feb 14 07:02:21 2023 -0600
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2023 Hasl Developers
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include "haslmechanismexternal.h"
+
+#include "haslcore.h"
+
+struct _HaslMechanismExternal {
+ HaslMechanism parent;
+};
+
+G_DEFINE_TYPE(HaslMechanismExternal, hasl_mechanism_external,
+ HASL_TYPE_MECHANISM)
+
+/******************************************************************************
+ * HaslMechanism Implementation
+ *****************************************************************************/
+HaslMechanismResult
+hasl_mechanism_external_step(G_GNUC_UNUSED HaslMechanism *mechanism,
+ HaslContext *ctx,
+ G_GNUC_UNUSED const guint8 *server_in,
+ G_GNUC_UNUSED gsize server_in_length,
+ guint8 **client_out,
+ gsize *client_out_length,
+ G_GNUC_UNUSED GError **error)
+{
+ const char *authzid = NULL;
+ gsize length = 0;
+
+ authzid = hasl_context_get_authzid(ctx);
+ if(authzid != NULL && authzid[0] != '\0') {
+ length = strlen(authzid);
+ *client_out = (guint8 *)g_strdup(authzid);
+ }
+
+ if(client_out_length != NULL) {
+ *client_out_length = length;
+ }
+
+ return HASL_MECHANISM_RESULT_SUCCESS;
+}
+
+/******************************************************************************
+ * GObject Implementation
+ *****************************************************************************/
+static void
+hasl_mechanism_external_init(G_GNUC_UNUSED HaslMechanismExternal *external) {
+}
+
+static void
+hasl_mechanism_external_class_init(HaslMechanismExternalClass *klass) {
+ HaslMechanismClass *mechanism_class = HASL_MECHANISM_CLASS(klass);
+
+ mechanism_class->step = hasl_mechanism_external_step;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/hasl/haslmechanismexternal.h Tue Feb 14 07:02:21 2023 -0600
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2023 Hasl Developers
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef HASL_MECHANISM_EXTERNAL_H
+#define HASL_MECHANISM_EXTERNAL_H
+
+#include <glib.h>
+#include <glib-object.h>
+
+#include <hasl/haslcontext.h>
+#include <hasl/haslmechanism.h>
+
+G_BEGIN_DECLS
+
+#define HASL_TYPE_MECHANISM_EXTERNAL (hasl_mechanism_external_get_type())
+G_DECLARE_FINAL_TYPE(HaslMechanismExternal, hasl_mechanism_external, HASL,
+ MECHANISM_EXTERNAL, HaslMechanism)
+
+G_END_DECLS
+
+#endif /* HASL_MECHANISM_EXTERNAL_H */
+
--- a/hasl/meson.build Tue Feb 14 04:52:16 2023 -0600
+++ b/hasl/meson.build Tue Feb 14 07:02:21 2023 -0600
@@ -1,6 +1,7 @@
HASL_SOURCES = [
'haslcontext.c',
'haslmechanism.c',
+ 'haslmechanismexternal.c',
'haslmechanismplain.c',
]
@@ -8,6 +9,7 @@
'haslcontext.h',
'haslcore.h',
'haslmechanism.h',
+ 'haslmechanismexternal.h',
'haslmechanismplain.h',
]
--- a/hasl/tests/meson.build Tue Feb 14 04:52:16 2023 -0600
+++ b/hasl/tests/meson.build Tue Feb 14 07:02:21 2023 -0600
@@ -1,6 +1,7 @@
TESTS = [
'context',
'mechanism',
+ 'mechanism-external',
'mechanism-plain',
]
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/hasl/tests/test-mechanism-external.c Tue Feb 14 07:02:21 2023 -0600
@@ -0,0 +1,117 @@
+/*
+ * Copyright (C) 2023 Hasl Developers
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <glib.h>
+
+#include <hasl.h>
+
+/******************************************************************************
+ * Helpers
+ *****************************************************************************/
+static void
+test_hasl_mechanism_external_helper(HaslContext *context,
+ const char *expected)
+{
+ HaslMechanism *mechanism = NULL;
+ HaslMechanismResult result = 0;
+ GError *error = NULL;
+ guint8 *client_out = NULL;
+ gsize client_out_length = 0;
+ gsize expected_length = 0;
+
+ if(expected != NULL) {
+ expected_length = strlen(expected);
+ }
+
+ mechanism = g_object_new(HASL_TYPE_MECHANISM_EXTERNAL, NULL);
+
+ result = hasl_mechanism_step(mechanism, context, NULL, 0, &client_out,
+ &client_out_length, &error);
+
+ g_assert_no_error(error);
+ g_assert_cmpint(result, ==, HASL_MECHANISM_RESULT_SUCCESS);
+
+ g_assert_cmpmem(client_out, client_out_length, expected, expected_length);
+
+ g_free(client_out);
+ g_clear_object(&mechanism);
+}
+
+/******************************************************************************
+ * Tests
+ *****************************************************************************/
+static void
+test_hasl_mechanism_external_new(void) {
+ HaslMechanism *mechanism = NULL;
+
+ mechanism = g_object_new(HASL_TYPE_MECHANISM_EXTERNAL, NULL);
+
+ g_assert_true(HASL_IS_MECHANISM_EXTERNAL(mechanism));
+
+ g_clear_object(&mechanism);
+}
+
+static void
+test_hasl_mechanism_external_authzid_null(void) {
+ HaslContext *context = hasl_context_new();
+
+ test_hasl_mechanism_external_helper(context, NULL);
+
+ g_clear_object(&context);
+}
+
+static void
+test_hasl_mechanism_external_authzid_empty(void) {
+ HaslContext *context = hasl_context_new();
+
+ hasl_context_set_authzid(context, "");
+
+ test_hasl_mechanism_external_helper(context, "");
+
+ g_clear_object(&context);
+}
+
+static void
+test_hasl_mechanism_external_authzid_value(void) {
+ HaslContext *context = hasl_context_new();
+
+ hasl_context_set_authzid(context, "pidgy");
+
+ test_hasl_mechanism_external_helper(context, "pidgy");
+
+ g_clear_object(&context);
+}
+
+/******************************************************************************
+ * Main
+ *****************************************************************************/
+int
+main(int argc, char *argv[]) {
+ g_test_init(&argc, &argv, NULL);
+
+ g_test_add_func("/hasl/mechanism-external/new",
+ test_hasl_mechanism_external_new);
+
+ g_test_add_func("/hasl/mechanism-external/authzid/null",
+ test_hasl_mechanism_external_authzid_null);
+ g_test_add_func("/hasl/mechanism-external/authzid/empty",
+ test_hasl_mechanism_external_authzid_empty);
+ g_test_add_func("/hasl/mechanism-external/authzid/value",
+ test_hasl_mechanism_external_authzid_value);
+
+ return g_test_run();
+}