[evolution-ews] Add camel_ews_settings_get_auth_mechanism() helper function



commit 4292fe89dee27d966fdce63849304f062a2a489c
Author: David Woodhouse <David Woodhouse intel com>
Date:   Thu Jul 24 11:00:53 2014 +0100

    Add camel_ews_settings_get_auth_mechanism() helper function
    
    The auth-mechanism string is not fun to deal with. It can sometimes
    be NULL and sometimes an empty string for NTLM, and I've managed to
    screw up even the *simple* cases, comparing for "BASIC" and then "Basic"
    in subsequent broken commits (b6ff74a4, 377da623) instead of "PLAIN".
    
    Make it nice and simple. Hard-code this knowledge about the strings in
    *one* place, camel_ews_settings_get_auth_mechanism(), and return an
    enum. Which in turn will help us to ensure we handle all cases, in
    places where that's appropriate (such as in
    e_ews_connection_utils_get_without_password())

 src/server/camel-ews-settings.c     |   37 +++++++++++++++++++++++++++++++++++
 src/server/camel-ews-settings.h     |    9 ++++++++
 src/server/e-ews-connection-utils.c |   23 ++++++++++++---------
 src/server/e-ews-connection.c       |   24 ++++++++--------------
 src/server/e-ews-notification.c     |   19 ++++++-----------
 5 files changed, 75 insertions(+), 37 deletions(-)
---
diff --git a/src/server/camel-ews-settings.c b/src/server/camel-ews-settings.c
index 714fca8..35146e5 100644
--- a/src/server/camel-ews-settings.c
+++ b/src/server/camel-ews-settings.c
@@ -621,6 +621,43 @@ camel_ews_settings_init (CamelEwsSettings *settings)
 }
 
 /**
+ * camel_ews_settings_get_auth_mechanism:
+ * @settings: a #CamelEwsSettings
+ *
+ * Returns a #EwsAuthType enum value indicating which authentication
+ * method to use.
+ *
+ * Returns: authentication method to use for this account
+ *
+ * Since: 3.14
+ **/
+EwsAuthType
+camel_ews_settings_get_auth_mechanism (CamelEwsSettings *settings)
+{
+       EwsAuthType result;
+       gchar *auth_mech = NULL;
+
+       g_return_val_if_fail (CAMEL_IS_EWS_SETTINGS (settings), FALSE);
+
+       g_object_get (G_OBJECT (settings), "auth-mechanism", &auth_mech, NULL);
+
+       /* The value for "NTLM" is a special case. Sometimes it's NULL,
+        * and sometimes it's "". But never "NTLM". No, that would be too
+        * simple. (I think it's for backward-compatibility with old
+        * profiles, so they default to NTLM). */
+       if (auth_mech && g_ascii_strcasecmp (auth_mech, "PLAIN") == 0)
+               result = EWS_AUTH_TYPE_BASIC;
+       else if (auth_mech && g_ascii_strcasecmp (auth_mech, "GSSAPI") == 0)
+               result = EWS_AUTH_TYPE_GSSAPI;
+       else
+               result = EWS_AUTH_TYPE_NTLM;
+
+       g_free (auth_mech);
+
+       return result;
+}
+
+/**
  * camel_ews_settings_get_check_all:
  * @settings: a #CamelEwsSettings
  *
diff --git a/src/server/camel-ews-settings.h b/src/server/camel-ews-settings.h
index acb1751..77ce750 100644
--- a/src/server/camel-ews-settings.h
+++ b/src/server/camel-ews-settings.h
@@ -55,7 +55,16 @@ struct _CamelEwsSettingsClass {
        CamelOfflineSettingsClass parent_class;
 };
 
+typedef enum {
+       EWS_AUTH_TYPE_NTLM,
+       EWS_AUTH_TYPE_BASIC,
+       EWS_AUTH_TYPE_GSSAPI
+} EwsAuthType;
+
+
 GType          camel_ews_settings_get_type     (void) G_GNUC_CONST;
+EwsAuthType    camel_ews_settings_get_auth_mechanism
+                                               (CamelEwsSettings *settings);
 gboolean       camel_ews_settings_get_check_all
                                                (CamelEwsSettings *settings);
 void           camel_ews_settings_set_check_all
diff --git a/src/server/e-ews-connection-utils.c b/src/server/e-ews-connection-utils.c
index 5d673b9..d59975c 100644
--- a/src/server/e-ews-connection-utils.c
+++ b/src/server/e-ews-connection-utils.c
@@ -26,6 +26,7 @@
 #include <glib/gstdio.h>
 
 #include "e-ews-connection-utils.h"
+#include "camel-ews-settings.h"
 
 #define EWS_GSSAPI_SOUP_SESSION "ews-gssapi-soup-session"
 #define EWS_GSSAPI_SASL                "ews-gssapi-sasl"
@@ -315,18 +316,20 @@ ews_connect_check_ntlm_available (void)
 gboolean
 e_ews_connection_utils_get_without_password (CamelEwsSettings *ews_settings)
 {
-       gboolean result = FALSE;
-       gchar *auth_mech = NULL;
+       switch (camel_ews_settings_get_auth_mechanism (ews_settings)) {
+       case EWS_AUTH_TYPE_GSSAPI:
+               return TRUE;
 
-       g_object_get (G_OBJECT (ews_settings), "auth-mechanism", &auth_mech,
-                     NULL);
+       case EWS_AUTH_TYPE_NTLM:
+               return ews_connect_check_ntlm_available ();
 
-       if (g_strcmp0 (auth_mech, "GSSAPI") == 0)
-               result = TRUE;
-       else if (g_strcmp0 (auth_mech, "PLAIN") != 0)
-               result = ews_connect_check_ntlm_available ();
+       case EWS_AUTH_TYPE_BASIC:
+               return FALSE;
 
-       g_free (auth_mech);
+       /* No default: case (which should never be used anyway). That
+        * means the compiler will warn if we ever add a new mechanism
+        * to the enum and don't handle it here. */
+       }
 
-       return result;
+       return FALSE;
 }
diff --git a/src/server/e-ews-connection.c b/src/server/e-ews-connection.c
index 96757c5..d5c9ab7 100644
--- a/src/server/e-ews-connection.c
+++ b/src/server/e-ews-connection.c
@@ -659,14 +659,12 @@ ews_next_request (gpointer _cnc)
        if (cnc->priv->soup_session) {
                SoupMessage *msg = SOUP_MESSAGE (node->msg);
                CamelEwsSettings *ews_settings = e_ews_connection_ref_settings (cnc);
-               gchar *auth_mech = NULL;
 
-               g_object_get (G_OBJECT (ews_settings), "auth-mechanism", &auth_mech, NULL);
-               if (g_strcmp0 (auth_mech, "GSSAPI") == 0)
+               if (camel_ews_settings_get_auth_mechanism (ews_settings) ==
+                   EWS_AUTH_TYPE_GSSAPI)
                        e_ews_connection_utils_setup_msg_gssapi_auth (cnc, cnc->priv->soup_session, msg);
 
                g_object_unref (ews_settings);
-               g_free (auth_mech);
 
                soup_session_queue_message (cnc->priv->soup_session, msg, ews_response_cb, node);
                QUEUE_UNLOCK (cnc);
@@ -1654,25 +1652,21 @@ ews_connection_constructor (GType gtype, guint n_properties,
 {
        GObject *obj = G_OBJECT_CLASS (e_ews_connection_parent_class)->
                constructor (gtype, n_properties, properties);
-       EEwsConnection *cnc = E_EWS_CONNECTION (obj);
-       CamelEwsSettings *ews_settings = e_ews_connection_ref_settings (cnc);
-       gchar *auth_mech = NULL;
+       EEwsConnectionPrivate *priv = E_EWS_CONNECTION_GET_PRIVATE (obj);
+       EwsAuthType mech;
 
-       g_object_get (G_OBJECT (ews_settings), "auth-mechanism", &auth_mech,
-                     NULL);
+       mech = camel_ews_settings_get_auth_mechanism (priv->settings);
 
        /* We need to disable Basic auth to avoid it getting in the way of
         * our GSSAPI hacks. But leave it enabled in the case where NTLM is
         * enabled, which is the default configuration. It's a useful fallback
         * which people may be relying on. */
-       if (g_strcmp0 (auth_mech, "GSSAPI") == 0)
-               soup_session_remove_feature_by_type (cnc->priv->soup_session,
+       if (mech == EWS_AUTH_TYPE_GSSAPI)
+               soup_session_remove_feature_by_type (priv->soup_session,
                                                     SOUP_TYPE_AUTH_BASIC);
-       else if (g_strcmp0 (auth_mech, "PLAIN") != 0) /* NTLM */
-               soup_session_add_feature_by_type (cnc->priv->soup_session,
+       else if (mech == EWS_AUTH_TYPE_NTLM)
+               soup_session_add_feature_by_type (priv->soup_session,
                                                  SOUP_TYPE_AUTH_NTLM);
-       g_free (auth_mech);
-       g_object_unref(ews_settings);
 
        return obj;
 }
diff --git a/src/server/e-ews-notification.c b/src/server/e-ews-notification.c
index b4d0696..6571c97 100644
--- a/src/server/e-ews-notification.c
+++ b/src/server/e-ews-notification.c
@@ -226,27 +226,24 @@ ews_notification_constructor (GType gtype, guint n_properties,
                constructor (gtype, n_properties, properties);
        EEwsNotificationPrivate *priv;
        CamelEwsSettings *ews_settings;
-       gchar *auth_mech = NULL;
+       EwsAuthType mech;
 
        priv = E_EWS_NOTIFICATION_GET_PRIVATE (obj);
-
        ews_settings = e_ews_connection_ref_settings (priv->connection);
+       mech = camel_ews_settings_get_auth_mechanism (ews_settings);
 
-       g_object_get (G_OBJECT (ews_settings), "auth-mechanism", &auth_mech,
-                     NULL);
+       g_object_unref (ews_settings);
 
        /* We need to disable Basic auth to avoid it getting in the way of
         * our GSSAPI hacks. But leave it enabled in the case where NTLM is
         * enabled, which is the default configuration. It's a useful fallback
         * which people may be relying on. */
-       if (g_strcmp0 (auth_mech, "GSSAPI") == 0)
+       if (mech == EWS_AUTH_TYPE_GSSAPI)
                soup_session_remove_feature_by_type (priv->soup_session,
                                                     SOUP_TYPE_AUTH_BASIC);
-       else if (g_strcmp0 (auth_mech, "PLAIN") != 0) /* NTLM */
+       else if (mech == EWS_AUTH_TYPE_NTLM)
                soup_session_add_feature_by_type (priv->soup_session,
                                                  SOUP_TYPE_AUTH_NTLM);
-       g_free (auth_mech);
-       g_object_unref(ews_settings);
 
        return obj;
 }
@@ -313,7 +310,6 @@ e_ews_notification_subscribe_folder_sync (EEwsNotification *notification,
        ESoapParameter *param, *subparam;
        GError *error = NULL;
        GSList *l;
-       gchar *auth_mech = NULL;
        guint event_type;
        xmlDoc *doc;
 
@@ -367,15 +363,14 @@ e_ews_notification_subscribe_folder_sync (EEwsNotification *notification,
 
        ews_settings = e_ews_connection_ref_settings (notification->priv->connection);
 
-       g_object_get (G_OBJECT (ews_settings), "auth-mechanism", &auth_mech, NULL);
-       if (g_strcmp0 (auth_mech, "GSSAPI") == 0)
+       if (camel_ews_settings_get_auth_mechanism (ews_settings) ==
+           EWS_AUTH_TYPE_GSSAPI)
                e_ews_connection_utils_setup_msg_gssapi_auth (
                        notification->priv->connection,
                        notification->priv->soup_session,
                        SOUP_MESSAGE (msg));
 
        g_object_unref (ews_settings);
-       g_free (auth_mech);
 
        if (g_cancellable_is_cancelled (cancellable)) {
                g_object_unref (msg);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]