1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2011 Collabora, Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Stef Walter <stefw@collabora.co.uk>
29 #include <glib/gprintf.h>
33 #include "gtlsconsoleinteraction.h"
36 * WARNING: This is not the example you're looking for [slow hand wave]. This
37 * is not industrial strength, it's just for testing. It uses embarassing
38 * functions like getpass() and does lazy things with threads.
41 G_DEFINE_TYPE (GTlsConsoleInteraction, g_tls_console_interaction, G_TYPE_TLS_INTERACTION);
44 /* win32 doesn't have getpass() */
46 getpass (const gchar *prompt)
48 static gchar buf[BUFSIZ];
51 g_printf ("%s", prompt);
54 for (i = 0; i < BUFSIZ - 1; ++i)
68 static GTlsInteractionResult
69 g_tls_console_interaction_ask_password (GTlsInteraction *interaction,
70 GTlsPassword *password,
71 GCancellable *cancellable,
77 prompt = g_strdup_printf ("Password \"%s\"': ", g_tls_password_get_description (password));
78 value = getpass (prompt);
81 if (g_cancellable_set_error_if_cancelled (cancellable, error))
82 return G_TLS_INTERACTION_FAILED;
84 g_tls_password_set_value (password, (guchar *)value, -1);
85 return G_TLS_INTERACTION_HANDLED;
89 ask_password_with_getpass (GSimpleAsyncResult *res,
91 GCancellable *cancellable)
93 GTlsPassword *password;
96 password = g_simple_async_result_get_op_res_gpointer (res);
97 g_tls_console_interaction_ask_password (G_TLS_INTERACTION (object), password,
100 g_simple_async_result_take_error (res, error);
104 g_tls_console_interaction_ask_password_async (GTlsInteraction *interaction,
105 GTlsPassword *password,
106 GCancellable *cancellable,
107 GAsyncReadyCallback callback,
110 GSimpleAsyncResult *res;
112 res = g_simple_async_result_new (G_OBJECT (interaction), callback, user_data,
113 g_tls_console_interaction_ask_password);
114 g_simple_async_result_set_op_res_gpointer (res, g_object_ref (password), g_object_unref);
115 g_simple_async_result_run_in_thread (res, ask_password_with_getpass,
116 G_PRIORITY_DEFAULT, cancellable);
117 g_object_unref (res);
120 static GTlsInteractionResult
121 g_tls_console_interaction_ask_password_finish (GTlsInteraction *interaction,
122 GAsyncResult *result,
125 g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (interaction),
126 g_tls_console_interaction_ask_password), G_TLS_INTERACTION_FAILED);
128 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error))
129 return G_TLS_INTERACTION_FAILED;
131 return G_TLS_INTERACTION_HANDLED;
135 g_tls_console_interaction_init (GTlsConsoleInteraction *interaction)
141 g_tls_console_interaction_class_init (GTlsConsoleInteractionClass *klass)
143 GTlsInteractionClass *interaction_class = G_TLS_INTERACTION_CLASS (klass);
144 interaction_class->ask_password = g_tls_console_interaction_ask_password;
145 interaction_class->ask_password_async = g_tls_console_interaction_ask_password_async;
146 interaction_class->ask_password_finish = g_tls_console_interaction_ask_password_finish;
150 g_tls_console_interaction_new (void)
152 return g_object_new (G_TYPE_TLS_CONSOLE_INTERACTION, NULL);