2 * e-source-authentication.c
4 * This library is free software you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * SECTION: e-source-authentication
20 * @include: libedataserver/libedataserver.h
21 * @short_description: #ESource extension for authentication settings
23 * The #ESourceAuthentication extension tracks authentication settings
24 * for a user account on a remote server.
26 * Access the extension as follows:
29 * #include <libedataserver/libedataserver.h>
31 * ESourceAuthentication *extension;
33 * extension = e_source_get_extension (source, E_SOURCE_EXTENSION_AUTHENTICATION);
37 #include "e-source-authentication.h"
39 #include <libedataserver/e-data-server-util.h>
41 #define E_SOURCE_AUTHENTICATION_GET_PRIVATE(obj) \
42 (G_TYPE_INSTANCE_GET_PRIVATE \
43 ((obj), E_TYPE_SOURCE_AUTHENTICATION, ESourceAuthenticationPrivate))
45 struct _ESourceAuthenticationPrivate {
51 gboolean remember_password;
54 /* GNetworkAddress caches data internally, so we maintain the
55 * instance to preserve the cache as opposed to just creating
56 * a new GNetworkAddress instance each time it's requested. */
57 GSocketConnectable *connectable;
67 PROP_REMEMBER_PASSWORD,
72 ESourceAuthentication,
73 e_source_authentication,
74 E_TYPE_SOURCE_EXTENSION)
77 source_authentication_update_connectable (ESourceAuthentication *extension)
82 /* This MUST be called with the property_lock acquired. */
84 g_clear_object (&extension->priv->connectable);
86 host = e_source_authentication_get_host (extension);
87 port = e_source_authentication_get_port (extension);
90 GSocketConnectable *connectable;
91 connectable = g_network_address_new (host, port);
92 extension->priv->connectable = connectable;
97 source_authentication_set_property (GObject *object,
102 switch (property_id) {
104 e_source_authentication_set_host (
105 E_SOURCE_AUTHENTICATION (object),
106 g_value_get_string (value));
110 e_source_authentication_set_method (
111 E_SOURCE_AUTHENTICATION (object),
112 g_value_get_string (value));
116 e_source_authentication_set_port (
117 E_SOURCE_AUTHENTICATION (object),
118 g_value_get_uint (value));
122 e_source_authentication_set_proxy_uid (
123 E_SOURCE_AUTHENTICATION (object),
124 g_value_get_string (value));
127 case PROP_REMEMBER_PASSWORD:
128 e_source_authentication_set_remember_password (
129 E_SOURCE_AUTHENTICATION (object),
130 g_value_get_boolean (value));
134 e_source_authentication_set_user (
135 E_SOURCE_AUTHENTICATION (object),
136 g_value_get_string (value));
140 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
144 source_authentication_get_property (GObject *object,
149 switch (property_id) {
150 case PROP_CONNECTABLE:
151 g_value_take_object (
153 e_source_authentication_ref_connectable (
154 E_SOURCE_AUTHENTICATION (object)));
158 g_value_take_string (
160 e_source_authentication_dup_host (
161 E_SOURCE_AUTHENTICATION (object)));
165 g_value_take_string (
167 e_source_authentication_dup_method (
168 E_SOURCE_AUTHENTICATION (object)));
174 e_source_authentication_get_port (
175 E_SOURCE_AUTHENTICATION (object)));
179 g_value_take_string (
181 e_source_authentication_dup_proxy_uid (
182 E_SOURCE_AUTHENTICATION (object)));
185 case PROP_REMEMBER_PASSWORD:
186 g_value_set_boolean (
188 e_source_authentication_get_remember_password (
189 E_SOURCE_AUTHENTICATION (object)));
193 g_value_take_string (
195 e_source_authentication_dup_user (
196 E_SOURCE_AUTHENTICATION (object)));
200 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
204 source_authentication_dispose (GObject *object)
206 ESourceAuthenticationPrivate *priv;
208 priv = E_SOURCE_AUTHENTICATION_GET_PRIVATE (object);
210 g_clear_object (&priv->connectable);
212 /* Chain up to parent's dispose() method. */
213 G_OBJECT_CLASS (e_source_authentication_parent_class)->dispose (object);
217 source_authentication_finalize (GObject *object)
219 ESourceAuthenticationPrivate *priv;
221 priv = E_SOURCE_AUTHENTICATION_GET_PRIVATE (object);
223 g_mutex_clear (&priv->property_lock);
226 g_free (priv->method);
227 g_free (priv->proxy_uid);
230 /* Chain up to parent's finalize() method. */
231 G_OBJECT_CLASS (e_source_authentication_parent_class)->finalize (object);
235 e_source_authentication_class_init (ESourceAuthenticationClass *class)
237 GObjectClass *object_class;
238 ESourceExtensionClass *extension_class;
240 g_type_class_add_private (class, sizeof (ESourceAuthenticationPrivate));
242 object_class = G_OBJECT_CLASS (class);
243 object_class->set_property = source_authentication_set_property;
244 object_class->get_property = source_authentication_get_property;
245 object_class->dispose = source_authentication_dispose;
246 object_class->finalize = source_authentication_finalize;
248 extension_class = E_SOURCE_EXTENSION_CLASS (class);
249 extension_class->name = E_SOURCE_EXTENSION_AUTHENTICATION;
251 g_object_class_install_property (
254 g_param_spec_object (
257 "A GSocketConnectable constructed "
258 "from the host and port properties",
259 G_TYPE_SOCKET_CONNECTABLE,
261 G_PARAM_STATIC_STRINGS));
263 g_object_class_install_property (
266 g_param_spec_string (
269 "Host name for the remote account",
273 G_PARAM_STATIC_STRINGS |
274 E_SOURCE_PARAM_SETTING));
276 g_object_class_install_property (
279 g_param_spec_string (
282 "Authentication method",
286 G_PARAM_STATIC_STRINGS |
287 E_SOURCE_PARAM_SETTING));
289 g_object_class_install_property (
295 "Port number for the remote account",
299 G_PARAM_STATIC_STRINGS |
300 E_SOURCE_PARAM_SETTING));
302 g_object_class_install_property (
305 g_param_spec_string (
308 "ESource UID of a proxy profile",
312 G_PARAM_STATIC_STRINGS |
313 E_SOURCE_PARAM_SETTING));
315 g_object_class_install_property (
317 PROP_REMEMBER_PASSWORD,
318 g_param_spec_boolean (
321 "Whether to offer to remember the "
322 "password by default when prompted",
326 G_PARAM_STATIC_STRINGS |
327 E_SOURCE_PARAM_SETTING));
329 g_object_class_install_property (
332 g_param_spec_string (
335 "User name for the remote account",
339 G_PARAM_STATIC_STRINGS |
340 E_SOURCE_PARAM_SETTING));
344 e_source_authentication_init (ESourceAuthentication *extension)
346 extension->priv = E_SOURCE_AUTHENTICATION_GET_PRIVATE (extension);
347 g_mutex_init (&extension->priv->property_lock);
351 * e_source_authentication_required:
352 * @extension: an #ESourceAuthentication
354 * This is a convenience function which returns whether authentication
355 * is required at all, regardless of the method used. This relies on
356 * the convention of setting #ESourceAuthentication:method to "none"
357 * when authentication is <emphasis>not</emphasis> required.
359 * Returns: whether authentication is required at all
364 e_source_authentication_required (ESourceAuthentication *extension)
368 g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), FALSE);
370 method = e_source_authentication_get_method (extension);
371 g_return_val_if_fail (method != NULL && *method != '\0', FALSE);
373 return (g_strcmp0 (method, "none") != 0);
377 * e_source_authentication_ref_connectable:
378 * @extension: an #ESourceAuthentication
380 * Returns a #GSocketConnectable instance constructed from @extension's
381 * #ESourceAuthentication:host and #ESourceAuthentication:port properties,
382 * or %NULL if the #ESourceAuthentication:host is not set.
384 * The returned #GSocketConnectable is referenced for thread-safety and must
385 * be unreferenced with g_object_unref() when finished with it.
387 * Returns: (transfer full): a #GSocketConnectable, or %NULL
392 e_source_authentication_ref_connectable (ESourceAuthentication *extension)
394 GSocketConnectable *connectable = NULL;
396 g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), NULL);
398 g_mutex_lock (&extension->priv->property_lock);
400 if (extension->priv->connectable != NULL)
401 connectable = g_object_ref (extension->priv->connectable);
403 g_mutex_unlock (&extension->priv->property_lock);
409 * e_source_authentication_get_host:
410 * @extension: an #ESourceAuthentication
412 * Returns the host name used to authenticate to a remote account.
414 * Returns: the host name of a remote account
419 e_source_authentication_get_host (ESourceAuthentication *extension)
421 g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), NULL);
423 return extension->priv->host;
427 * e_source_authentication_dup_host:
428 * @extension: an #ESourceAuthentication
430 * Thread-safe variation of e_source_authentication_get_host().
431 * Use this function when accessing @extension from multiple threads.
433 * The returned string should be freed with g_free() when no longer needed.
435 * Returns: a newly-allocated copy of #ESourceAuthentication:host
440 e_source_authentication_dup_host (ESourceAuthentication *extension)
442 const gchar *protected;
445 g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), NULL);
447 g_mutex_lock (&extension->priv->property_lock);
449 protected = e_source_authentication_get_host (extension);
450 duplicate = g_strdup (protected);
452 g_mutex_unlock (&extension->priv->property_lock);
458 * e_source_authentication_set_host:
459 * @extension: an #ESourceAuthentication
460 * @host: (allow-none): a host name, or %NULL
462 * Sets the host name used to authenticate to a remote account.
464 * The internal copy of @host is automatically stripped of leading and
465 * trailing whitespace. If the resulting string is empty, %NULL is set
471 e_source_authentication_set_host (ESourceAuthentication *extension,
474 g_return_if_fail (E_IS_SOURCE_AUTHENTICATION (extension));
476 g_mutex_lock (&extension->priv->property_lock);
478 if (g_strcmp0 (extension->priv->host, host) == 0) {
479 g_mutex_unlock (&extension->priv->property_lock);
483 g_free (extension->priv->host);
484 extension->priv->host = e_util_strdup_strip (host);
486 source_authentication_update_connectable (extension);
488 g_mutex_unlock (&extension->priv->property_lock);
490 g_object_notify (G_OBJECT (extension), "host");
492 /* Changing the host also changes the connectable. */
493 g_object_notify (G_OBJECT (extension), "connectable");
497 * e_source_authentication_get_method:
498 * @extension: an #ESourceAuthentication
500 * Returns the authentication method for a remote account. There are
501 * no pre-defined method names; backends are free to set this however
502 * they wish. If authentication is not required for a remote account,
503 * the convention is to set #ESourceAuthentication:method to "none".
505 * Returns: the authentication method for a remote account
510 e_source_authentication_get_method (ESourceAuthentication *extension)
512 g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), NULL);
514 return extension->priv->method;
518 * e_source_authentication_dup_method:
519 * @extension: an #ESourceAuthentication
521 * Thread-safe variation of e_source_authentication_get_method().
522 * Use this function when accessing @extension from multiple threads.
524 * The returned string should be freed with g_free() when no longer needed.
526 * Returns: a newly-allocated copy of #ESourceAuthentication:method
531 e_source_authentication_dup_method (ESourceAuthentication *extension)
533 const gchar *protected;
536 g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), NULL);
538 g_mutex_lock (&extension->priv->property_lock);
540 protected = e_source_authentication_get_method (extension);
541 duplicate = g_strdup (protected);
543 g_mutex_unlock (&extension->priv->property_lock);
549 * e_source_authentication_set_method:
550 * @extension: an #ESourceAuthentication
551 * @method: (allow-none): authentication method, or %NULL
553 * Sets the authentication method for a remote account. There are no
554 * pre-defined method names; backends are free to set this however they
555 * wish. If authentication is not required for a remote account, the
556 * convention is to set the method to "none". In keeping with that
557 * convention, #ESourceAuthentication:method will be set to "none" if
558 * @method is %NULL or an empty string.
563 e_source_authentication_set_method (ESourceAuthentication *extension,
566 g_return_if_fail (E_IS_SOURCE_AUTHENTICATION (extension));
568 g_mutex_lock (&extension->priv->property_lock);
570 if (g_strcmp0 (extension->priv->method, method) == 0) {
571 g_mutex_unlock (&extension->priv->property_lock);
575 g_free (extension->priv->method);
576 extension->priv->method = e_util_strdup_strip (method);
578 if (extension->priv->method == NULL)
579 extension->priv->method = g_strdup ("none");
581 g_mutex_unlock (&extension->priv->property_lock);
583 g_object_notify (G_OBJECT (extension), "method");
587 * e_source_authentication_get_port:
588 * @extension: an #ESourceAuthentication
590 * Returns the port number used to authenticate to a remote account.
592 * Returns: the port number of a remote account
597 e_source_authentication_get_port (ESourceAuthentication *extension)
599 g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), 0);
601 return extension->priv->port;
605 * e_source_authentication_set_port:
606 * @extension: an #ESourceAuthentication
607 * @port: a port number
609 * Sets the port number used to authenticate to a remote account.
614 e_source_authentication_set_port (ESourceAuthentication *extension,
617 g_return_if_fail (E_SOURCE_AUTHENTICATION (extension));
619 g_mutex_lock (&extension->priv->property_lock);
621 if (extension->priv->port == port) {
622 g_mutex_unlock (&extension->priv->property_lock);
626 extension->priv->port = port;
628 source_authentication_update_connectable (extension);
630 g_mutex_unlock (&extension->priv->property_lock);
632 g_object_notify (G_OBJECT (extension), "port");
634 /* Changing the port also changes the connectable. */
635 g_object_notify (G_OBJECT (extension), "connectable");
639 * e_source_authentication_get_proxy_uid:
640 * @extension: an #ESourceAuthentication
642 * Returns the #ESource:uid of the #ESource that holds network proxy
643 * settings for use when connecting to a remote account.
645 * Returns: the proxy profile #ESource:uid
650 e_source_authentication_get_proxy_uid (ESourceAuthentication *extension)
652 g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), NULL);
654 return extension->priv->proxy_uid;
658 * e_source_authentication_dup_proxy_uid:
659 * @extension: an #ESourceAuthentication
661 * Thread-safe variation of e_source_authentication_get_proxy_uid().
662 * Use this function when accessing @extension from multiple threads.
664 * The returned string should be freed with g_free() when no longer needed.
666 * Returns: a newly-allocated copy of #ESourceAuthentication:proxy-uid
671 e_source_authentication_dup_proxy_uid (ESourceAuthentication *extension)
673 const gchar *protected;
676 g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), NULL);
678 g_mutex_lock (&extension->priv->property_lock);
680 protected = e_source_authentication_get_proxy_uid (extension);
681 duplicate = g_strdup (protected);
683 g_mutex_unlock (&extension->priv->property_lock);
689 * e_source_authentication_set_proxy_uid:
690 * @extension: an #ESourceAuthentication
691 * @proxy_uid: the proxy profile #ESource:uid
693 * Sets the #ESource:uid of the #ESource that holds network proxy settings
694 * for use when connecting to a remote account.
699 e_source_authentication_set_proxy_uid (ESourceAuthentication *extension,
700 const gchar *proxy_uid)
702 g_return_if_fail (E_IS_SOURCE_AUTHENTICATION (extension));
703 g_return_if_fail (proxy_uid != NULL);
705 g_mutex_lock (&extension->priv->property_lock);
707 if (g_strcmp0 (proxy_uid, extension->priv->proxy_uid) == 0) {
708 g_mutex_unlock (&extension->priv->property_lock);
712 g_free (extension->priv->proxy_uid);
713 extension->priv->proxy_uid = g_strdup (proxy_uid);
715 g_mutex_unlock (&extension->priv->property_lock);
717 g_object_notify (G_OBJECT (extension), "proxy-uid");
721 * e_source_authentication_get_remember_password:
722 * @extension: an #ESourceAuthentication
724 * Returns whether to offer to remember the provided password by default
725 * in password prompts. This way, if the user unchecks the option it will
726 * be unchecked by default in future password prompts.
728 * Returns: whether to offer to remember the password by default
733 e_source_authentication_get_remember_password (ESourceAuthentication *extension)
735 g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), FALSE);
737 return extension->priv->remember_password;
741 * e_source_authentication_set_remember_password:
742 * @extension: an #ESourceAuthentication
743 * @remember_password: whether to offer to remember the password by default
745 * Sets whether to offer to remember the provided password by default in
746 * password prompts. This way, if the user unchecks the option it will be
747 * unchecked by default in future password prompts.
752 e_source_authentication_set_remember_password (ESourceAuthentication *extension,
753 gboolean remember_password)
755 g_return_if_fail (E_IS_SOURCE_AUTHENTICATION (extension));
757 if (extension->priv->remember_password == remember_password)
760 extension->priv->remember_password = remember_password;
762 g_object_notify (G_OBJECT (extension), "remember-password");
766 * e_source_authentication_get_user:
767 * @extension: an #ESourceAuthentication
769 * Returns the user name used to authenticate to a remote account.
771 * Returns: the user name of a remote account
776 e_source_authentication_get_user (ESourceAuthentication *extension)
778 g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), NULL);
780 return extension->priv->user;
784 * e_source_authentication_dup_user:
785 * @extension: an #ESourceAuthentication
787 * Thread-safe variation of e_source_authentication_get_user().
788 * Use this function when accessing @extension from multiple threads.
790 * The returned string should be freed with g_free() when no longer needed.
792 * Returns: a newly-allocated copy of #ESourceAuthentication:user
797 e_source_authentication_dup_user (ESourceAuthentication *extension)
799 const gchar *protected;
802 g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), NULL);
804 g_mutex_lock (&extension->priv->property_lock);
806 protected = e_source_authentication_get_user (extension);
807 duplicate = g_strdup (protected);
809 g_mutex_unlock (&extension->priv->property_lock);
815 * e_source_authentication_set_user:
816 * @extension: an #ESourceAuthentication
817 * @user: (allow-none): a user name, or %NULL
819 * Sets the user name used to authenticate to a remote account.
821 * The internal copy of @user is automatically stripped of leading and
822 * trailing whitespace. If the resulting string is empty, %NULL is set
828 e_source_authentication_set_user (ESourceAuthentication *extension,
831 g_return_if_fail (E_IS_SOURCE_AUTHENTICATION (extension));
833 g_mutex_lock (&extension->priv->property_lock);
835 if (g_strcmp0 (extension->priv->user, user) == 0) {
836 g_mutex_unlock (&extension->priv->property_lock);
840 g_free (extension->priv->user);
841 extension->priv->user = e_util_strdup_strip (user);
843 g_mutex_unlock (&extension->priv->property_lock);
845 g_object_notify (G_OBJECT (extension), "user");