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>
27 #include "gtlsconsoleinteraction.h"
30 * WARNING: This is not the example you're looking for [slow hand wave]. This
31 * is not industrial strength, it's just for testing. It uses embarassing
32 * functions like getpass() and does lazy things with threads.
35 G_DEFINE_TYPE (GTlsConsoleInteraction, g_tls_console_interaction, G_TYPE_TLS_INTERACTION);
37 static GTlsInteractionResult
38 g_tls_console_interaction_ask_password (GTlsInteraction *interaction,
39 GTlsPassword *password)
44 prompt = g_strdup_printf ("Password \"%s\"': ", g_tls_password_get_description (password));
45 value = getpass (prompt);
48 g_tls_password_set_value (password, (guchar *)value, -1);
49 return G_TLS_INTERACTION_HANDLED;
53 ask_password_with_getpass (GSimpleAsyncResult *res,
55 GCancellable *cancellable)
57 GTlsPassword *password;
59 password = g_simple_async_result_get_op_res_gpointer (res);
60 g_tls_console_interaction_ask_password (G_TLS_INTERACTION (object), password);
64 g_tls_console_interaction_ask_password_async (GTlsInteraction *interaction,
65 GTlsPassword *password,
66 GAsyncReadyCallback callback,
69 GSimpleAsyncResult *res;
71 res = g_simple_async_result_new (G_OBJECT (interaction), callback, user_data,
72 g_tls_console_interaction_ask_password);
73 g_simple_async_result_set_op_res_gpointer (res, g_object_ref (password), g_object_unref);
74 g_simple_async_result_run_in_thread (res, ask_password_with_getpass,
75 G_PRIORITY_DEFAULT, NULL);
80 g_tls_console_interaction_ask_password_finish (GTlsInteraction *interaction,
83 g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (interaction),
84 g_tls_console_interaction_ask_password), G_TLS_INTERACTION_ABORTED);
85 return G_TLS_INTERACTION_HANDLED;
89 g_tls_console_interaction_init (GTlsConsoleInteraction *interaction)
95 g_tls_console_interaction_class_init (GTlsConsoleInteractionClass *klass)
97 GTlsInteractionClass *interaction_class = G_TLS_INTERACTION_CLASS (klass);
98 interaction_class->ask_password = g_tls_console_interaction_ask_password;
99 interaction_class->ask_password_async = g_tls_console_interaction_ask_password_async;
100 interaction_class->ask_password_finish = g_tls_console_interaction_ask_password_finish;
104 g_tls_console_interaction_new (void)
106 return g_object_new (G_TYPE_TLS_CONSOLE_INTERACTION, NULL);