pidgin/pidgin

Remove gg_oauth_parameter_t

2021-02-26, Gary Kramlich
1ea760e56414
Parents 65dbbd384c8e
Children 4f197d3e42d6
Remove gg_oauth_parameter_t

Use variadic function gg_oauth_generate_request instead.

This is a repost of https://reviews.imfreedom.org/r/516/ but reviewboard wouldn't let me update it directly.

Original patch was from Arkadiy Illarionov with updates from Gary Kramlich.

Testing Done:
Compile only.

Reviewed at https://reviews.imfreedom.org/r/525/
--- a/libpurple/protocols/gg/meson.build Thu Feb 25 20:44:26 2021 -0600
+++ b/libpurple/protocols/gg/meson.build Fri Feb 26 00:12:08 2021 -0600
@@ -45,8 +45,6 @@
'xml.h',
'oauth/oauth.c',
'oauth/oauth.h',
- 'oauth/oauth-parameter.c',
- 'oauth/oauth-parameter.h',
'oauth/oauth-purple.c',
'oauth/oauth-purple.h'
]
--- a/libpurple/protocols/gg/oauth/oauth-parameter.c Thu Feb 25 20:44:26 2021 -0600
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,161 +0,0 @@
-/* purple
- *
- * Purple is the legal property of its developers, whose names are too numerous
- * to list here. Please refer to the COPYRIGHT file distributed with this
- * source distribution.
- *
- * Code adapted from libgadu (C) 2008 Wojtek Kaniewski <wojtekka@irc.pl>
- * (http://toxygen.net/libgadu/) during Google Summer of Code 2012
- * by Tomek Wasilczyk (http://www.wasilczyk.pl).
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
- */
-
-#include "oauth-parameter.h"
-
-#include <glib.h>
-
-struct gg_oauth_parameter {
- char *key;
- char *value;
- struct gg_oauth_parameter *next;
-};
-
-int gg_oauth_parameter_set(gg_oauth_parameter_t **list, const char *key, const char *value)
-{
- gg_oauth_parameter_t *p, *new_p;
- char *new_key;
- char *new_value;
-
- if (value == NULL)
- return 0;
-
- if (list == NULL)
- return -1;
-
- new_key = g_strdup(key);
-
- if (new_key == NULL)
- return -1;
-
- new_value = g_strdup(value);
-
- if (new_value == NULL) {
- g_free(new_key);
- return -1;
- }
-
- new_p = g_new0(gg_oauth_parameter_t, 1);
-
- if (new_p == NULL) {
- g_free(new_key);
- g_free(new_value);
- return -1;
- }
-
- memset(new_p, 0, sizeof(gg_oauth_parameter_t));
- new_p->key = new_key;
- new_p->value = new_value;
-
- if (*list != NULL) {
- p = *list;
-
- while (p != NULL && p->next != NULL)
- p = p->next;
-
- p->next = new_p;
- } else {
- *list = new_p;
- }
-
- return 0;
-}
-
-char *gg_oauth_parameter_join(gg_oauth_parameter_t *list, int header)
-{
- gg_oauth_parameter_t *p;
- int len = 0;
- char *res, *out;
-
- if (header)
- len += strlen("OAuth ");
-
- for (p = list; p; p = p->next) {
- gchar *escaped;
- len += strlen(p->key);
-
- len += (header) ? 3 : 1;
-
- escaped = g_uri_escape_string(p->value, NULL, FALSE);
- len += strlen(escaped);
- g_free(escaped);
-
- if (p->next)
- len += 1;
- }
-
- res = g_malloc(len + 1);
-
- if (res == NULL)
- return NULL;
-
- out = res;
-
- *out = 0;
-
- if (header) {
- strcpy(out, "OAuth ");
- out += strlen(out);
- }
-
- for (p = list; p; p = p->next) {
- gchar *escaped;
- strcpy(out, p->key);
- out += strlen(p->key);
-
- strcpy(out++, "=");
-
- if (header)
- strcpy(out++, "\"");
-
- escaped = g_uri_escape_string(p->value, NULL, FALSE);
- strcpy(out, escaped);
- out += strlen(escaped);
- g_free(escaped);
-
- if (header)
- strcpy(out++, "\"");
-
- if (p->next != NULL)
- strcpy(out++, (header) ? "," : "&");
- }
-
- return res;
-}
-
-void gg_oauth_parameter_free(gg_oauth_parameter_t *list)
-{
- while (list != NULL) {
- gg_oauth_parameter_t *next;
-
- next = list->next;
-
- g_free(list->key);
- g_free(list->value);
- g_free(list);
-
- list = next;
- }
-}
--- a/libpurple/protocols/gg/oauth/oauth-parameter.h Thu Feb 25 20:44:26 2021 -0600
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,35 +0,0 @@
-/* purple
- *
- * Purple is the legal property of its developers, whose names are too numerous
- * to list here. Please refer to the COPYRIGHT file distributed with this
- * source distribution.
- *
- * Code adapted from libgadu (C) 2008 Wojtek Kaniewski <wojtekka@irc.pl>
- * (http://toxygen.net/libgadu/) during Google Summer of Code 2012
- * by Tomek Wasilczyk (http://www.wasilczyk.pl).
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
- */
-
-#ifndef _GGP_OAUTH_PARAMETER_H
-#define _GGP_OAUTH_PARAMETER_H
-
-typedef struct gg_oauth_parameter gg_oauth_parameter_t;
-
-int gg_oauth_parameter_set(gg_oauth_parameter_t **list, const char *key, const char *value);
-char *gg_oauth_parameter_join(gg_oauth_parameter_t *list, int header);
-void gg_oauth_parameter_free(gg_oauth_parameter_t *list);
-
-#endif /* _GGP_OAUTH_PARAMETER_H */
--- a/libpurple/protocols/gg/oauth/oauth.c Thu Feb 25 20:44:26 2021 -0600
+++ b/libpurple/protocols/gg/oauth/oauth.c Fri Feb 26 00:12:08 2021 -0600
@@ -25,8 +25,6 @@
#include "oauth.h"
-#include "oauth-parameter.h"
-
#include <time.h>
#include <glib.h>
@@ -34,6 +32,8 @@
char *gg_oauth_static_nonce; /* dla unit testów */
char *gg_oauth_static_timestamp; /* dla unit testów */
+static char *gg_oauth_generate_request(gboolean header, ...) G_GNUC_NULL_TERMINATED;
+
static void gg_oauth_generate_nonce(char *buf, int len)
{
const char charset[] = "0123456789";
@@ -49,6 +49,46 @@
*buf = 0;
}
+/* Returns a comma separated header value if header is true,
+ * or a url-encoded request otherwise
+ */
+static char *
+gg_oauth_generate_request(gboolean header, ...)
+{
+ GString *res = g_string_new(NULL);
+ va_list params;
+ const gchar *key;
+ gboolean truncate = FALSE;
+
+ if(header) {
+ res = g_string_append(res, "OAuth ");
+ }
+
+ va_start(params, header);
+ while((key = va_arg(params, const gchar *))) {
+ const gchar *value = va_arg(params, const gchar *);
+ gchar *escaped = g_uri_escape_string(value, NULL, FALSE);
+
+ if(header) {
+ g_string_append_printf(res, "%s=\"%s\",", key, escaped);
+ } else {
+ g_string_append_printf(res, "%s=%s&", key, escaped);
+ }
+
+ g_free(escaped);
+
+ truncate = TRUE;
+ }
+ va_end(params);
+
+ if(truncate) {
+ /* remove trailing separator */
+ res = g_string_truncate(res, res->len - 1);
+ }
+
+ return g_string_free(res, FALSE);
+}
+
static gchar *gg_hmac_sha1(const char *key, const char *message)
{
GHmac *hmac;
@@ -99,7 +139,6 @@
{
char *request, *signature, *res;
char nonce[80], timestamp[16];
- gg_oauth_parameter_t *params = NULL;
if (gg_oauth_static_nonce == NULL)
gg_oauth_generate_nonce(nonce, sizeof(nonce));
@@ -115,38 +154,32 @@
timestamp[sizeof(timestamp) - 1] = 0;
}
- gg_oauth_parameter_set(&params, "oauth_consumer_key", consumer_key);
- gg_oauth_parameter_set(&params, "oauth_nonce", nonce);
- gg_oauth_parameter_set(&params, "oauth_signature_method", "HMAC-SHA1");
- gg_oauth_parameter_set(&params, "oauth_timestamp", timestamp);
- gg_oauth_parameter_set(&params, "oauth_token", token);
- gg_oauth_parameter_set(&params, "oauth_version", "1.0");
-
- request = gg_oauth_parameter_join(params, 0);
+ request = gg_oauth_generate_request(FALSE,
+ "oauth_consumer_key", consumer_key,
+ "oauth_nonce", nonce,
+ "oauth_signature_method", "HMAC-SHA1",
+ "oauth_timestamp", timestamp,
+ "oauth_token", token,
+ "oauth_version", "1.0",
+ NULL);
signature = gg_oauth_generate_signature(method, url, request, consumer_secret, token_secret);
g_free(request);
- gg_oauth_parameter_free(params);
- params = NULL;
-
if (signature == NULL)
return NULL;
- gg_oauth_parameter_set(&params, "oauth_version", "1.0");
- gg_oauth_parameter_set(&params, "oauth_nonce", nonce);
- gg_oauth_parameter_set(&params, "oauth_timestamp", timestamp);
- gg_oauth_parameter_set(&params, "oauth_consumer_key", consumer_key);
- gg_oauth_parameter_set(&params, "oauth_token", token);
- gg_oauth_parameter_set(&params, "oauth_signature_method", "HMAC-SHA1");
- gg_oauth_parameter_set(&params, "oauth_signature", signature);
-
+ res = gg_oauth_generate_request(TRUE,
+ "oauth_version", "1.0",
+ "oauth_nonce", nonce,
+ "oauth_timestamp", timestamp,
+ "oauth_consumer_key", consumer_key,
+ "oauth_token", token,
+ "oauth_signature_method", "HMAC-SHA1",
+ "oauth_signature", signature,
+ NULL);
g_free(signature);
- res = gg_oauth_parameter_join(params, 1);
-
- gg_oauth_parameter_free(params);
-
return res;
}