[gnome-software/wip/ubuntu] Refactor snap macaroon code
- From: Robert Ancell <rancell src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-software/wip/ubuntu] Refactor snap macaroon code
- Date: Fri, 30 Sep 2016 01:35:05 +0000 (UTC)
commit cab5ebbf0fa023d569b9027ff086d11e55119389
Author: Robert Ancell <robert ancell canonical com>
Date: Fri Sep 30 11:45:58 2016 +1300
Refactor snap macaroon code
src/plugins/gs-plugin-snap.c | 37 +++++++++++++++++++++++----
src/plugins/gs-snapd.c | 48 +++++++++++++++++++-----------------
src/plugins/gs-snapd.h | 6 +++-
src/plugins/gs-ubuntuone-dialog.c | 4 +-
src/plugins/gs-ubuntuone.c | 33 ++++++++++++++++---------
src/plugins/gs-ubuntuone.h | 4 ++-
6 files changed, 86 insertions(+), 46 deletions(-)
---
diff --git a/src/plugins/gs-plugin-snap.c b/src/plugins/gs-plugin-snap.c
index 8fbfd50..83deeb2 100644
--- a/src/plugins/gs-plugin-snap.c
+++ b/src/plugins/gs-plugin-snap.c
@@ -127,7 +127,12 @@ refine_app (GsPlugin *plugin, GsApp *app, JsonObject *package, gboolean from_sea
g_autofree gchar *icon_response = NULL;
gsize icon_response_length;
- if (send_snapd_request ("GET", icon_url, NULL, TRUE, NULL, TRUE, NULL, NULL, NULL, NULL,
&icon_response, &icon_response_length, NULL, NULL)) {
+ if (send_snapd_request ("GET", icon_url, NULL,
+ TRUE,
+ NULL, NULL,
+ TRUE,
+ NULL, NULL,
+ NULL, NULL, NULL, &icon_response, &icon_response_length, NULL, NULL))
{
g_autoptr(GdkPixbufLoader) loader = NULL;
loader = gdk_pixbuf_loader_new ();
@@ -208,7 +213,12 @@ get_apps (GsPlugin *plugin, gchar **search_terms, GList **list, AppFilterFunc fi
g_string_append (path, fields);
}
g_ptr_array_free (query_fields, TRUE);
- if (!send_snapd_request ("GET", path->str, NULL, TRUE, NULL, TRUE, NULL, &status_code,
&reason_phrase, &response_type, &response, NULL, cancellable, error))
+ if (!send_snapd_request ("GET", path->str, NULL,
+ TRUE,
+ NULL, NULL,
+ TRUE,
+ NULL, NULL,
+ &status_code, &reason_phrase, &response_type, &response, NULL, cancellable,
error))
return FALSE;
if (status_code != SOUP_STATUS_OK) {
@@ -260,7 +270,12 @@ get_app (GsPlugin *plugin, GsApp *app, GCancellable *cancellable, GError **error
JsonObject *root, *result;
path = g_strdup_printf ("/v2/snaps/%s", gs_app_get_id (app));
- if (!send_snapd_request ("GET", path, NULL, TRUE, NULL, TRUE, NULL, &status_code, &reason_phrase,
&response_type, &response, NULL, cancellable, error))
+ if (!send_snapd_request ("GET", path, NULL,
+ TRUE,
+ NULL, NULL,
+ TRUE,
+ NULL, NULL,
+ &status_code, &reason_phrase, &response_type, &response, NULL, cancellable,
error))
return FALSE;
if (status_code == SOUP_STATUS_NOT_FOUND)
@@ -360,11 +375,17 @@ send_package_action (GsPlugin *plugin, GsApp *app, const char *id, const gchar *
const gchar *resource_path;
const gchar *type;
const gchar *change_id;
- g_autoptr(GVariant) macaroon = NULL;
+ g_autofree gchar *macaroon = NULL;
+ g_auto(GStrv) discharges = NULL;
content = g_strdup_printf ("{\"action\": \"%s\"}", action);
path = g_strdup_printf ("/v2/snaps/%s", id);
- if (!send_snapd_request ("POST", path, content, TRUE, NULL, TRUE, &macaroon, &status_code,
&reason_phrase, &response_type, &response, NULL, cancellable, error))
+ if (!send_snapd_request ("POST", path, content,
+ TRUE,
+ NULL, NULL,
+ TRUE,
+ &macaroon, &discharges,
+ &status_code, &reason_phrase, &response_type, &response, NULL, cancellable,
error))
return FALSE;
if (status_code != SOUP_STATUS_ACCEPTED) {
@@ -393,7 +414,11 @@ send_package_action (GsPlugin *plugin, GsApp *app, const char *id, const gchar *
/* Wait for a little bit before polling */
g_usleep (100 * 1000);
- if (!send_snapd_request ("GET", resource_path, NULL, TRUE, macaroon, TRUE, NULL,
+ if (!send_snapd_request ("GET", resource_path, NULL,
+ TRUE,
+ macaroon, discharges,
+ TRUE,
+ NULL, NULL,
&status_code, &status_reason_phrase, &status_response_type,
&status_response, NULL, cancellable, error)) {
return FALSE;
diff --git a/src/plugins/gs-snapd.c b/src/plugins/gs-snapd.c
index 804c103..bbeadec 100644
--- a/src/plugins/gs-snapd.c
+++ b/src/plugins/gs-snapd.c
@@ -96,9 +96,11 @@ send_snapd_request (const gchar *method,
const gchar *path,
const gchar *content,
gboolean authenticate,
- GVariant *macaroon,
+ const gchar *macaroon_,
+ gchar **discharges_,
gboolean retry_after_login,
- GVariant **out_macaroon,
+ gchar **out_macaroon,
+ gchar ***out_discharges,
guint *status_code,
gchar **reason_phrase,
gchar **response_type,
@@ -113,18 +115,17 @@ send_snapd_request (const gchar *method,
gsize max_data_length = 65535, data_length = 0, header_length;
gchar data[max_data_length + 1], *body = NULL;
g_autoptr (SoupMessageHeaders) headers = NULL;
- g_autoptr(GVariant) auto_macaroon = NULL;
+ g_autofree gchar *macaroon = NULL;
+ g_auto(GStrv) discharges = NULL;
gsize chunk_length, n_required;
gchar *chunk_start = NULL;
- const gchar *root;
- const gchar *discharge;
- GVariantIter *iter;
guint code;
gboolean ret;
+ macaroon = g_strdup (macaroon_);
+ discharges = g_strdupv (discharges_);
if (macaroon == NULL && authenticate) {
- auto_macaroon = gs_ubuntuone_get_macaroon (TRUE, FALSE, NULL);
- macaroon = auto_macaroon;
+ gs_ubuntuone_get_macaroon (TRUE, FALSE, &macaroon, &discharges, NULL);
}
// NOTE: Would love to use libsoup but it doesn't support unix sockets
@@ -138,17 +139,15 @@ send_snapd_request (const gchar *method,
g_string_append_printf (request, "%s %s HTTP/1.1\r\n", method, path);
g_string_append (request, "Host:\r\n");
if (macaroon != NULL) {
- g_variant_get (macaroon, "(&sas)", &root, &iter);
- g_string_append_printf (request, "Authorization: Macaroon root=\"%s\"", root);
+ gint i;
- while (g_variant_iter_next (iter, "&s", &discharge))
- g_string_append_printf (request, ",discharge=\"%s\"", discharge);
-
- g_variant_iter_free (iter);
+ g_string_append_printf (request, "Authorization: Macaroon root=\"%s\"", macaroon);
+ for (i = 0; discharges[i] != NULL; i++)
+ g_string_append_printf (request, ",discharge=\"%s\"", discharges[i]);
g_string_append (request, "\r\n");
}
if (content)
- g_string_append_printf (request, "Content-Length: %zi\r\n", strlen (content));
+ g_string_append_printf (request, "Content-Length: %zu\r\n", strlen (content));
g_string_append (request, "\r\n");
if (content)
g_string_append (request, content);
@@ -203,7 +202,9 @@ send_snapd_request (const gchar *method,
gs_ubuntuone_clear_macaroon ();
- macaroon = gs_ubuntuone_get_macaroon (FALSE, TRUE, NULL);
+ g_clear_pointer (&macaroon, g_free);
+ g_clear_pointer (&discharges, g_strfreev);
+ gs_ubuntuone_get_macaroon (FALSE, TRUE, &macaroon, &discharges, NULL);
if (macaroon == NULL) {
g_set_error_literal (error, GS_PLUGIN_ERROR, GS_PLUGIN_ERROR_FAILED,
@@ -215,9 +216,9 @@ send_snapd_request (const gchar *method,
path,
content,
TRUE,
- macaroon,
+ macaroon, discharges,
FALSE,
- NULL,
+ NULL, NULL,
status_code,
reason_phrase,
response_type,
@@ -227,9 +228,8 @@ send_snapd_request (const gchar *method,
error);
if (ret && out_macaroon != NULL) {
- *out_macaroon = macaroon;
- } else {
- g_variant_unref (macaroon);
+ *out_macaroon = g_steal_pointer (&macaroon);
+ *out_discharges = g_steal_pointer (&discharges);
}
return ret;
@@ -284,8 +284,10 @@ send_snapd_request (const gchar *method,
if (!read_from_snapd (socket, data, n_required - data_length, &data_length, cancellable,
error))
return FALSE;
- if (out_macaroon != NULL)
- *out_macaroon = g_variant_ref (macaroon);
+ if (out_macaroon != NULL) {
+ *out_macaroon = g_steal_pointer (&macaroon);
+ *out_discharges = g_steal_pointer (&discharges);
+ }
if (response_type)
*response_type = g_strdup (soup_message_headers_get_one (headers, "Content-Type"));
if (response) {
diff --git a/src/plugins/gs-snapd.h b/src/plugins/gs-snapd.h
index f41635a..7c4439f 100644
--- a/src/plugins/gs-snapd.h
+++ b/src/plugins/gs-snapd.h
@@ -30,9 +30,11 @@ gboolean send_snapd_request (const gchar *method,
const gchar *path,
const gchar *content,
gboolean authenticate,
- GVariant *macaroon,
+ const gchar *macaroon,
+ gchar **discharges,
gboolean retry_after_login,
- GVariant **out_macaroon,
+ gchar **out_macaroon,
+ gchar ***out_discharges,
guint *status_code,
gchar **reason_phrase,
gchar **response_type,
diff --git a/src/plugins/gs-ubuntuone-dialog.c b/src/plugins/gs-ubuntuone-dialog.c
index ca4dd32..0650dc2 100644
--- a/src/plugins/gs-ubuntuone-dialog.c
+++ b/src/plugins/gs-ubuntuone-dialog.c
@@ -432,9 +432,9 @@ send_login_request (GsUbuntuoneDialog *self)
"/v2/login",
content,
FALSE,
- NULL,
+ NULL, NULL,
FALSE,
- NULL,
+ NULL, NULL,
&status_code,
&reason_phrase,
&response_type,
diff --git a/src/plugins/gs-ubuntuone.c b/src/plugins/gs-ubuntuone.c
index 3c7b582..f778eef 100644
--- a/src/plugins/gs-ubuntuone.c
+++ b/src/plugins/gs-ubuntuone.c
@@ -112,15 +112,16 @@ show_login_dialog (gpointer user_data)
return G_SOURCE_REMOVE;
}
-GVariant *
+gboolean
gs_ubuntuone_get_macaroon (gboolean use_cache,
gboolean show_dialog,
+ gchar **macaroon,
+ gchar ***discharges,
GError **error)
{
LoginContext login_context = { 0 };
g_autofree gchar *password = NULL;
g_autofree gchar *printed = NULL;
- GVariant *macaroon = NULL;
GError *error_local = NULL;
if (use_cache) {
@@ -131,14 +132,19 @@ gs_ubuntuone_get_macaroon (gboolean use_cache,
NULL);
if (password) {
- macaroon = g_variant_parse (G_VARIANT_TYPE ("(sas)"),
- password,
- NULL,
- NULL,
- &error_local);
-
- if (macaroon)
- return macaroon;
+ GVariant *value;
+
+ value = g_variant_parse (G_VARIANT_TYPE ("(sas)"),
+ password,
+ NULL,
+ NULL,
+ &error_local);
+
+ if (value != NULL) {
+ g_variant_get (value, "(s^as)", macaroon, discharges);
+ g_variant_unref (value);
+ return TRUE;
+ }
g_warning ("could not parse macaroon: %s", error_local->message);
g_clear_error (&error_local);
@@ -181,10 +187,13 @@ gs_ubuntuone_get_macaroon (gboolean use_cache,
}
}
- return login_context.macaroon;
+ g_variant_get (login_context.macaroon, "(s^as)", macaroon, discharges);
+ g_variant_unref (login_context.macaroon);
+
+ return TRUE;
}
- return NULL;
+ return FALSE;
}
void
diff --git a/src/plugins/gs-ubuntuone.h b/src/plugins/gs-ubuntuone.h
index 4e9fde8..b3ca792 100644
--- a/src/plugins/gs-ubuntuone.h
+++ b/src/plugins/gs-ubuntuone.h
@@ -26,8 +26,10 @@
G_BEGIN_DECLS
-GVariant *gs_ubuntuone_get_macaroon (gboolean use_cache,
+gboolean gs_ubuntuone_get_macaroon (gboolean use_cache,
gboolean show_dialog,
+ gchar **macaroon,
+ gchar ***discharges,
GError **error);
void gs_ubuntuone_clear_macaroon (void);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]