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 (GTlsPassword
, g_tls_password
, G_TYPE_OBJECT
);
71 g_tls_password_init (GTlsPassword
*password
)
73 password
->priv
= G_TYPE_INSTANCE_GET_PRIVATE (password
, G_TYPE_TLS_PASSWORD
,
78 g_tls_password_real_get_value (GTlsPassword
*password
,
82 *length
= password
->priv
->length
;
83 return password
->priv
->value
;
87 g_tls_password_real_set_value (GTlsPassword
*password
,
90 GDestroyNotify destroy
)
92 if (password
->priv
->destroy
)
93 (password
->priv
->destroy
) (password
->priv
->value
);
94 password
->priv
->destroy
= NULL
;
95 password
->priv
->value
= NULL
;
96 password
->priv
->length
= 0;
99 length
= strlen ((gchar
*) value
);
101 password
->priv
->value
= value
;
102 password
->priv
->length
= length
;
103 password
->priv
->destroy
= destroy
;
107 g_tls_password_real_get_default_warning (GTlsPassword
*password
)
109 GTlsPasswordFlags flags
;
111 flags
= g_tls_password_get_flags (password
);
113 if (flags
& G_TLS_PASSWORD_FINAL_TRY
)
114 return _("This is the last chance to enter the password correctly before your access is locked out.");
115 if (flags
& G_TLS_PASSWORD_MANY_TRIES
)
116 return _("Several password entered have been incorrect, and your access will be locked out after further failures.");
117 if (flags
& G_TLS_PASSWORD_RETRY
)
118 return _("The password entered is incorrect.");
124 g_tls_password_get_property (GObject
*object
,
129 GTlsPassword
*password
= G_TLS_PASSWORD (object
);
134 g_value_set_flags (value
, g_tls_password_get_flags (password
));
137 g_value_set_string (value
, g_tls_password_get_warning (password
));
139 case PROP_DESCRIPTION
:
140 g_value_set_string (value
, g_tls_password_get_description (password
));
143 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
149 g_tls_password_set_property (GObject
*object
,
154 GTlsPassword
*password
= G_TLS_PASSWORD (object
);
159 g_tls_password_set_flags (password
, g_value_get_flags (value
));
162 g_tls_password_set_warning (password
, g_value_get_string (value
));
164 case PROP_DESCRIPTION
:
165 g_tls_password_set_description (password
, g_value_get_string (value
));
168 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
174 g_tls_password_finalize (GObject
*object
)
176 GTlsPassword
*password
= G_TLS_PASSWORD (object
);
178 g_tls_password_real_set_value (password
, NULL
, 0, NULL
);
179 g_free (password
->priv
->warning
);
180 g_free (password
->priv
->description
);
182 G_OBJECT_CLASS (g_tls_password_parent_class
)->finalize (object
);
186 g_tls_password_class_init (GTlsPasswordClass
*klass
)
188 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
190 klass
->get_value
= g_tls_password_real_get_value
;
191 klass
->set_value
= g_tls_password_real_set_value
;
192 klass
->get_default_warning
= g_tls_password_real_get_default_warning
;
194 gobject_class
->get_property
= g_tls_password_get_property
;
195 gobject_class
->set_property
= g_tls_password_set_property
;
196 gobject_class
->finalize
= g_tls_password_finalize
;
198 g_type_class_add_private (klass
, sizeof (GTlsPasswordPrivate
));
200 g_object_class_install_property (gobject_class
, PROP_FLAGS
,
201 g_param_spec_flags ("flags", "Flags", "Flags about the password",
202 G_TYPE_TLS_PASSWORD_FLAGS
, G_TLS_PASSWORD_NONE
,
203 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
205 g_object_class_install_property (gobject_class
, PROP_DESCRIPTION
,
206 g_param_spec_string ("description", "Description", "Description of what the password is for",
207 "", G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
209 g_object_class_install_property (gobject_class
, PROP_WARNING
,
210 g_param_spec_string ("warning", "Warning", "Warning about the password",
211 "", G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
));
216 * g_tls_password_new:
217 * @flags: the password flags
218 * @description: description of what the password is for
220 * Create a new #GTlsPassword object.
222 * Returns: (transfer full): The newly allocated password object
225 g_tls_password_new (GTlsPasswordFlags flags
,
226 const gchar
*description
)
228 return g_object_new (G_TYPE_TLS_PASSWORD
,
230 "description", description
,
235 * g_tls_password_get_value:
236 * @password: a #GTlsPassword object
237 * @length: (allow-none): location to place the length of the password.
239 * Get the password value. If @length is not %NULL then it will be filled
240 * in with the length of the password value.
242 * Returns: The password value owned by the password object.
247 g_tls_password_get_value (GTlsPassword
*password
,
250 g_return_val_if_fail (G_IS_TLS_PASSWORD (password
), NULL
);
251 return G_TLS_PASSWORD_GET_CLASS (password
)->get_value (password
, length
);
255 * g_tls_password_set_value:
256 * @password: a #GTlsPassword object
257 * @value: the new password value
258 * @length: the length of the password, or -1
260 * Set the value for this password. The @value will be copied by the password
263 * Specify the @length, for a non-null-terminated password. Pass -1 as
264 * @length if using a null-terminated password, and @length will be calculated
270 g_tls_password_set_value (GTlsPassword
*password
,
274 g_return_if_fail (G_IS_TLS_PASSWORD (password
));
277 length
= strlen ((gchar
*)value
);
279 g_tls_password_set_value_full (password
, g_memdup (value
, length
), length
, g_free
);
283 * g_tls_password_set_value_full:
284 * @password: a #GTlsPassword object
285 * @value: the value for the password
286 * @length: the length of the password, or -1
287 * @destroy: (allow-none): a function to use to free the password.
289 * Provide the value for this password.
291 * The @value will be owned by the password object, and later freed using
292 * the @destroy function callback.
294 * Specify the @length, for a non-null-terminated password. Pass -1 as
295 * @length if using a null-terminated password, and @length will be calculated
301 g_tls_password_set_value_full (GTlsPassword
*password
,
304 GDestroyNotify destroy
)
306 g_return_if_fail (G_IS_TLS_PASSWORD (password
));
307 G_TLS_PASSWORD_GET_CLASS (password
)->set_value (password
, value
,
312 * g_tls_password_get_flags:
313 * @password: a #GTlsPassword object
315 * Get flags about the password.
317 * Return value: The flags about the password.
322 g_tls_password_get_flags (GTlsPassword
*password
)
324 g_return_val_if_fail (G_IS_TLS_PASSWORD (password
), G_TLS_PASSWORD_NONE
);
325 return password
->priv
->flags
;
329 * g_tls_password_set_flags:
330 * @password: a #GTlsPassword object
331 * @flags: The flags about the password
333 * Set flags about the password.
338 g_tls_password_set_flags (GTlsPassword
*password
,
339 GTlsPasswordFlags flags
)
341 g_return_if_fail (G_IS_TLS_PASSWORD (password
));
343 password
->priv
->flags
= flags
;
345 g_object_notify (G_OBJECT (password
), "flags");
349 * g_tls_password_get_description:
350 * @password: a #GTlsPassword object
352 * Get a description string about what the password will be used for.
354 * Return value: The description of the password.
359 g_tls_password_get_description (GTlsPassword
*password
)
361 g_return_val_if_fail (G_IS_TLS_PASSWORD (password
), NULL
);
362 return password
->priv
->description
;
366 * g_tls_password_set_description:
367 * @password: a #GTlsPassword object
368 * @description: The description of the password
370 * Set a description string about what the password will be used for.
375 g_tls_password_set_description (GTlsPassword
*password
,
376 const gchar
*description
)
380 g_return_if_fail (G_IS_TLS_PASSWORD (password
));
382 copy
= g_strdup (description
);
383 g_free (password
->priv
->description
);
384 password
->priv
->description
= copy
;
386 g_object_notify (G_OBJECT (password
), "description");
390 * g_tls_password_get_warning:
391 * @password: a #GTlsPassword object
393 * Get a user readable translated warning. Usually this warning is a
394 * representation of the password flags returned from
395 * g_tls_password_get_flags().
397 * Return value: The warning.
402 g_tls_password_get_warning (GTlsPassword
*password
)
404 g_return_val_if_fail (G_IS_TLS_PASSWORD (password
), NULL
);
406 if (password
->priv
->warning
== NULL
)
407 return G_TLS_PASSWORD_GET_CLASS (password
)->get_default_warning (password
);
409 return password
->priv
->warning
;
413 * g_tls_password_set_warning:
414 * @password: a #GTlsPassword object
415 * @warning: The user readable warning
417 * Set a user readable translated warning. Usually this warning is a
418 * representation of the password flags returned from
419 * g_tls_password_get_flags().
424 g_tls_password_set_warning (GTlsPassword
*password
,
425 const gchar
*warning
)
429 g_return_if_fail (G_IS_TLS_PASSWORD (password
));
431 copy
= g_strdup (warning
);
432 g_free (password
->priv
->warning
);
433 password
->priv
->warning
= copy
;
435 g_object_notify (G_OBJECT (password
), "warning");