Commit 14391b6c authored by Kévin Commaille's avatar Kévin Commaille
Browse files

Add support for GNOME Online Account

Moves etesync_config_lookup_discover to e_etesync_util_check_credentials to make it available in libevolution-etesync

Fixes #2
Related to GNOME/gnome-online-accounts#152
parent 4debb2af
......@@ -58,6 +58,7 @@ EEteSyncConnection *
e_etesync_connection_new (ESource *collection_source)
{
EEteSyncConnection *connection;
ESourceCollection *collection_extension = NULL;
gchar *hash_key;
const gchar *username = NULL, *server_url = NULL;
......@@ -65,19 +66,21 @@ e_etesync_connection_new (ESource *collection_source)
g_return_val_if_fail (E_IS_SOURCE (collection_source), NULL);
else
return g_object_new (E_TYPE_ETESYNC_CONNECTION, NULL);
collection_extension = e_source_get_extension (collection_source, E_SOURCE_EXTENSION_COLLECTION);
  • This means when the source does not contain the extension, it is added. Maybe there was a reason why the code checked for the extension existence before your change.

  • I missed the part in the docs where it said it's created if it doesn't exist. However I'm not sure why it wouldn't exist since all EteSync accounts should be collection accounts.

  • Well, I do not know why the check was there, but I suppose it was for some purpose, as everywhere in the code.

Please register or sign in to reply
if (e_source_has_extension (collection_source, E_SOURCE_EXTENSION_COLLECTION)) {
ESourceCollection *collection_extension;
if (e_source_has_extension (collection_source, E_SOURCE_EXTENSION_GOA)) {
ESourceGoa *goa_extension;
collection_extension = e_source_get_extension (collection_source, E_SOURCE_EXTENSION_COLLECTION);
server_url = e_source_collection_get_calendar_url (collection_extension);
  • Not reading the URL from here means breaking things for non-GOA configured EteSync accounts.

  • This is the part for GOA accounts, this line of code has been moved to L83 for non-GOA accounts

  • Ah, I missed the line there. The logic is different too, it used to be "read server URL when collection account and user name when authentication extension exists", but right now, it's "read both is authentication exists". I guess your change is fine.

Please register or sign in to reply
}
if (e_source_has_extension (collection_source, E_SOURCE_EXTENSION_AUTHENTICATION)) {
goa_extension = e_source_get_extension (collection_source, E_SOURCE_EXTENSION_GOA);
username = e_source_collection_get_identity (collection_extension);
server_url = e_source_goa_get_calendar_url (goa_extension);
} else if (e_source_has_extension (collection_source, E_SOURCE_EXTENSION_AUTHENTICATION)) {
Please register or sign in to reply
ESourceAuthentication *authentication_extension;
authentication_extension = e_source_get_extension (collection_source, E_SOURCE_EXTENSION_AUTHENTICATION);
username = e_source_authentication_get_user (authentication_extension);
server_url = e_source_collection_get_calendar_url (collection_extension);
}
g_return_val_if_fail (username != NULL, NULL);
......@@ -176,18 +179,27 @@ e_etesync_connection_set_connection_from_sources (EEteSyncConnection *connection
{
const gchar *server_url , *session_key;
gboolean success = TRUE;
ESourceCollection *collection_extension;
g_return_val_if_fail (connection != NULL ,FALSE);
g_rec_mutex_lock (&connection->priv->connection_lock);
collection_extension = e_source_get_extension (connection->priv->collection_source, E_SOURCE_EXTENSION_COLLECTION);
e_etesync_connection_clear (connection);
g_rec_mutex_lock (&connection->priv->connection_lock);
Please register or sign in to reply
/* 1) get_server from ESourceAuthentication as it was saved when the user was entering credentials in the dialog first time.
Set the etebase_client then for this backend so as long as the backend is alive, we don't have to do this process again */
server_url = e_source_collection_get_calendar_url (collection_extension);
if (e_source_has_extension (connection->priv->collection_source, E_SOURCE_EXTENSION_GOA)) {
ESourceGoa *goa_extension;
goa_extension = e_source_get_extension (connection->priv->collection_source, E_SOURCE_EXTENSION_GOA);
server_url = e_source_goa_get_calendar_url (goa_extension);
} else {
ESourceCollection *collection_extension;
collection_extension = e_source_get_extension (connection->priv->collection_source, E_SOURCE_EXTENSION_COLLECTION);
server_url = e_source_collection_get_calendar_url (collection_extension);
}
e_etesync_connection_clear (connection);
connection->priv->etebase_client = etebase_client_new (PACKAGE "/" VERSION, server_url);
/* problem with the server_url */
......@@ -360,7 +372,7 @@ e_etesync_connection_reconnect_sync (EEteSyncConnection *connection,
if (e_etesync_connection_is_connected (connection)) {
const gchar *session_key = e_named_parameters_get (credentials, E_ETESYNC_CREDENTIAL_SESSION_KEY);
Please register or sign in to reply
if (session_key) {
if (g_strcmp0 (session_key, connection->priv->session_key) == 0) {
if (e_etesync_connection_check_session_key_validation_sync (connection, NULL, error) == E_SOURCE_AUTHENTICATION_REJECTED) {
......
......@@ -7,10 +7,13 @@
#include "evolution-etesync-config.h"
#include <glib/gi18n-lib.h>
#include <libedata-book/libedata-book.h>
#include <libedata-cal/libedata-cal.h>
#include "e-etesync-defines.h"
#include "e-etesync-utils.h"
#include "e-etesync-connection.h"
#include <e-util/e-util.h>
static const gchar *const collection_supported_types[] = {
E_ETESYNC_COLLECTION_TYPE_ADDRESS_BOOK,
......@@ -191,4 +194,79 @@ const gchar *const *
e_etesync_util_get_collection_supported_types_default_names (void)
{
return collection_supported_types_default_names;
}
\ No newline at end of file
}
gboolean
e_etesync_util_check_credentials (const gchar *username,
const gchar *password,
const gchar *server_url,
GError **error)
{
gboolean success = TRUE;
const gchar *default_server_url;
EtebaseClient *etebase_client;
g_return_val_if_fail (username != NULL && username[0] != '\0', FALSE);
default_server_url = etebase_get_default_server_url ();
if (!server_url || !*server_url)
server_url = default_server_url;
etebase_client = etebase_client_new (PACKAGE "/" VERSION, server_url);
if (etebase_client) {
gint32 etebase_server_check = 0;
if (!g_str_equal (server_url, default_server_url))
etebase_server_check = etebase_client_check_etebase_server (etebase_client);
/* Returns 0 if client is pointing an etebase server, 1 if not, -1 on error */
if (etebase_server_check != 0) {
if (etebase_server_check == 1)
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Etebase server not found."));
else
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Failed connecting to server."));
success = FALSE;
} else if (password == NULL || username[0] == '\0') {
g_set_error_literal (error, E_CONFIG_LOOKUP_WORKER_ERROR, E_CONFIG_LOOKUP_WORKER_ERROR_REQUIRES_PASSWORD,
_("Requires password to continue."));
success = FALSE;
} else {
EtebaseErrorCode etebase_error;
EEteSyncConnection *connection;
connection = e_etesync_connection_new (NULL);
if (e_etesync_connection_login_connection_sync (connection, username, password, server_url, &etebase_error)) {
/* The connection was successfully set, but need to check permession denied error using token valid */
if (e_etesync_connection_check_session_key_validation_sync (connection, &etebase_error, NULL) != E_SOURCE_AUTHENTICATION_ACCEPTED)
success = FALSE;
etebase_account_logout (e_etesync_connection_get_etebase_account (connection));
} else
success = FALSE;
if (!success) {
if (etebase_error == ETEBASE_ERROR_CODE_UNAUTHORIZED)
g_set_error_literal (error, E_CONFIG_LOOKUP_WORKER_ERROR, E_CONFIG_LOOKUP_WORKER_ERROR_REQUIRES_PASSWORD, etebase_error_get_message ());
else
e_etesync_utils_set_io_gerror (etebase_error, etebase_error_get_message (), error);
}
g_object_unref (connection);
}
etebase_client_destroy (etebase_client);
} else {
if (server_url) {
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
_("Malformed server name, please make sure to enter a full url (e.g https://etesync.example.com)."));
}
success = FALSE;
}
return success;
}
......@@ -44,6 +44,11 @@ const gchar *const *
e_etesync_util_get_collection_supported_types (void);
const gchar *const *
e_etesync_util_get_collection_supported_types_default_names (void);
gboolean e_etesync_util_check_credentials
(const gchar *username,
const gchar *password,
const gchar *server_url,
GError **error);
G_END_DECLS
#endif /* E_ETESYNC_UTILS_H */
......@@ -108,86 +108,6 @@ etesync_config_lookup_worker_result (EConfigLookupWorker *lookup_worker,
e_config_lookup_add_result (config_lookup, lookup_result);
}
static gboolean
etesync_config_lookup_discover (const ENamedParameters *params,
GError **error)
{
gboolean success = TRUE;
const gchar *email_address, *server_url, *default_server_url;
EtebaseClient *etebase_client;
email_address = e_named_parameters_get (params, E_CONFIG_LOOKUP_PARAM_EMAIL_ADDRESS);
server_url = e_named_parameters_get (params, E_CONFIG_LOOKUP_PARAM_SERVERS);
default_server_url = etebase_get_default_server_url ();
if (!email_address)
return FALSE;
if (!server_url || !*server_url)
server_url = default_server_url;
etebase_client = etebase_client_new (PACKAGE "/" VERSION, server_url);
if (etebase_client) {
gint32 etebase_server_check = 0;
if (!g_str_equal (server_url, default_server_url))
etebase_server_check = etebase_client_check_etebase_server (etebase_client);
/* Returns 0 if client is pointing an etebase server, 1 if not, -1 on error */
if (etebase_server_check != 0) {
if (etebase_server_check == 1)
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Etebase server not found."));
else
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Failed connecting to server."));
success = FALSE;
} else if (!e_named_parameters_exists (params, E_CONFIG_LOOKUP_PARAM_PASSWORD)) {
g_set_error_literal (error, E_CONFIG_LOOKUP_WORKER_ERROR, E_CONFIG_LOOKUP_WORKER_ERROR_REQUIRES_PASSWORD,
_("Requires password to continue."));
success = FALSE;
} else {
EtebaseErrorCode etebase_error;
EEteSyncConnection *connection;
const gchar *password;
connection = e_etesync_connection_new (NULL);
password = e_named_parameters_get (params, E_CONFIG_LOOKUP_PARAM_PASSWORD);
if (e_etesync_connection_login_connection_sync (connection, email_address, password, server_url, &etebase_error)) {
/* The connection was successfully set, but need to check permession denied error using token valid */
if (e_etesync_connection_check_session_key_validation_sync (connection, &etebase_error, NULL) != E_SOURCE_AUTHENTICATION_ACCEPTED)
success = FALSE;
etebase_account_logout (e_etesync_connection_get_etebase_account (connection));
} else
success = FALSE;
if (!success) {
if (etebase_error == ETEBASE_ERROR_CODE_UNAUTHORIZED)
g_set_error_literal (error, E_CONFIG_LOOKUP_WORKER_ERROR, E_CONFIG_LOOKUP_WORKER_ERROR_REQUIRES_PASSWORD, etebase_error_get_message ());
else
e_etesync_utils_set_io_gerror (etebase_error, etebase_error_get_message (), error);
}
g_object_unref (connection);
}
etebase_client_destroy (etebase_client);
} else {
if (server_url) {
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
_("Malformed server name, please make sure to enter a full url (e.g https://etesync.example.com)."));
}
success = FALSE;
}
return success;
}
static void
etesync_config_lookup_worker_run (EConfigLookupWorker *lookup_worker,
EConfigLookup *config_lookup,
......@@ -196,20 +116,22 @@ etesync_config_lookup_worker_run (EConfigLookupWorker *lookup_worker,
GCancellable *cancellable,
GError **error)
{
const gchar *email_address;
const gchar *email_address, *password, *server_url;
g_return_if_fail (E_IS_ETESYNC_CONFIG_LOOKUP (lookup_worker));
g_return_if_fail (E_IS_CONFIG_LOOKUP (config_lookup));
g_return_if_fail (params != NULL);
email_address = e_named_parameters_get (params, E_CONFIG_LOOKUP_PARAM_EMAIL_ADDRESS);
password = e_named_parameters_get (params, E_CONFIG_LOOKUP_PARAM_PASSWORD);
server_url = e_named_parameters_get (params, E_CONFIG_LOOKUP_PARAM_SERVERS);
if (!email_address || !*email_address)
return;
if (etesync_config_lookup_discover (params, error))
if (e_etesync_util_check_credentials (email_address, password, server_url, error))
etesync_config_lookup_worker_result (lookup_worker, config_lookup, email_address, params);
if (out_restart_params && !*out_restart_params)
*out_restart_params = e_named_parameters_new_clone (params);
}
......
......@@ -101,11 +101,13 @@ etesync_backend_populate (ECollectionBackend *backend)
if (e_source_has_extension (source, E_SOURCE_EXTENSION_AUTHENTICATION)) {
ESourceAuthentication *auth_extension;
gchar *method, *user;
gboolean is_external;
auth_extension = e_source_get_extension (source, E_SOURCE_EXTENSION_AUTHENTICATION);
method = e_source_authentication_dup_method (auth_extension);
user = e_source_authentication_dup_user (auth_extension);
needs_credentials = user && *user && g_strcmp0 (method, "EteSync") != 0;
is_external = e_source_authentication_get_is_external(auth_extension);
needs_credentials = is_external || (user && *user && g_strcmp0 (method, "EteSync") != 0);
g_free (method);
g_free (user);
}
......@@ -718,10 +720,54 @@ etesync_backend_authenticate_sync (EBackend *backend,
if (!etesync_backend->priv->connection)
etesync_backend->priv->connection = e_etesync_connection_new (source);
if (e_etesync_connection_reconnect_sync (etesync_backend->priv->connection, &result, cancellable, error))
result = E_SOURCE_AUTHENTICATION_ACCEPTED;
else if (e_source_has_extension (source, E_SOURCE_EXTENSION_GOA)) {
ESourceCollection *collection_extension;
ESourceGoa *goa_extension;
const gchar *username;
const gchar *password;
const gchar *server_url;
EtebaseErrorCode etebase_error;
gboolean success = TRUE;
/* This is the first time a GOA account is authenticated, we need to store the session key */
goa_extension = e_source_get_extension (source, E_SOURCE_EXTENSION_GOA);
collection_extension = e_source_get_extension (source, E_SOURCE_EXTENSION_COLLECTION);
username = e_source_collection_get_identity (collection_extension);
password = e_named_parameters_get (credentials, E_SOURCE_CREDENTIAL_PASSWORD);
server_url = e_source_goa_get_calendar_url (goa_extension);
if (!server_url || !*server_url)
server_url = etebase_get_default_server_url ();
if(e_etesync_connection_login_connection_sync (etesync_backend->priv->connection, username, password, server_url, &etebase_error)) {
EtebaseAccount *etebase_account;
ENamedParameters *etesync_credentials;
gchar *session_key;
gchar *label;
const gchar *uid;
etebase_account = e_etesync_connection_get_etebase_account (etesync_backend->priv->connection);
session_key = etebase_account_save (etebase_account, NULL, 0);
label = e_source_dup_secret_label (source);
uid = e_source_get_uid (source);
etesync_credentials = e_named_parameters_new ();
e_named_parameters_set (etesync_credentials, E_ETESYNC_CREDENTIAL_SESSION_KEY, session_key);
if (e_etesync_service_store_credentials_sync (uid, label, etesync_credentials, TRUE, cancellable, error))
result = E_SOURCE_AUTHENTICATION_ACCEPTED;
e_named_parameters_free (etesync_credentials);
g_free(session_key);
g_free(label);
}
}
}
if (result == E_SOURCE_AUTHENTICATION_ACCEPTED) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment