static void get_macaroon (GsPlugin *plugin, gchar **macaroon, gchar ***discharges) { GsAuth *auth; const gchar *serialized_macaroon; g_autoptr(GVariant) macaroon_variant = NULL; g_autoptr (GError) error_local = NULL; *macaroon = NULL; *discharges = NULL; auth = gs_plugin_get_auth_by_id (plugin, "snapd"); if (auth == NULL) return; serialized_macaroon = gs_auth_get_metadata_item (auth, "macaroon"); if (serialized_macaroon == NULL) return; macaroon_variant = g_variant_parse (G_VARIANT_TYPE ("(sas)"), serialized_macaroon, NULL, NULL, NULL); if (macaroon_variant == NULL) return; g_variant_get (macaroon_variant, "(s^as)", macaroon, discharges); }
static gboolean parse_reviews (GsPlugin *plugin, JsonParser *parser, GsApp *app, GCancellable *cancellable, GError **error) { GsAuth *auth; JsonArray *array; const gchar *consumer_key = NULL; guint i; auth = gs_plugin_get_auth_by_id (plugin, "ubuntuone"); if (auth != NULL) consumer_key = gs_auth_get_metadata_item (auth, "consumer-key"); if (!JSON_NODE_HOLDS_ARRAY (json_parser_get_root (parser))) return FALSE; array = json_node_get_array (json_parser_get_root (parser)); for (i = 0; i < json_array_get_length (array); i++) { g_autoptr(AsReview) review = NULL; /* Read in from JSON... (skip bad entries) */ review = as_review_new (); if (parse_review (review, consumer_key, json_array_get_element (array, i))) gs_app_add_review (app, review); } return TRUE; }
static void load_auth (GsPlugin *plugin) { GsPluginData *priv = gs_plugin_get_data (plugin); GsAuth *auth; GoaObject *goa_object; GoaPasswordBased *password_based; g_autofree gchar *macaroon = NULL; g_autofree gchar *discharges_str = NULL; g_autoptr(GVariant) discharges_var = NULL; g_auto(GStrv) discharges = NULL; g_autoptr(SnapdAuthData) auth_data = NULL; g_autoptr(GError) error = NULL; auth = gs_plugin_get_auth_by_id (plugin, "snapd"); if (auth == NULL) return; g_clear_object (&priv->auth_data); goa_object = gs_auth_peek_goa_object (auth); if (goa_object == NULL) return; password_based = goa_object_peek_password_based (goa_object); g_return_if_fail (password_based != NULL); goa_password_based_call_get_password_sync (password_based, "macaroon", &macaroon, NULL, &error); if (error != NULL) { g_warning ("Failed to get macaroon: %s", error->message); return; } goa_password_based_call_get_password_sync (password_based, "discharges", &discharges_str, NULL, &error); if (error != NULL) { g_warning ("Failed to get discharges %s", error->message); return; } if (discharges_str) discharges_var = g_variant_parse (G_VARIANT_TYPE ("as"), discharges_str, NULL, NULL, NULL); if (discharges_var) discharges = g_variant_dup_strv (discharges_var, NULL); priv->auth_data = snapd_auth_data_new (macaroon, discharges); }
gboolean gs_plugin_review_submit (GsPlugin *plugin, GsApp *app, AsReview *review, GCancellable *cancellable, GError **error) { GsPluginData *priv = gs_plugin_get_data (plugin); GsAuth *auth; const gchar *consumer_key = NULL; const gchar *language; gint rating; gint n_stars; g_autofree gchar *architecture = NULL; g_autoptr(JsonBuilder) request = NULL; guint status_code; g_autoptr(JsonParser) result = NULL; /* Ubuntu reviews require a summary and description - just make one up for now */ rating = as_review_get_rating (review); if (rating > 80) n_stars = 5; else if (rating > 60) n_stars = 4; else if (rating > 40) n_stars = 3; else if (rating > 20) n_stars = 2; else n_stars = 1; language = gs_plugin_get_language (plugin); // FIXME: Need to get Apt::Architecture configuration value from APT architecture = g_strdup ("amd64"); /* Create message for reviews.ubuntu.com */ request = json_builder_new (); json_builder_begin_object (request); add_string_member (request, "package_name", gs_app_get_source_default (app)); add_string_member (request, "summary", as_review_get_summary (review)); add_string_member (request, "review_text", as_review_get_description (review)); add_string_member (request, "language", language); add_string_member (request, "origin", priv->origin); add_string_member (request, "distroseries", priv->distroseries); add_string_member (request, "version", as_review_get_version (review)); add_int_member (request, "rating", n_stars); add_string_member (request, "arch_tag", architecture); json_builder_end_object (request); if (!send_review_request (plugin, SOUP_METHOD_POST, "/api/1.0/reviews/", request, TRUE, &status_code, &result, cancellable, error)) return FALSE; if (status_code != SOUP_STATUS_OK) { g_set_error (error, GS_PLUGIN_ERROR, GS_PLUGIN_ERROR_FAILED, "Failed to submit review, server returned status code %u", status_code); return FALSE; } // Extract new fields from posted review auth = gs_plugin_get_auth_by_id (plugin, "ubuntuone"); if (auth != NULL) consumer_key = gs_auth_get_metadata_item (auth, "consumer-key"); parse_review (review, consumer_key, json_parser_get_root (result)); return TRUE; }
static gboolean send_review_request (GsPlugin *plugin, const gchar *method, const gchar *path, JsonBuilder *request, gboolean do_sign, guint *status_code, JsonParser **result, GCancellable *cancellable, GError **error) { g_autofree gchar *uri = NULL; g_autoptr(SoupMessage) msg = NULL; uri = g_strdup_printf ("%s%s", UBUNTU_REVIEWS_SERVER, path); msg = soup_message_new (method, uri); if (request != NULL) { g_autoptr(JsonGenerator) generator = NULL; gchar *data; gsize length; generator = json_generator_new (); json_generator_set_root (generator, json_builder_get_root (request)); data = json_generator_to_data (generator, &length); soup_message_set_request (msg, "application/json", SOUP_MEMORY_TAKE, data, length); } if (do_sign) sign_message (msg, OA_PLAINTEXT, gs_plugin_get_auth_by_id (plugin, "ubuntuone")); *status_code = soup_session_send_message (gs_plugin_get_soup_session (plugin), msg); if (*status_code == SOUP_STATUS_UNAUTHORIZED) { g_set_error_literal (error, GS_PLUGIN_ERROR, GS_PLUGIN_ERROR_AUTH_REQUIRED, "Requires authentication with @ubuntuone"); return FALSE; } if (result != NULL) { g_autoptr(JsonParser) parser = NULL; const gchar *content_type; content_type = soup_message_headers_get_content_type (msg->response_headers, NULL); if (g_strcmp0 (content_type, "application/json") != 0) { g_set_error (error, GS_PLUGIN_ERROR, GS_PLUGIN_ERROR_FAILED, "Got unknown content type %s from reviews.ubuntu.com", content_type); return FALSE; } parser = json_parser_new (); if (!json_parser_load_from_data (parser, msg->response_body->data, -1, error)) { return FALSE; } *result = g_steal_pointer (&parser); } return TRUE; }