[epiphany] Remember mailman passwords



commit e616f8e442e92d9d949a79a56bb8fa4b0434e9fe
Author: Bastien Nocera <hadess hadess net>
Date:   Thu Apr 25 18:43:21 2013 +0200

    Remember mailman passwords
    
    https://bugzilla.gnome.org/show_bug.cgi?id=698866

 embed/web-extension/ephy-embed-form-auth.c |    1 -
 embed/web-extension/ephy-web-extension.c   |   78 +++++++++++++++++----------
 lib/ephy-form-auth-data.c                  |   61 ++++++++++++++--------
 lib/ephy-web-dom-utils.c                   |   18 ++++++-
 4 files changed, 106 insertions(+), 52 deletions(-)
---
diff --git a/embed/web-extension/ephy-embed-form-auth.c b/embed/web-extension/ephy-embed-form-auth.c
index 3831220..669b4aa 100644
--- a/embed/web-extension/ephy-embed-form-auth.c
+++ b/embed/web-extension/ephy-embed-form-auth.c
@@ -68,7 +68,6 @@ ephy_embed_form_auth_new (WebKitWebPage *web_page,
 {
   EphyEmbedFormAuth *form_auth;
 
-  g_return_val_if_fail (WEBKIT_DOM_IS_NODE (username_node), NULL);
   g_return_val_if_fail (WEBKIT_DOM_IS_NODE (password_node), NULL);
 
   form_auth = EPHY_EMBED_FORM_AUTH (g_object_new (EPHY_TYPE_EMBED_FORM_AUTH, NULL));
diff --git a/embed/web-extension/ephy-web-extension.c b/embed/web-extension/ephy-web-extension.c
index a87139a..92c0ce2 100644
--- a/embed/web-extension/ephy-web-extension.c
+++ b/embed/web-extension/ephy-web-extension.c
@@ -142,11 +142,14 @@ store_password (EphyEmbedFormAuth *form_auth)
   char *username_field_value = NULL;
   char *password_field_name = NULL;
   char *password_field_value = NULL;
-
-  g_object_get (ephy_embed_form_auth_get_username_node (form_auth),
-                "name", &username_field_name,
-                "value", &username_field_value,
-                NULL);
+  WebKitDOMNode *username_node;
+
+  username_node = ephy_embed_form_auth_get_username_node (form_auth);
+  if (username_node)
+    g_object_get (username_node,
+                  "name", &username_field_name,
+                  "value", &username_field_value,
+                  NULL);
   g_object_get (ephy_embed_form_auth_get_password_node (form_auth),
                 "name", &password_field_name,
                 "value", &password_field_value,
@@ -182,6 +185,7 @@ request_decision_on_storing (EphyEmbedFormAuth *form_auth)
   guint request_id;
   SoupURI *uri;
   GError *error = NULL;
+  WebKitDOMNode *username_node;
 
   if (!dbus_connection) {
     g_object_unref (form_auth);
@@ -190,8 +194,9 @@ request_decision_on_storing (EphyEmbedFormAuth *form_auth)
 
   request_id = form_auth_data_save_request_new_id ();
   uri = ephy_embed_form_auth_get_uri (form_auth);
-  g_object_get (ephy_embed_form_auth_get_username_node (form_auth),
-                "value", &username_field_value, NULL);
+  username_node = ephy_embed_form_auth_get_username_node (form_auth);
+  if (username_node)
+    g_object_get (username_node, "value", &username_field_value, NULL);
 
   g_dbus_connection_emit_signal (dbus_connection,
                                  NULL,
@@ -224,18 +229,20 @@ should_store_cb (const char *username,
 {
   EphyEmbedFormAuth *form_auth = EPHY_EMBED_FORM_AUTH (user_data);
 
-  if (username && password) {
+  if (password) {
+    WebKitDOMNode *username_node;
     char *username_field_value = NULL;
     char *password_field_value = NULL;
 
-    g_object_get (ephy_embed_form_auth_get_username_node (form_auth),
-                  "value", &username_field_value, NULL);
+    username_node = ephy_embed_form_auth_get_username_node (form_auth);
+    if (username_node)
+      g_object_get (username_node, "value", &username_field_value, NULL);
     g_object_get (ephy_embed_form_auth_get_password_node (form_auth),
                   "value", &password_field_value, NULL);
 
     /* FIXME: We use only the first result, for now; We need to do
      * something smarter here */
-    if (g_str_equal (username, username_field_value) &&
+    if (g_strcmp0 (username, username_field_value) == 0 &&
         g_str_equal (password, password_field_value)) {
       LOG ("User/password already stored. Not asking about storing.");
     } else {
@@ -268,16 +275,19 @@ form_submitted_cb (WebKitDOMHTMLFormElement *dom_form,
   if (!ephy_web_dom_utils_find_form_auth_elements (dom_form, &username_node, &password_node))
     return TRUE;
 
-  g_object_get (username_node,
-                "value", &username_field_value,
-                NULL);
+  if (username_node) {
+    g_object_get (username_node,
+                  "value", &username_field_value,
+                  NULL);
+  }
 
   /* EphyEmbedFormAuth takes ownership of the nodes */
   form_auth = ephy_embed_form_auth_new (web_page, username_node, password_node, username_field_value);
   uri = ephy_embed_form_auth_get_uri (form_auth);
   soup_uri_set_query (uri, NULL);
 
-  g_object_get (username_node, "name", &username_field_name, NULL);
+  if (username_node)
+    g_object_get (username_node, "name", &username_field_name, NULL);
   g_object_get (password_node, "name", &password_field_name, NULL);
   uri_str = soup_uri_to_string (uri, FALSE);
 
@@ -303,15 +313,18 @@ fill_form_cb (const char *username,
               gpointer user_data)
 {
   EphyEmbedFormAuth *form_auth = EPHY_EMBED_FORM_AUTH (user_data);
+  WebKitDOMNode *username_node;
 
   if (username == NULL && password == NULL) {
     LOG ("No result");
     return;
   }
 
-  LOG ("Found: user %s pass (hidden)", username);
-  g_object_set (ephy_embed_form_auth_get_username_node (form_auth),
-                "value", username, NULL);
+  username_node = ephy_embed_form_auth_get_username_node (form_auth);
+
+  LOG ("Found: user %s pass (hidden)", username_node ? username : "(none)");
+  if (username_node)
+    g_object_set (username_node, "value", username, NULL);
   g_object_set (ephy_embed_form_auth_get_password_node (form_auth),
                 "value", password, NULL);
 }
@@ -320,12 +333,14 @@ static gint
 ephy_form_auth_data_compare (EphyFormAuthData *form_data,
                              EphyEmbedFormAuth *form_auth)
 {
-  char *username_field_name;
+  WebKitDOMNode *username_node;
+  char *username_field_name = NULL;
   char *password_field_name;
   gboolean retval;
 
-  g_object_get (ephy_embed_form_auth_get_username_node (form_auth),
-                "name", &username_field_name, NULL);
+  username_node = ephy_embed_form_auth_get_username_node (form_auth);
+  if (username_node)
+    g_object_get (username_node, "name", &username_field_name, NULL);
   g_object_get (ephy_embed_form_auth_get_password_node (form_auth),
                 "name", &password_field_name, NULL);
 
@@ -347,6 +362,7 @@ pre_fill_form (EphyEmbedFormAuth *form_auth)
   SoupURI *uri;
   char *uri_str;
   char *username;
+  WebKitDOMNode *username_node;
 
   uri = ephy_embed_form_auth_get_uri (form_auth);
   if (!uri)
@@ -360,12 +376,14 @@ pre_fill_form (EphyEmbedFormAuth *form_auth)
   form_data = (EphyFormAuthData *)l->data;
   uri_str = soup_uri_to_string (uri, FALSE);
 
-  g_object_get (ephy_embed_form_auth_get_username_node (form_auth),
-                "value", &username,
-                NULL);
+  username_node = ephy_embed_form_auth_get_username_node (form_auth);
+  if (username_node)
+    g_object_get (username_node, "value", &username, NULL);
+  else
+    username = NULL;
 
   /* The username node is empty, so pre-fill with the default. */
-  if (g_str_equal (username, ""))
+  if (username != NULL && g_str_equal (username, ""))
     g_clear_pointer (&username, g_free);
 
   ephy_form_auth_data_query (uri_str,
@@ -868,9 +886,11 @@ web_page_document_loaded (WebKitWebPage *web_page,
       webkit_dom_event_target_add_event_listener (WEBKIT_DOM_EVENT_TARGET (form), "submit",
                                                   G_CALLBACK (form_submitted_cb), FALSE,
                                                   web_page);
-      webkit_dom_event_target_add_event_listener (WEBKIT_DOM_EVENT_TARGET (username_node), "blur",
-                                                  G_CALLBACK (username_changed_cb), FALSE,
-                                                  form_auth);
+      if (username_node) {
+        webkit_dom_event_target_add_event_listener (WEBKIT_DOM_EVENT_TARGET (username_node), "blur",
+                                                    G_CALLBACK (username_changed_cb), FALSE,
+                                                    form_auth);
+      }
 
       /* Plug in the user autocomplete */
       uri_string = webkit_web_page_get_uri (web_page);
@@ -880,7 +900,7 @@ web_page_document_loaded (WebKitWebPage *web_page,
 
       soup_uri_free (uri);
 
-      if (auth_data_list && auth_data_list->next) {
+      if (auth_data_list && auth_data_list->next && username_node) {
         LOG ("More than 1 password saved, hooking menu for choosing which on focus");
         g_object_set_data (G_OBJECT (username_node), "ephy-auth-data-list", auth_data_list);
         g_object_set_data (G_OBJECT (username_node), "ephy-form-auth", form_auth);
diff --git a/lib/ephy-form-auth-data.c b/lib/ephy-form-auth-data.c
index 35a8a82..45c282c 100644
--- a/lib/ephy-form-auth-data.c
+++ b/lib/ephy-form-auth-data.c
@@ -44,7 +44,8 @@ ephy_form_auth_data_get_password_schema (void)
 }
 
 static void
-normalize_and_prepare_uri (SoupURI *uri)
+normalize_and_prepare_uri (SoupURI  *uri,
+                           gboolean  remove_path)
 {
   g_assert (uri != NULL);
 
@@ -55,7 +56,8 @@ normalize_and_prepare_uri (SoupURI *uri)
     soup_uri_set_scheme (uri, SOUP_URI_SCHEME_HTTP);
 
   soup_uri_set_query (uri, NULL);
-  soup_uri_set_path (uri, "/");
+  if (remove_path)
+    soup_uri_set_path (uri, "/");
 }
 
 static GHashTable *
@@ -64,12 +66,19 @@ ephy_form_auth_data_get_secret_attributes_table (const char *uri,
                                                  const char *field_password,
                                                  const char *username)
 {
-  return secret_attributes_build (EPHY_FORM_PASSWORD_SCHEMA,
-                                  URI_KEY, uri,
-                                  FORM_USERNAME_KEY, field_username,
-                                  FORM_PASSWORD_KEY, field_password,
-                                  username ? USERNAME_KEY : NULL, username,
-                                  NULL);
+  if (field_username)
+    return secret_attributes_build (EPHY_FORM_PASSWORD_SCHEMA,
+                                    URI_KEY, uri,
+                                    FORM_USERNAME_KEY, field_username,
+                                    FORM_PASSWORD_KEY, field_password,
+                                    username ? USERNAME_KEY : NULL, username,
+                                    NULL);
+  else
+    return secret_attributes_build (EPHY_FORM_PASSWORD_SCHEMA,
+                                    URI_KEY, uri,
+                                    FORM_PASSWORD_KEY, field_password,
+                                    username ? USERNAME_KEY : NULL, username,
+                                    NULL);
 }
 
 static void
@@ -104,27 +113,37 @@ ephy_form_auth_data_store (const char *uri,
   GSimpleAsyncResult *res;
 
   g_return_if_fail (uri);
-  g_return_if_fail (form_username);
   g_return_if_fail (form_password);
-  g_return_if_fail (username);
   g_return_if_fail (password);
+  g_return_if_fail ((form_username && username) || (!form_username && !username));
 
   fake_uri = soup_uri_new (uri);
   g_return_if_fail (fake_uri);
 
   res = g_simple_async_result_new (NULL, callback, userdata, ephy_form_auth_data_store);
 
-  normalize_and_prepare_uri (fake_uri);
+  /* Mailman passwords need the full URI */
+  if (!form_username && g_strcmp0 (form_password, "adminpw") == 0)
+    normalize_and_prepare_uri (fake_uri, FALSE);
+  else
+    normalize_and_prepare_uri (fake_uri, TRUE);
   fake_uri_str = soup_uri_to_string (fake_uri, FALSE);
   value = secret_value_new (password, -1, "text/plain");
   attributes = ephy_form_auth_data_get_secret_attributes_table (fake_uri_str, form_username,
                                                                 form_password, username);
-  /* Translators: The first %s is the username and the second one is the
-   * hostname where this is happening. Example: gnome gmail com and
-   * mail.google.com.
-   */
-  label = g_strdup_printf (_("Password for %s in a form in %s"),
-                           username, fake_uri_str);
+  if (username != NULL) {
+    /* Translators: The first %s is the username and the second one is the
+     * hostname where this is happening. Example: gnome gmail com and
+     * mail.google.com.
+     */
+    label = g_strdup_printf (_("Password for %s in a form in %s"),
+                             username, fake_uri_str);
+  } else {
+    /* Translators: The first %s is the hostname where this is happening.
+     * Example: mail.google.com.
+     */
+    label = g_strdup_printf (_("Password in a form in %s"), fake_uri_str);
+  }
   secret_service_store (NULL, EPHY_FORM_PASSWORD_SCHEMA,
                         attributes, NULL, label, value,
                         NULL,
@@ -223,13 +242,15 @@ ephy_form_auth_data_query (const char *uri,
   GHashTable *attributes;
 
   g_return_if_fail (uri);
-  g_return_if_fail (form_username);
   g_return_if_fail (form_password);
 
   key = soup_uri_new (uri);
   g_return_if_fail (key);
 
-  normalize_and_prepare_uri (key);
+  if (!form_username && g_strcmp0 (form_password, "adminpw") == 0)
+    normalize_and_prepare_uri (key, FALSE);
+  else
+    normalize_and_prepare_uri (key, TRUE);
 
   key_str = soup_uri_to_string (key, FALSE);
 
@@ -381,9 +402,7 @@ ephy_form_auth_data_cache_add (EphyFormAuthDataCache *cache,
 
   g_return_if_fail (cache);
   g_return_if_fail (uri);
-  g_return_if_fail (form_username);
   g_return_if_fail (form_password);
-  g_return_if_fail (username);
 
   data = ephy_form_auth_data_new (form_username, form_password, username);
   l = g_hash_table_lookup (cache->form_auth_data_map, uri);
diff --git a/lib/ephy-web-dom-utils.c b/lib/ephy-web-dom-utils.c
index acf0fd2..72df883 100644
--- a/lib/ephy-web-dom-utils.c
+++ b/lib/ephy-web-dom-utils.c
@@ -359,6 +359,7 @@ ephy_web_dom_utils_find_form_auth_elements (WebKitDOMHTMLFormElement *form,
   WebKitDOMNode *password_node = NULL;
   guint i, n_elements;
   gboolean found_auth_elements = FALSE;
+  gboolean found_auth_no_username_elements = FALSE;
 
   elements = webkit_dom_html_form_element_get_elements (form);
   n_elements = webkit_dom_html_collection_get_length (elements);
@@ -366,12 +367,13 @@ ephy_web_dom_utils_find_form_auth_elements (WebKitDOMHTMLFormElement *form,
   for (i = 0; i < n_elements; i++) {
     WebKitDOMNode *element;
     char *element_type;
+    char *element_name;
 
     element = webkit_dom_html_collection_item (elements, i);
     if (!WEBKIT_DOM_IS_HTML_INPUT_ELEMENT (element))
       continue;
 
-    g_object_get (element, "type", &element_type, NULL);
+    g_object_get (element, "type", &element_type, "name", &element_name, NULL);
 
     if (g_str_equal (element_type, "text") || g_str_equal (element_type, "email")) {
       /* We found more than one inputs of type text; we won't be saving here. */
@@ -393,13 +395,27 @@ ephy_web_dom_utils_find_form_auth_elements (WebKitDOMHTMLFormElement *form,
 
       password_node = g_object_ref (element);
       found_auth_elements = TRUE;
+
+      /* We found an input that usually doesn't require a separate login
+       * adminpw is used by mailman admin pages */
+      if (g_str_equal (element_name, "adminpw"))
+        found_auth_no_username_elements = TRUE;
     }
 
     g_free (element_type);
+    g_free (element_name);
   }
 
   g_object_unref(elements);
 
+  if (found_auth_no_username_elements && password_node) {
+    g_clear_object (&username_node);
+    *username = NULL;
+    *password = password_node;
+
+    return TRUE;
+  }
+
   if (found_auth_elements && username_node && password_node) {
     *username = username_node;
     *password = password_node;


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