summaryrefslogtreecommitdiff
path: root/auth/examples
diff options
context:
space:
mode:
Diffstat (limited to 'auth/examples')
-rw-r--r--auth/examples/full_example.c66
-rw-r--r--auth/examples/full_example.js38
-rw-r--r--auth/examples/meson.build18
-rw-r--r--auth/examples/simple_example.c31
-rw-r--r--auth/examples/simple_example.js9
5 files changed, 162 insertions, 0 deletions
diff --git a/auth/examples/full_example.c b/auth/examples/full_example.c
new file mode 100644
index 0000000..a20c02b
--- /dev/null
+++ b/auth/examples/full_example.c
@@ -0,0 +1,66 @@
+#include <bsd/readpassphrase.h>
+
+#include "astal-auth.h"
+
+GMainLoop *loop;
+
+static void authenticate(AstalAuthPam *pam) {
+ if (!astal_auth_pam_start_authenticate(pam)) {
+ g_print("could not start authentication process\n");
+ g_object_unref(pam);
+ g_main_loop_quit(loop);
+ }
+}
+
+static void on_visible(AstalAuthPam *pam, const gchar *data) {
+ gchar passbuf[1024];
+ readpassphrase(data, passbuf, sizeof(passbuf), RPP_ECHO_ON);
+ astal_auth_pam_supply_secret(pam, passbuf);
+}
+
+static void on_hidden(AstalAuthPam *pam, const gchar *data) {
+ gchar passbuf[1024];
+ readpassphrase(data, passbuf, sizeof(passbuf), RPP_ECHO_OFF);
+ astal_auth_pam_supply_secret(pam, passbuf);
+}
+
+static void on_info(AstalAuthPam *pam, const gchar *data) {
+ g_print("info: %s\n", data);
+ astal_auth_pam_supply_secret(pam, NULL);
+}
+
+static void on_error(AstalAuthPam *pam, const gchar *data) {
+ g_print("error: %s\n", data);
+ astal_auth_pam_supply_secret(pam, NULL);
+}
+
+static void on_success(AstalAuthPam *pam) {
+ g_print("success\n");
+ g_object_unref(pam);
+ g_main_loop_quit(loop);
+}
+
+static void on_fail(AstalAuthPam *pam, const gchar *data) {
+ g_print("fail: %s\n", data);
+ authenticate(pam);
+}
+
+int main(void) {
+ GMainContext *loopctx = NULL;
+
+ loop = g_main_loop_new(loopctx, FALSE);
+
+ AstalAuthPam *pam = g_object_new(ASTAL_AUTH_TYPE_PAM, NULL);
+
+ g_signal_connect(pam, "auth-prompt-visible", G_CALLBACK(on_visible), NULL);
+ g_signal_connect(pam, "auth-prompt-hidden", G_CALLBACK(on_hidden), NULL);
+ g_signal_connect(pam, "auth-info", G_CALLBACK(on_info), NULL);
+ g_signal_connect(pam, "auth-error", G_CALLBACK(on_error), NULL);
+
+ g_signal_connect(pam, "success", G_CALLBACK(on_success), NULL);
+ g_signal_connect(pam, "fail", G_CALLBACK(on_fail), NULL);
+
+ authenticate(pam);
+
+ g_main_loop_run(loop);
+}
diff --git a/auth/examples/full_example.js b/auth/examples/full_example.js
new file mode 100644
index 0000000..7359784
--- /dev/null
+++ b/auth/examples/full_example.js
@@ -0,0 +1,38 @@
+#!/usr/bin/env -S gjs -m
+
+import Auth from "gi://AstalAuth";
+import GLib from "gi://GLib";
+
+const loop = GLib.MainLoop.new(null, false);
+
+const pam = new Auth.Pam();
+pam.connect("auth-prompt-visible", (p, msg) => {
+ print(msg);
+ p.supply_secret("");
+});
+pam.connect("auth-prompt-hidden", (p, msg) => {
+ print(msg);
+ p.supply_secret("password");
+});
+pam.connect("auth-info", (p, msg) => {
+ print(msg);
+ p.supply_secret("");
+});
+pam.connect("auth-error", (p, msg) => {
+ print(msg);
+ p.supply_secret("");
+});
+
+pam.connect("success", p => {
+ print("authentication sucessful");
+ loop.quit();
+});
+pam.connect("fail", (p, msg) => {
+ print(msg);
+ loop.quit();
+});
+
+pam.start_authenticate();
+
+loop.runAsync()
+
diff --git a/auth/examples/meson.build b/auth/examples/meson.build
new file mode 100644
index 0000000..cf23d3f
--- /dev/null
+++ b/auth/examples/meson.build
@@ -0,0 +1,18 @@
+
+deps_example = [
+ dependency('gobject-2.0'),
+ dependency('libbsd'),
+ libastal_auth
+]
+
+astal_auth_full_exmple = executable(
+ 'astal_auth_full_example',
+ files('full_example.c'),
+ dependencies : deps_example,
+ install : false)
+
+astal_auth_simple_example = executable(
+ 'astal_auth_simple_example',
+ files('simple_example.c'),
+ dependencies : deps_example,
+ install : false)
diff --git a/auth/examples/simple_example.c b/auth/examples/simple_example.c
new file mode 100644
index 0000000..d00bad2
--- /dev/null
+++ b/auth/examples/simple_example.c
@@ -0,0 +1,31 @@
+#include <bsd/readpassphrase.h>
+
+#include "astal-auth.h"
+
+GMainLoop *loop;
+
+void ready_callback(AstalAuthPam *pam, GAsyncResult *res, gpointer user_data) {
+ GError *error = NULL;
+ astal_auth_pam_authenticate_finish(res, &error);
+ if (error == NULL) {
+ g_print("success\n");
+ } else {
+ g_print("failure: %s\n", error->message);
+ g_error_free(error);
+ }
+
+ g_main_loop_quit(loop);
+}
+
+int main(void) {
+ GMainContext *loopctx = NULL;
+ loop = g_main_loop_new(loopctx, FALSE);
+
+ gchar *passbuf = calloc(1024, sizeof(gchar));
+ readpassphrase("Password: ", passbuf, 1024, RPP_ECHO_OFF);
+ astal_auth_pam_authenticate(passbuf, (GAsyncReadyCallback)ready_callback, NULL);
+ g_free(passbuf);
+
+ g_main_loop_run(loop);
+ exit(EXIT_SUCCESS);
+}
diff --git a/auth/examples/simple_example.js b/auth/examples/simple_example.js
new file mode 100644
index 0000000..2bf38c1
--- /dev/null
+++ b/auth/examples/simple_example.js
@@ -0,0 +1,9 @@
+#!/usr/bin/env -S gjs -m
+import Auth from "gi://AstalAuth";
+import Gio from "gi://Gio";
+
+Gio._promisify(Auth.Pam, "authenticate");
+
+await Auth.Pam.authenticate("password")
+ .then(_ => print("authentication sucessful"))
+ .catch(logError); \ No newline at end of file