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 "gioenumtypes.h"
28 #include "gtlspassword.h"
33 * SECTION:gtlspassword
34 * @title: GTlsPassword
35 * @short_description: TLS Passwords for prompting
38 * Holds a password used in TLS.
44 * An abstract interface representing a password used in TLS. Often used in
45 * user interaction such as unlocking a key storage token.
58 struct _GTlsPasswordPrivate
62 GDestroyNotify destroy;
63 GTlsPasswordFlags flags;
68 G_DEFINE_TYPE_WITH_PRIVATE (GTlsPassword, g_tls_password, G_TYPE_OBJECT)
71 g_tls_password_init (GTlsPassword *password)
73 password->priv = g_tls_password_get_instance_private (password);
77 g_tls_password_real_get_value (GTlsPassword *password,
81 *length = password->priv->length;
82 return password->priv->value;
86 g_tls_password_real_set_value (GTlsPassword *password,
89 GDestroyNotify destroy)
91 if (password->priv->destroy)
92 (password->priv->destroy) (password->priv->value);
93 password->priv->destroy = NULL;
94 password->priv->value = NULL;
95 password->priv->length = 0;
98 length = strlen ((gchar*) value);
100 password->priv->value = value;
101 password->priv->length = length;
102 password->priv->destroy = destroy;
106 g_tls_password_real_get_default_warning (GTlsPassword *password)
108 GTlsPasswordFlags flags;
110 flags = g_tls_password_get_flags (password);
112 if (flags & G_TLS_PASSWORD_FINAL_TRY)
113 return _("This is the last chance to enter the password correctly before your access is locked out.");
114 if (flags & G_TLS_PASSWORD_MANY_TRIES)
115 return _("Several password entered have been incorrect, and your access will be locked out after further failures.");
116 if (flags & G_TLS_PASSWORD_RETRY)
117 return _("The password entered is incorrect.");
123 g_tls_password_get_property (GObject *object,
128 GTlsPassword *password = G_TLS_PASSWORD (object);
133 g_value_set_flags (value, g_tls_password_get_flags (password));
136 g_value_set_string (value, g_tls_password_get_warning (password));
138 case PROP_DESCRIPTION:
139 g_value_set_string (value, g_tls_password_get_description (password));
142 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
148 g_tls_password_set_property (GObject *object,
153 GTlsPassword *password = G_TLS_PASSWORD (object);
158 g_tls_password_set_flags (password, g_value_get_flags (value));
161 g_tls_password_set_warning (password, g_value_get_string (value));
163 case PROP_DESCRIPTION:
164 g_tls_password_set_description (password, g_value_get_string (value));
167 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
173 g_tls_password_finalize (GObject *object)
175 GTlsPassword *password = G_TLS_PASSWORD (object);
177 g_tls_password_real_set_value (password, NULL, 0, NULL);
178 g_free (password->priv->warning);
179 g_free (password->priv->description);
181 G_OBJECT_CLASS (g_tls_password_parent_class)->finalize (object);
185 g_tls_password_class_init (GTlsPasswordClass *klass)
187 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
189 klass->get_value = g_tls_password_real_get_value;
190 klass->set_value = g_tls_password_real_set_value;
191 klass->get_default_warning = g_tls_password_real_get_default_warning;
193 gobject_class->get_property = g_tls_password_get_property;
194 gobject_class->set_property = g_tls_password_set_property;
195 gobject_class->finalize = g_tls_password_finalize;
197 g_object_class_install_property (gobject_class, PROP_FLAGS,
198 g_param_spec_flags ("flags",
200 P_("Flags about the password"),
201 G_TYPE_TLS_PASSWORD_FLAGS,
204 G_PARAM_STATIC_STRINGS));
206 g_object_class_install_property (gobject_class, PROP_DESCRIPTION,
207 g_param_spec_string ("description",
209 P_("Description of what the password is for"),
212 G_PARAM_STATIC_STRINGS));
214 g_object_class_install_property (gobject_class, PROP_WARNING,
215 g_param_spec_string ("warning",
217 P_("Warning about the password"),
220 G_PARAM_STATIC_STRINGS));
225 * g_tls_password_new:
226 * @flags: the password flags
227 * @description: description of what the password is for
229 * Create a new #GTlsPassword object.
231 * Returns: (transfer full): The newly allocated password object
234 g_tls_password_new (GTlsPasswordFlags flags,
235 const gchar *description)
237 return g_object_new (G_TYPE_TLS_PASSWORD,
239 "description", description,
244 * g_tls_password_get_value:
245 * @password: a #GTlsPassword object
246 * @length: (allow-none): location to place the length of the password.
248 * Get the password value. If @length is not %NULL then it will be
249 * filled in with the length of the password value. (Note that the
250 * password value is not nul-terminated, so you can only pass %NULL
251 * for @length in contexts where you know the password will have a
252 * certain fixed length.)
254 * Returns: The password value (owned by the password object).
259 g_tls_password_get_value (GTlsPassword *password,
262 g_return_val_if_fail (G_IS_TLS_PASSWORD (password), NULL);
263 return G_TLS_PASSWORD_GET_CLASS (password)->get_value (password, length);
267 * g_tls_password_set_value:
268 * @password: a #GTlsPassword object
269 * @value: the new password value
270 * @length: the length of the password, or -1
272 * Set the value for this password. The @value will be copied by the password
275 * Specify the @length, for a non-nul-terminated password. Pass -1 as
276 * @length if using a nul-terminated password, and @length will be
277 * calculated automatically. (Note that the terminating nul is not
278 * considered part of the password in this case.)
283 g_tls_password_set_value (GTlsPassword *password,
287 g_return_if_fail (G_IS_TLS_PASSWORD (password));
290 length = strlen ((gchar *)value);
292 g_tls_password_set_value_full (password, g_memdup (value, length), length, g_free);
296 * g_tls_password_set_value_full:
297 * @password: a #GTlsPassword object
298 * @value: the value for the password
299 * @length: the length of the password, or -1
300 * @destroy: (allow-none): a function to use to free the password.
302 * Provide the value for this password.
304 * The @value will be owned by the password object, and later freed using
305 * the @destroy function callback.
307 * Specify the @length, for a non-nul-terminated password. Pass -1 as
308 * @length if using a nul-terminated password, and @length will be
309 * calculated automatically. (Note that the terminating nul is not
310 * considered part of the password in this case.)
316 g_tls_password_set_value_full (GTlsPassword *password,
319 GDestroyNotify destroy)
321 g_return_if_fail (G_IS_TLS_PASSWORD (password));
322 G_TLS_PASSWORD_GET_CLASS (password)->set_value (password, value,
327 * g_tls_password_get_flags:
328 * @password: a #GTlsPassword object
330 * Get flags about the password.
332 * Return value: The flags about the password.
337 g_tls_password_get_flags (GTlsPassword *password)
339 g_return_val_if_fail (G_IS_TLS_PASSWORD (password), G_TLS_PASSWORD_NONE);
340 return password->priv->flags;
344 * g_tls_password_set_flags:
345 * @password: a #GTlsPassword object
346 * @flags: The flags about the password
348 * Set flags about the password.
353 g_tls_password_set_flags (GTlsPassword *password,
354 GTlsPasswordFlags flags)
356 g_return_if_fail (G_IS_TLS_PASSWORD (password));
358 password->priv->flags = flags;
360 g_object_notify (G_OBJECT (password), "flags");
364 * g_tls_password_get_description:
365 * @password: a #GTlsPassword object
367 * Get a description string about what the password will be used for.
369 * Return value: The description of the password.
374 g_tls_password_get_description (GTlsPassword *password)
376 g_return_val_if_fail (G_IS_TLS_PASSWORD (password), NULL);
377 return password->priv->description;
381 * g_tls_password_set_description:
382 * @password: a #GTlsPassword object
383 * @description: The description of the password
385 * Set a description string about what the password will be used for.
390 g_tls_password_set_description (GTlsPassword *password,
391 const gchar *description)
395 g_return_if_fail (G_IS_TLS_PASSWORD (password));
397 copy = g_strdup (description);
398 g_free (password->priv->description);
399 password->priv->description = copy;
401 g_object_notify (G_OBJECT (password), "description");
405 * g_tls_password_get_warning:
406 * @password: a #GTlsPassword object
408 * Get a user readable translated warning. Usually this warning is a
409 * representation of the password flags returned from
410 * g_tls_password_get_flags().
412 * Return value: The warning.
417 g_tls_password_get_warning (GTlsPassword *password)
419 g_return_val_if_fail (G_IS_TLS_PASSWORD (password), NULL);
421 if (password->priv->warning == NULL)
422 return G_TLS_PASSWORD_GET_CLASS (password)->get_default_warning (password);
424 return password->priv->warning;
428 * g_tls_password_set_warning:
429 * @password: a #GTlsPassword object
430 * @warning: The user readable warning
432 * Set a user readable translated warning. Usually this warning is a
433 * representation of the password flags returned from
434 * g_tls_password_get_flags().
439 g_tls_password_set_warning (GTlsPassword *password,
440 const gchar *warning)
444 g_return_if_fail (G_IS_TLS_PASSWORD (password));
446 copy = g_strdup (warning);
447 g_free (password->priv->warning);
448 password->priv->warning = copy;
450 g_object_notify (G_OBJECT (password), "warning");