[evolution-kolab/ek-wip-porting] CamelKolabSession: fixed error handling in authentication



commit bfe0947c1f72e7fcd6b83d38c5d545be4eac4277
Author: Christian Hilberg <hilberg kernelconcepts de>
Date:   Mon Sep 10 15:37:19 2012 +0200

    CamelKolabSession: fixed error handling in authentication
    
    * avoid layering of GError instances in case of
      SASL auth failure
    * use a local GError and propagate it if set
    * do our default function arg checks in
      kolab_session_authenticate_sync()
    * let no local variable go uninitialized

 src/libekolab/camel-kolab-session.c |   79 ++++++++++++++++++++++++-----------
 1 files changed, 55 insertions(+), 24 deletions(-)
---
diff --git a/src/libekolab/camel-kolab-session.c b/src/libekolab/camel-kolab-session.c
index ac3b14e..e588142 100644
--- a/src/libekolab/camel-kolab-session.c
+++ b/src/libekolab/camel-kolab-session.c
@@ -190,14 +190,20 @@ kolab_session_authenticate_sync (CamelSession *session,
                                  GCancellable *cancellable,
                                  GError **error)
 {
-	CamelKolabSession *kolab_session;
-	ESourceAuthenticator *auth;
+	CamelKolabSession *kolab_session = NULL;
+	ESourceAuthenticator *auth = NULL;
 	CamelServiceAuthType *authtype = NULL;
-	CamelAuthenticationResult result;
-	EBackend *backend;
-	gboolean authenticated;
+	CamelAuthenticationResult result = CAMEL_AUTHENTICATION_ERROR;
+	EBackend *backend = NULL;
+	gboolean authenticated = FALSE;
 	GError *local_error = NULL;
 
+	g_return_val_if_fail (CAMEL_IS_SESSION (session), FALSE);
+	g_return_val_if_fail (CAMEL_IS_SERVICE (service), FALSE);
+	/* mechanism may be NULL */
+	/* cancellable may be NULL */
+	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
 	/* this is a dupe of mail_session_authenticate_sync()
 	 * from e-mail-session.c in evolution's libemail-engine.
 	 *
@@ -220,14 +226,23 @@ kolab_session_authenticate_sync (CamelSession *session,
 
 	/* If the SASL mechanism does not involve a user
 	 * password, then it gets one shot to authenticate. */
-	if (authtype != NULL && !authtype->need_password) {
-		result = camel_service_authenticate_sync (
-			service, mechanism, cancellable, error);
-		if (result == CAMEL_AUTHENTICATION_REJECTED)
-			g_set_error (
-				error, CAMEL_SERVICE_ERROR,
-				CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
-				_("%s authentication failed"), mechanism);
+	if ((authtype != NULL) && (!authtype->need_password)) {
+		result = camel_service_authenticate_sync (service,
+		                                          mechanism,
+		                                          cancellable,
+		                                          &local_error);
+		if (local_error != NULL) {
+			g_propagate_error (error, local_error);
+			return FALSE;
+		}
+		if (result == CAMEL_AUTHENTICATION_REJECTED) {
+			g_set_error (error,
+			             CAMEL_SERVICE_ERROR,
+			             CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
+			             _("%s authentication failed"),
+			             mechanism);
+			return FALSE;
+		}
 		return (result == CAMEL_AUTHENTICATION_ACCEPTED);
 	}
 
@@ -235,9 +250,9 @@ kolab_session_authenticate_sync (CamelSession *session,
 	 * user password being provided (e.g. single-sign-on credentials),
 	 * but can fall back to a user password.  Handle that case next. */
 	if (mechanism != NULL) {
-		CamelProvider *provider;
-		CamelSasl *sasl;
-		const gchar *service_name;
+		CamelProvider *provider = NULL;
+		CamelSasl *sasl = NULL;
+		const gchar *service_name = NULL;
 		gboolean success = FALSE;
 
 		provider = camel_service_get_provider (service);
@@ -248,13 +263,20 @@ kolab_session_authenticate_sync (CamelSession *session,
 		 *     detect errors. */
 		sasl = camel_sasl_new (service_name, mechanism, service);
 		if (sasl != NULL) {
-			success = camel_sasl_try_empty_password_sync (
-				sasl, cancellable, &local_error);
+			success = camel_sasl_try_empty_password_sync (sasl,
+			                                              cancellable,
+			                                              &local_error);
 			g_object_unref (sasl);
 		}
 
-		if (success)
+		if (success) {
+			if (local_error != NULL) {
+				g_warning ("%s()[%u] Success but GError set: %s",
+				           __func__, __LINE__, local_error->message);
+				g_clear_error (&local_error);
+			}
 			return TRUE;
+		}
 	}
 
 	/* Abort authentication if we got cancelled.
@@ -264,20 +286,29 @@ kolab_session_authenticate_sync (CamelSession *session,
 		return FALSE;
 	}
 
-	g_clear_error (&local_error);
+	if (local_error != NULL) {
+		g_warning ("%s()[%u]: %s", __func__, __LINE__, local_error->message);
+		g_clear_error (&local_error);
+		local_error = NULL;
+	}
 
 	kolab_session = CAMEL_KOLAB_SESSION (session);
 	backend = camel_kolab_session_ref_backend (kolab_session);
 
 	auth = e_mail_authenticator_new (service, mechanism);
 
-	authenticated = e_backend_authenticate_sync (
-		backend, auth, cancellable, error);
-
+	authenticated = e_backend_authenticate_sync (backend,
+	                                             auth,
+	                                             cancellable,
+	                                             &local_error);
 	g_object_unref (auth);
-
 	g_object_unref (backend);
 
+	if (local_error != NULL) {
+		g_propagate_error (error, local_error);
+		authenticated = FALSE;
+	}
+
 	return authenticated;
 }
 



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