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, see <http://www.gnu.org/licenses/>.
18 * Author: Stef Walter <stefw@collabora.co.uk>
25 #include "gioenumtypes.h"
26 #include "gtlspassword.h"
31 * SECTION:gtlspassword
32 * @title: GTlsPassword
33 * @short_description: TLS Passwords for prompting
36 * Holds a password used in TLS.
42 * An abstract interface representing a password used in TLS. Often used in
43 * user interaction such as unlocking a key storage token.
56 struct _GTlsPasswordPrivate
60 GDestroyNotify destroy;
61 GTlsPasswordFlags flags;
66 G_DEFINE_TYPE_WITH_PRIVATE (GTlsPassword, g_tls_password, G_TYPE_OBJECT)
69 g_tls_password_init (GTlsPassword *password)
71 password->priv = g_tls_password_get_instance_private (password);
75 g_tls_password_real_get_value (GTlsPassword *password,
79 *length = password->priv->length;
80 return password->priv->value;
84 g_tls_password_real_set_value (GTlsPassword *password,
87 GDestroyNotify destroy)
89 if (password->priv->destroy)
90 (password->priv->destroy) (password->priv->value);
91 password->priv->destroy = NULL;
92 password->priv->value = NULL;
93 password->priv->length = 0;
96 length = strlen ((gchar*) value);
98 password->priv->value = value;
99 password->priv->length = length;
100 password->priv->destroy = destroy;
104 g_tls_password_real_get_default_warning (GTlsPassword *password)
106 GTlsPasswordFlags flags;
108 flags = g_tls_password_get_flags (password);
110 if (flags & G_TLS_PASSWORD_FINAL_TRY)
111 return _("This is the last chance to enter the password correctly before your access is locked out.");
112 if (flags & G_TLS_PASSWORD_MANY_TRIES)
113 return _("Several password entered have been incorrect, and your access will be locked out after further failures.");
114 if (flags & G_TLS_PASSWORD_RETRY)
115 return _("The password entered is incorrect.");
121 g_tls_password_get_property (GObject *object,
126 GTlsPassword *password = G_TLS_PASSWORD (object);
131 g_value_set_flags (value, g_tls_password_get_flags (password));
134 g_value_set_string (value, g_tls_password_get_warning (password));
136 case PROP_DESCRIPTION:
137 g_value_set_string (value, g_tls_password_get_description (password));
140 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
146 g_tls_password_set_property (GObject *object,
151 GTlsPassword *password = G_TLS_PASSWORD (object);
156 g_tls_password_set_flags (password, g_value_get_flags (value));
159 g_tls_password_set_warning (password, g_value_get_string (value));
161 case PROP_DESCRIPTION:
162 g_tls_password_set_description (password, g_value_get_string (value));
165 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
171 g_tls_password_finalize (GObject *object)
173 GTlsPassword *password = G_TLS_PASSWORD (object);
175 g_tls_password_real_set_value (password, NULL, 0, NULL);
176 g_free (password->priv->warning);
177 g_free (password->priv->description);
179 G_OBJECT_CLASS (g_tls_password_parent_class)->finalize (object);
183 g_tls_password_class_init (GTlsPasswordClass *klass)
185 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
187 klass->get_value = g_tls_password_real_get_value;
188 klass->set_value = g_tls_password_real_set_value;
189 klass->get_default_warning = g_tls_password_real_get_default_warning;
191 gobject_class->get_property = g_tls_password_get_property;
192 gobject_class->set_property = g_tls_password_set_property;
193 gobject_class->finalize = g_tls_password_finalize;
195 g_object_class_install_property (gobject_class, PROP_FLAGS,
196 g_param_spec_flags ("flags",
198 P_("Flags about the password"),
199 G_TYPE_TLS_PASSWORD_FLAGS,
202 G_PARAM_STATIC_STRINGS));
204 g_object_class_install_property (gobject_class, PROP_DESCRIPTION,
205 g_param_spec_string ("description",
207 P_("Description of what the password is for"),
210 G_PARAM_STATIC_STRINGS));
212 g_object_class_install_property (gobject_class, PROP_WARNING,
213 g_param_spec_string ("warning",
215 P_("Warning about the password"),
218 G_PARAM_STATIC_STRINGS));
223 * g_tls_password_new:
224 * @flags: the password flags
225 * @description: description of what the password is for
227 * Create a new #GTlsPassword object.
229 * Returns: (transfer full): The newly allocated password object
232 g_tls_password_new (GTlsPasswordFlags flags,
233 const gchar *description)
235 return g_object_new (G_TYPE_TLS_PASSWORD,
237 "description", description,
242 * g_tls_password_get_value:
243 * @password: a #GTlsPassword object
244 * @length: (allow-none): location to place the length of the password.
246 * Get the password value. If @length is not %NULL then it will be
247 * filled in with the length of the password value. (Note that the
248 * password value is not nul-terminated, so you can only pass %NULL
249 * for @length in contexts where you know the password will have a
250 * certain fixed length.)
252 * Returns: The password value (owned by the password object).
257 g_tls_password_get_value (GTlsPassword *password,
260 g_return_val_if_fail (G_IS_TLS_PASSWORD (password), NULL);
261 return G_TLS_PASSWORD_GET_CLASS (password)->get_value (password, length);
265 * g_tls_password_set_value:
266 * @password: a #GTlsPassword object
267 * @value: the new password value
268 * @length: the length of the password, or -1
270 * Set the value for this password. The @value will be copied by the password
273 * Specify the @length, for a non-nul-terminated password. Pass -1 as
274 * @length if using a nul-terminated password, and @length will be
275 * calculated automatically. (Note that the terminating nul is not
276 * considered part of the password in this case.)
281 g_tls_password_set_value (GTlsPassword *password,
285 g_return_if_fail (G_IS_TLS_PASSWORD (password));
288 length = strlen ((gchar *)value);
290 g_tls_password_set_value_full (password, g_memdup (value, length), length, g_free);
294 * g_tls_password_set_value_full:
295 * @password: a #GTlsPassword object
296 * @value: the value for the password
297 * @length: the length of the password, or -1
298 * @destroy: (allow-none): a function to use to free the password.
300 * Provide the value for this password.
302 * The @value will be owned by the password object, and later freed using
303 * the @destroy function callback.
305 * Specify the @length, for a non-nul-terminated password. Pass -1 as
306 * @length if using a nul-terminated password, and @length will be
307 * calculated automatically. (Note that the terminating nul is not
308 * considered part of the password in this case.)
314 g_tls_password_set_value_full (GTlsPassword *password,
317 GDestroyNotify destroy)
319 g_return_if_fail (G_IS_TLS_PASSWORD (password));
320 G_TLS_PASSWORD_GET_CLASS (password)->set_value (password, value,
325 * g_tls_password_get_flags:
326 * @password: a #GTlsPassword object
328 * Get flags about the password.
330 * Returns: The flags about the password.
335 g_tls_password_get_flags (GTlsPassword *password)
337 g_return_val_if_fail (G_IS_TLS_PASSWORD (password), G_TLS_PASSWORD_NONE);
338 return password->priv->flags;
342 * g_tls_password_set_flags:
343 * @password: a #GTlsPassword object
344 * @flags: The flags about the password
346 * Set flags about the password.
351 g_tls_password_set_flags (GTlsPassword *password,
352 GTlsPasswordFlags flags)
354 g_return_if_fail (G_IS_TLS_PASSWORD (password));
356 password->priv->flags = flags;
358 g_object_notify (G_OBJECT (password), "flags");
362 * g_tls_password_get_description:
363 * @password: a #GTlsPassword object
365 * Get a description string about what the password will be used for.
367 * Returns: The description of the password.
372 g_tls_password_get_description (GTlsPassword *password)
374 g_return_val_if_fail (G_IS_TLS_PASSWORD (password), NULL);
375 return password->priv->description;
379 * g_tls_password_set_description:
380 * @password: a #GTlsPassword object
381 * @description: The description of the password
383 * Set a description string about what the password will be used for.
388 g_tls_password_set_description (GTlsPassword *password,
389 const gchar *description)
393 g_return_if_fail (G_IS_TLS_PASSWORD (password));
395 copy = g_strdup (description);
396 g_free (password->priv->description);
397 password->priv->description = copy;
399 g_object_notify (G_OBJECT (password), "description");
403 * g_tls_password_get_warning:
404 * @password: a #GTlsPassword object
406 * Get a user readable translated warning. Usually this warning is a
407 * representation of the password flags returned from
408 * g_tls_password_get_flags().
410 * Returns: The warning.
415 g_tls_password_get_warning (GTlsPassword *password)
417 g_return_val_if_fail (G_IS_TLS_PASSWORD (password), NULL);
419 if (password->priv->warning == NULL)
420 return G_TLS_PASSWORD_GET_CLASS (password)->get_default_warning (password);
422 return password->priv->warning;
426 * g_tls_password_set_warning:
427 * @password: a #GTlsPassword object
428 * @warning: The user readable warning
430 * Set a user readable translated warning. Usually this warning is a
431 * representation of the password flags returned from
432 * g_tls_password_get_flags().
437 g_tls_password_set_warning (GTlsPassword *password,
438 const gchar *warning)
442 g_return_if_fail (G_IS_TLS_PASSWORD (password));
444 copy = g_strdup (warning);
445 g_free (password->priv->warning);
446 password->priv->warning = copy;
448 g_object_notify (G_OBJECT (password), "warning");