diff options
author | CodedOre <25259-CodedOre@users.noreply.gitlab.gnome.org> | 2022-01-13 11:36:29 +0000 |
---|---|---|
committer | CodedOre <25259-CodedOre@users.noreply.gitlab.gnome.org> | 2022-01-13 11:36:29 +0000 |
commit | 7b3285507d2d01acf293223fba275011636aeb07 (patch) | |
tree | 7c9a9cb2cca39f01274d7236339962aca7b4972b /rest | |
parent | 7e645d051ce4b1ef44482e2f3017e43fd5ebcdaa (diff) | |
parent | 85bd00adfa6e06d3426ce7c9007e68e62f51be14 (diff) | |
download | librest-7b3285507d2d01acf293223fba275011636aeb07.tar.gz |
Fix merge conflicts.
Diffstat (limited to 'rest')
-rw-r--r-- | rest/Makefile.am | 139 | ||||
-rw-r--r-- | rest/meson.build | 28 | ||||
-rw-r--r-- | rest/oauth-proxy-call.c | 53 | ||||
-rw-r--r-- | rest/oauth-proxy-private.h | 45 | ||||
-rw-r--r-- | rest/oauth-proxy.c | 119 | ||||
-rw-r--r-- | rest/oauth-proxy.h | 151 | ||||
-rw-r--r-- | rest/oauth2-proxy-call.c | 68 | ||||
-rw-r--r-- | rest/oauth2-proxy-call.h | 49 | ||||
-rw-r--r-- | rest/oauth2-proxy.c | 397 | ||||
-rw-r--r-- | rest/oauth2-proxy.h | 95 | ||||
-rw-r--r-- | rest/rest-oauth2-proxy-call.c | 62 | ||||
-rw-r--r-- | rest/rest-oauth2-proxy-call.h (renamed from rest/oauth2-proxy-private.h) | 29 | ||||
-rw-r--r-- | rest/rest-oauth2-proxy.c | 802 | ||||
-rw-r--r-- | rest/rest-oauth2-proxy.h | 101 | ||||
-rw-r--r-- | rest/rest-param.c | 2 | ||||
-rw-r--r-- | rest/rest-params.c | 196 | ||||
-rw-r--r-- | rest/rest-params.h | 52 | ||||
-rw-r--r-- | rest/rest-pkce-code-challenge.c | 121 | ||||
-rw-r--r-- | rest/rest-pkce-code-challenge.h | 44 | ||||
-rw-r--r-- | rest/rest-proxy-call.c | 20 | ||||
-rw-r--r-- | rest/rest-proxy.c | 73 | ||||
-rw-r--r-- | rest/rest-proxy.h | 61 | ||||
-rw-r--r-- | rest/rest-utils.c | 44 | ||||
-rw-r--r-- | rest/rest-utils.h | 27 | ||||
-rw-r--r-- | rest/rest-xml-node.h | 34 | ||||
-rw-r--r-- | rest/rest-xml-parser.h | 19 | ||||
-rw-r--r-- | rest/rest.h | 35 |
27 files changed, 1723 insertions, 1143 deletions
diff --git a/rest/Makefile.am b/rest/Makefile.am deleted file mode 100644 index 64205d4..0000000 --- a/rest/Makefile.am +++ /dev/null @@ -1,139 +0,0 @@ -CLEANFILES = - -# For some reason I can't use $(librest_@API_VERSION@_la_SOURCES) in -# test_runner_SOURCES, so we have to do this -lib_sources = \ - rest-param.c \ - rest-params.c \ - rest-proxy.c \ - rest-proxy-auth.c \ - rest-proxy-auth-private.h \ - rest-proxy-call.c \ - rest-proxy-call-private.h \ - rest-xml-node.c \ - rest-xml-parser.c \ - rest-main.c \ - rest-private.h \ - rest-enum-types.c \ - oauth-proxy.c \ - oauth-proxy-call.c \ - oauth-proxy-private.h \ - oauth2-proxy.c \ - oauth2-proxy-call.c \ - oauth2-proxy-private.h \ - sha1.c \ - sha1.h -nodist_lib_sources = \ - rest-marshal.h \ - rest-marshal.c -lib_headers = \ - rest-param.h \ - rest-params.h \ - rest-proxy.h \ - rest-proxy-auth.h \ - rest-proxy-call.h \ - rest-enum-types.h \ - oauth-proxy.h \ - oauth-proxy-call.h \ - oauth2-proxy.h \ - oauth2-proxy-call.h \ - rest-xml-node.h \ - rest-xml-parser.h - -EXTRA_DIST = \ - rest-marshal.txt - -lib_LTLIBRARIES = librest-@API_VERSION@.la -librest_@API_VERSION@_la_CFLAGS = $(GLIB_CFLAGS) $(GTHREAD_CFLAGS) \ - $(SOUP_CFLAGS) $(SOUP_GNOME_CFLAGS) \ - $(XML_CFLAGS) $(GCOV_CFLAGS) \ - -I$(top_srcdir) -Wall -DG_LOG_DOMAIN=\"Rest\" -librest_@API_VERSION@_la_LDFLAGS = -no-undefined -librest_@API_VERSION@_la_LIBADD = $(GLIB_LIBS) $(GTHREAD_LIBS) \ - $(SOUP_LIBS) $(SOUP_GNOME_LIBS) $(XML_LIBS) \ - $(GCOV_LDFLAGS) -librest_@API_VERSION@_la_SOURCES = $(lib_sources) $(lib_headers) -nodist_librest_@API_VERSION@_la_SOURCES = $(nodist_lib_sources) -librest_@API_VERSION@_la_HEADERS = $(lib_headers) -librest_@API_VERSION@_ladir = $(includedir)/rest-@API_VERSION@/rest - -rest-enum-types.h: stamp-rest-enum-types.h - @true - -stamp-rest-enum-types.h: rest-proxy.h rest-proxy-call.h Makefile - $(AM_V_GEN) (cd $(srcdir) && $(GLIB_MKENUMS) \ - --fhead "#ifndef __REST_ENUM_TYPES_H__\n#define __REST_ENUM_TYPES_H__\n\n#include <glib-object.h>\n\nG_BEGIN_DECLS\n" \ - --fprod "/* enumerations from \"@filename@\" */\n" \ - --vhead "GType @enum_name@_get_type (void) G_GNUC_CONST;\n#define REST_TYPE_@ENUMSHORT@ (@enum_name@_get_type())\n" \ - --ftail "G_END_DECLS\n\n#endif /* __REST_ENUM_TYPES_H__ */" rest-proxy.h rest-proxy-call.h) >> xgen-gtbh \ - && (cmp -s xgen-gtbh rest-enum-types.h || cp xgen-gtbh rest-enum-types.h ) \ - && rm -f xgen-gtbh \ - && echo timestamp > $(@F) - -rest-enum-types.c: rest-proxy.h rest-proxy-call.h Makefile rest-enum-types.h - $(AM_V_GEN) (cd $(srcdir) && $(GLIB_MKENUMS) \ - --fhead "#include \"rest-proxy.h\"\n#include \"rest-proxy-call.h\"\n#include \"rest-enum-types.h\"" \ - --fprod "\n/* enumerations from \"@filename@\" */" \ - --vhead "GType\n@enum_name@_get_type (void)\n{\n static GType etype = 0;\n if (etype == 0) {\n static const G@Type@Value values[] = {" \ - --vprod " { @VALUENAME@, \"@VALUENAME@\", \"@valuenick@\" }," \ - --vtail " { 0, NULL, NULL }\n };\n etype = g_@type@_register_static (\"@EnumName@\", values);\n }\n return etype;\n}\n" \ - rest-proxy.h rest-proxy-call.h) > xgen-gtbc \ - && cp xgen-gtbc rest-enum-types.c \ - && rm -f xgen-gtbc - -BUILT_SOURCES = rest-marshal.c rest-marshal.h - -rest-marshal.c: rest-marshal.txt - $(AM_V_GEN)echo "#include \"rest-marshal.h\"" > $@ && \ - glib-genmarshal --body $< >> $@ || (rm -f $@ && exit 1) - -rest-marshal.h: rest-marshal.txt - $(AM_V_GEN)glib-genmarshal --header $< > $@ || (rm -f $@ && exit 1) - - -# Test suite -TESTS = test-runner -check_PROGRAMS = test-runner - -test_runner_SOURCES = test-runner.c $(lib_sources) $(nodist_lib_sources) $(lib_headers) -test_runner_CFLAGS = -DBUILD_TESTS $(librest_@API_VERSION@_la_CFLAGS) $(GCOV_CFLAGS) -test_runner_LDFLAGS = $(librest_@API_VERSION@_la_LIBADD) $(GCOV_LDFLAGS) - -# TODO: use gtester - -# introspection --include $(INTROSPECTION_MAKEFILE) - -if HAVE_INTROSPECTION -INTROSPECTION_GIRS = Rest-@API_VERSION@.gir - -Rest-@API_VERSION@.gir: librest-@API_VERSION@.la Makefile - -Rest_@API_VERSION_AM@_gir_NAMESPACE = Rest -Rest_@API_VERSION_AM@_gir_VERSION = @API_VERSION@ -Rest_@API_VERSION_AM@_gir_LIBS = librest-@API_VERSION@.la -Rest_@API_VERSION_AM@_gir_FILES = \ - $(lib_headers) \ - $(filter-out %private.h, $(lib_sources)) -Rest_@API_VERSION_AM@_gir_CFLAGS = -I$(top_srcdir) -Rest_@API_VERSION_AM@_gir_INCLUDES = GObject-2.0 Gio-2.0 Soup-2.4 -Rest_@API_VERSION_AM@_gir_PACKAGES = gobject-2.0 libsoup-2.4 libxml-2.0 gio-2.0 -Rest_@API_VERSION_AM@_gir_SCANNERFLAGS = --accept-unprefixed --warn-all -Rest_@API_VERSION_AM@_gir_EXPORT_PACKAGES = rest-@API_VERSION@ -REST_CINCLUDES=$(patsubst %,--c-include='rest/%',$(shell echo $(lib_headers))) -INTROSPECTION_SCANNER_ARGS = $(REST_CINCLUDES) - -girdir = $(datadir)/gir-1.0 -dist_gir_DATA = $(INTROSPECTION_GIRS) - -typelibsdir = $(libdir)/girepository-1.0/ -typelibs_DATA = $(INTROSPECTION_GIRS:.gir=.typelib) - -CLEANFILES += $(dist_gir_DATA) \ - $(typelibs_DATA) \ - rest-enum-types.h \ - rest-enum-types.c \ - stamp-rest-enum-types.h \ - $(NULL) - -endif # HAVE_INTROSPECTION diff --git a/rest/meson.build b/rest/meson.build index 136416f..868c239 100644 --- a/rest/meson.build +++ b/rest/meson.build @@ -19,10 +19,13 @@ librest_sources = [ 'rest-main.c', 'oauth-proxy.c', 'oauth-proxy-call.c', - 'oauth2-proxy.c', - 'oauth2-proxy-call.c', 'sha1.c', + 'rest-oauth2-proxy.c', + 'rest-oauth2-proxy-call.c', + 'rest-pkce-code-challenge.c', + 'rest-utils.c', + librest_enums, librest_marshal, ] @@ -30,8 +33,6 @@ librest_sources = [ librest_headers = [ 'oauth-proxy-call.h', 'oauth-proxy.h', - 'oauth2-proxy-call.h', - 'oauth2-proxy.h', 'rest-param.h', 'rest-params.h', 'rest-proxy-auth.h', @@ -39,11 +40,18 @@ librest_headers = [ 'rest-proxy.h', 'rest-xml-node.h', 'rest-xml-parser.h', + + 'rest-oauth2-proxy.h', + 'rest-oauth2-proxy-call.h', + 'rest-pkce-code-challenge.h', + 'rest-utils.h', + 'rest.h', ] librest_deps = [ glib_dep, libsoup_dep, + libjson_glib_dep, libxml_dep, ] @@ -65,6 +73,8 @@ install_headers(librest_headers, subdir: librest_pkg_string / 'rest', ) +rest_dep_sources = [] + # GObject Introspection if get_option('introspection') librest_gir_extra_args = [ @@ -77,13 +87,16 @@ if get_option('introspection') endforeach librest_gir = gnome.generate_gir(librest_lib, - sources: [ librest_headers, librest_enums[1] ], - namespace: 'Rest', + sources: librest_sources + librest_headers + librest_enums, nsversion: librest_api_version, + namespace: 'Rest', + symbol_prefix: 'rest', + identifier_prefix: 'Rest', includes: [ 'GObject-2.0', 'Gio-2.0', 'Soup-@0@'.format(libsoup_api_version) ], extra_args: librest_gir_extra_args, install: true, ) + rest_dep_sources += librest_gir if get_option('vapi') librest_vapi = gnome.generate_vapi(librest_pkg_string, @@ -95,7 +108,10 @@ if get_option('introspection') endif librest_dep = declare_dependency( + sources: rest_dep_sources, link_with: librest_lib, + include_directories: include_directories('..'), + dependencies: librest_deps, ) # Test suite diff --git a/rest/oauth-proxy-call.c b/rest/oauth-proxy-call.c index e238c3c..560e928 100644 --- a/rest/oauth-proxy-call.c +++ b/rest/oauth-proxy-call.c @@ -24,7 +24,7 @@ #include <libsoup/soup.h> #include <rest/rest-proxy-call.h> #include "oauth-proxy-call.h" -#include "oauth-proxy-private.h" +#include "oauth-proxy.h" #include "rest-proxy-call-private.h" #include "sha1.h" @@ -33,14 +33,14 @@ G_DEFINE_TYPE (OAuthProxyCall, oauth_proxy_call, REST_TYPE_PROXY_CALL) #define OAUTH_ENCODE_STRING(x_) (x_ ? g_uri_escape_string( (x_), NULL, TRUE) : g_strdup ("")) static char * -sign_plaintext (OAuthProxyPrivate *priv) +sign_plaintext (OAuthProxy *proxy) { char *cs; char *ts; char *rv; - cs = OAUTH_ENCODE_STRING (priv->consumer_secret); - ts = OAUTH_ENCODE_STRING (priv->token_secret); + cs = OAUTH_ENCODE_STRING (oauth_proxy_get_consumer_secret (proxy)); + ts = OAUTH_ENCODE_STRING (oauth_proxy_get_token_secret (proxy)); rv = g_strconcat (cs, "&", ts, NULL); g_free (cs); @@ -117,7 +117,6 @@ merge_params (GHashTable *hash, RestParams *params) static char * sign_hmac (OAuthProxy *proxy, RestProxyCall *call, GHashTable *oauth_params) { - OAuthProxyPrivate *priv; const char *url_str; char *key, *signature, *ep, *eep; const char *content_type; @@ -127,15 +126,14 @@ sign_hmac (OAuthProxy *proxy, RestProxyCall *call, GHashTable *oauth_params) RestParam *param; gboolean encode_query_params = TRUE; - priv = PROXY_GET_PRIVATE (proxy); url_str = rest_proxy_call_get_url (call); text = g_string_new (NULL); g_string_append (text, rest_proxy_call_get_method (call)); g_string_append_c (text, '&'); - if (priv->oauth_echo) { - g_string_append_uri_escaped (text, priv->service_url, NULL, FALSE); - } else if (priv->signature_host != NULL) { + if (oauth_proxy_is_echo (proxy)) + g_string_append_uri_escaped (text, oauth_proxy_get_service_url (proxy), NULL, FALSE); + else if (oauth_proxy_get_signature_host (proxy) != NULL) { GUri *url = g_uri_parse (url_str, G_URI_FLAGS_ENCODED, NULL); GUri *new_url; gchar *signing_url; @@ -143,7 +141,7 @@ sign_hmac (OAuthProxy *proxy, RestProxyCall *call, GHashTable *oauth_params) new_url = g_uri_build (g_uri_get_flags (url), g_uri_get_scheme (url), g_uri_get_userinfo (url), - priv->signature_host, + oauth_proxy_get_signature_host (proxy), g_uri_get_port (url), g_uri_get_path (url), g_uri_get_query (url), @@ -178,7 +176,7 @@ sign_hmac (OAuthProxy *proxy, RestProxyCall *call, GHashTable *oauth_params) /* Merge the OAuth parameters with the query parameters */ all_params = g_hash_table_new (g_str_hash, g_str_equal); merge_hashes (all_params, oauth_params); - if (encode_query_params && !priv->oauth_echo) { + if (encode_query_params && !oauth_proxy_is_echo (proxy)) { merge_params (all_params, rest_proxy_call_get_params (call)); } @@ -191,7 +189,7 @@ sign_hmac (OAuthProxy *proxy, RestProxyCall *call, GHashTable *oauth_params) g_hash_table_destroy (all_params); /* PLAINTEXT signature value is the HMAC-SHA1 key value */ - key = sign_plaintext (priv); + key = sign_plaintext (proxy); signature = hmac_sha1 (key, text->str); @@ -263,12 +261,10 @@ static gboolean _prepare (RestProxyCall *call, GError **error) { OAuthProxy *proxy = NULL; - OAuthProxyPrivate *priv; char *s; GHashTable *oauth_params; g_object_get (call, "proxy", &proxy, NULL); - priv = PROXY_GET_PRIVATE (proxy); /* We have to make this hash free the strings and thus duplicate when we put * them in since when we call call steal_oauth_params that has to duplicate @@ -288,15 +284,15 @@ _prepare (RestProxyCall *call, GError **error) g_hash_table_insert (oauth_params, g_strdup ("oauth_nonce"), s); g_hash_table_insert (oauth_params, g_strdup ("oauth_consumer_key"), - g_strdup (priv->consumer_key)); + g_strdup (oauth_proxy_get_consumer_key (proxy))); - if (priv->token) - g_hash_table_insert (oauth_params, g_strdup ("oauth_token"), g_strdup (priv->token)); + if (oauth_proxy_get_token (proxy)) + g_hash_table_insert (oauth_params, g_strdup ("oauth_token"), g_strdup (oauth_proxy_get_token (proxy))); - switch (priv->method) { + switch (oauth_proxy_get_sign_method (proxy)) { case PLAINTEXT: g_hash_table_insert (oauth_params, g_strdup ("oauth_signature_method"), g_strdup ("PLAINTEXT")); - s = sign_plaintext (priv); + s = sign_plaintext (proxy); break; case HMAC_SHA1: g_hash_table_insert (oauth_params, g_strdup ("oauth_signature_method"), g_strdup ("HMAC-SHA1")); @@ -306,9 +302,9 @@ _prepare (RestProxyCall *call, GError **error) g_hash_table_insert (oauth_params, g_strdup ("oauth_signature"), s); s = make_authorized_header (oauth_params); - if (priv->oauth_echo) { + if (oauth_proxy_is_echo (proxy)) { rest_proxy_call_add_header (call, "X-Verify-Credentials-Authorization", s); - rest_proxy_call_add_param (call, "X-Auth-Service-Provider", priv->service_url); + rest_proxy_call_add_param (call, "X-Auth-Service-Provider", oauth_proxy_get_service_url (proxy)); } else { rest_proxy_call_add_header (call, "Authorization", s); } @@ -336,27 +332,24 @@ oauth_proxy_call_init (OAuthProxyCall *self) void oauth_proxy_call_parse_token_response (OAuthProxyCall *call) { - OAuthProxyPrivate *priv; GHashTable *form; OAuthProxy *proxy; + g_autofree gchar *formstr = NULL; /* TODO: sanity checks, error handling, probably return gboolean */ g_return_if_fail (OAUTH_IS_PROXY_CALL (call)); g_object_get (call, "proxy", &proxy, NULL); - priv = PROXY_GET_PRIVATE (proxy); g_object_unref (proxy); - g_assert (priv); - form = soup_form_decode (rest_proxy_call_get_payload (REST_PROXY_CALL (call))); + formstr = g_strndup (rest_proxy_call_get_payload (REST_PROXY_CALL (call)), rest_proxy_call_get_payload_length (REST_PROXY_CALL (call))); + form = soup_form_decode (formstr); - g_free (priv->token); - g_free (priv->token_secret); - priv->token = g_strdup (g_hash_table_lookup (form, "oauth_token")); - priv->token_secret = g_strdup (g_hash_table_lookup (form, "oauth_token_secret")); + oauth_proxy_set_token (proxy, g_hash_table_lookup (form, "oauth_token")); + oauth_proxy_set_token_secret (proxy, g_hash_table_lookup (form, "oauth_token_secret")); /* This header should only exist for request_token replies, but its easier just to always check it */ - priv->oauth_10a = g_hash_table_lookup (form, "oauth_callback_confirmed") != NULL; + oauth_proxy_set_oauth10a (proxy, g_hash_table_lookup (form, "oauth_callback_confirmed") != NULL); g_hash_table_destroy (form); } diff --git a/rest/oauth-proxy-private.h b/rest/oauth-proxy-private.h deleted file mode 100644 index 2d178a5..0000000 --- a/rest/oauth-proxy-private.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * librest - RESTful web services access - * Copyright (c) 2008, 2009, Intel Corporation. - * - * Authors: Rob Bradford <rob@linux.intel.com> - * Ross Burton <ross@linux.intel.com> - * - * This program is free software; you can redistribute it and/or modify it - * under the terms and conditions of the GNU Lesser General Public License, - * version 2.1, as published by the Free Software Foundation. - * - * This program is distributed in the hope it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for - * more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. - * - */ - -#include "oauth-proxy.h" - -#define PROXY_GET_PRIVATE(o) \ - (G_TYPE_INSTANCE_GET_PRIVATE ((o), OAUTH_TYPE_PROXY, OAuthProxyPrivate)) - -typedef struct { - /* Application "consumer" keys */ - char *consumer_key; - char *consumer_secret; - /* Authorisation "user" tokens */ - char *token; - char *token_secret; - /* How we're signing */ - OAuthSignatureMethod method; - /* OAuth 1.0a */ - gboolean oauth_10a; - char *verifier; - /* OAuth Echo */ - gboolean oauth_echo; - char *service_url; - /* URL to use for signatures */ - char *signature_host; -} OAuthProxyPrivate; diff --git a/rest/oauth-proxy.c b/rest/oauth-proxy.c index f8c0053..d81c0d7 100644 --- a/rest/oauth-proxy.c +++ b/rest/oauth-proxy.c @@ -23,10 +23,32 @@ #include <rest/rest-proxy.h> #include <libsoup/soup.h> #include "oauth-proxy.h" -#include "oauth-proxy-private.h" #include "oauth-proxy-call.h" -G_DEFINE_TYPE (OAuthProxy, oauth_proxy, REST_TYPE_PROXY) +typedef struct { + /* Application "consumer" keys */ + char *consumer_key; + char *consumer_secret; + /* Authorisation "user" tokens */ + char *token; + char *token_secret; + /* How we're signing */ + OAuthSignatureMethod method; + /* OAuth 1.0a */ + gboolean oauth_10a; + char *verifier; + /* OAuth Echo */ + gboolean oauth_echo; + char *service_url; + /* URL to use for signatures */ + char *signature_host; +} OAuthProxyPrivate; + +struct _OAuthProxy { + RestProxy parent_instance; +}; + +G_DEFINE_TYPE_WITH_PRIVATE (OAuthProxy, oauth_proxy, REST_TYPE_PROXY) enum { PROP_0, @@ -54,7 +76,8 @@ static void oauth_proxy_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec) { - OAuthProxyPrivate *priv = PROXY_GET_PRIVATE (object); + OAuthProxy *self = OAUTH_PROXY (object); + OAuthProxyPrivate *priv = oauth_proxy_get_instance_private (self); switch (property_id) { case PROP_CONSUMER_KEY: @@ -84,7 +107,8 @@ static void oauth_proxy_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) { - OAuthProxyPrivate *priv = PROXY_GET_PRIVATE (object); + OAuthProxy *self = OAUTH_PROXY (object); + OAuthProxyPrivate *priv = oauth_proxy_get_instance_private (self); switch (property_id) { case PROP_CONSUMER_KEY: @@ -123,7 +147,8 @@ oauth_proxy_set_property (GObject *object, guint property_id, static void oauth_proxy_finalize (GObject *object) { - OAuthProxyPrivate *priv = PROXY_GET_PRIVATE (object); + OAuthProxy *self = OAUTH_PROXY (object); + OAuthProxyPrivate *priv = oauth_proxy_get_instance_private (self); g_free (priv->consumer_key); g_free (priv->consumer_secret); @@ -146,8 +171,6 @@ oauth_proxy_class_init (OAuthProxyClass *klass) RestProxyClass *proxy_class = REST_PROXY_CLASS (klass); GParamSpec *pspec; - g_type_class_add_private (klass, sizeof (OAuthProxyPrivate)); - object_class->get_property = oauth_proxy_get_property; object_class->set_property = oauth_proxy_set_property; object_class->finalize = oauth_proxy_finalize; @@ -201,7 +224,8 @@ oauth_proxy_class_init (OAuthProxyClass *klass) static void oauth_proxy_init (OAuthProxy *self) { - PROXY_GET_PRIVATE (self)->method = HMAC_SHA1; + OAuthProxyPrivate *priv = oauth_proxy_get_instance_private (self); + priv->method = HMAC_SHA1; } /** @@ -534,6 +558,22 @@ oauth_proxy_access_token_finish (OAuthProxy *proxy, return g_task_propagate_boolean (G_TASK (result), error); } +const char * +oauth_proxy_get_consumer_key (OAuthProxy *self) +{ + OAuthProxyPrivate *priv = oauth_proxy_get_instance_private (self); + + return priv->consumer_key; +} + +const char * +oauth_proxy_get_consumer_secret (OAuthProxy *self) +{ + OAuthProxyPrivate *priv = oauth_proxy_get_instance_private (self); + + return priv->consumer_secret; +} + /** * oauth_proxy_get_token: * @proxy: an #OAuthProxy @@ -546,7 +586,8 @@ oauth_proxy_access_token_finish (OAuthProxy *proxy, const char * oauth_proxy_get_token (OAuthProxy *proxy) { - OAuthProxyPrivate *priv = PROXY_GET_PRIVATE (proxy); + OAuthProxyPrivate *priv = oauth_proxy_get_instance_private (proxy); + return priv->token; } @@ -560,10 +601,9 @@ oauth_proxy_get_token (OAuthProxy *proxy) void oauth_proxy_set_token (OAuthProxy *proxy, const char *token) { - OAuthProxyPrivate *priv; + OAuthProxyPrivate *priv = oauth_proxy_get_instance_private (proxy); g_return_if_fail (OAUTH_IS_PROXY (proxy)); - priv = PROXY_GET_PRIVATE (proxy); g_free (priv->token); priv->token = g_strdup (token); @@ -581,7 +621,8 @@ oauth_proxy_set_token (OAuthProxy *proxy, const char *token) const char * oauth_proxy_get_token_secret (OAuthProxy *proxy) { - OAuthProxyPrivate *priv = PROXY_GET_PRIVATE (proxy); + OAuthProxyPrivate *priv = oauth_proxy_get_instance_private (proxy); + return priv->token_secret; } @@ -595,10 +636,9 @@ oauth_proxy_get_token_secret (OAuthProxy *proxy) void oauth_proxy_set_token_secret (OAuthProxy *proxy, const char *token_secret) { - OAuthProxyPrivate *priv; + OAuthProxyPrivate *priv = oauth_proxy_get_instance_private (proxy); g_return_if_fail (OAUTH_IS_PROXY (proxy)); - priv = PROXY_GET_PRIVATE (proxy); if (priv->token_secret) g_free (priv->token_secret); @@ -619,9 +659,22 @@ oauth_proxy_set_token_secret (OAuthProxy *proxy, const char *token_secret) gboolean oauth_proxy_is_oauth10a (OAuthProxy *proxy) { + OAuthProxyPrivate *priv = oauth_proxy_get_instance_private (proxy); + g_return_val_if_fail (OAUTH_IS_PROXY (proxy), FALSE); - return PROXY_GET_PRIVATE (proxy)->oauth_10a; + return priv->oauth_10a; +} + +void +oauth_proxy_set_oauth10a (OAuthProxy *self, + gboolean oauth10a) +{ + OAuthProxyPrivate *priv = oauth_proxy_get_instance_private (self); + + g_return_if_fail (OAUTH_IS_PROXY (self)); + + priv->oauth_10a = oauth10a; } /** @@ -636,10 +689,9 @@ oauth_proxy_is_oauth10a (OAuthProxy *proxy) const char * oauth_proxy_get_signature_host (OAuthProxy *proxy) { - OAuthProxyPrivate *priv; + OAuthProxyPrivate *priv = oauth_proxy_get_instance_private (proxy); g_return_val_if_fail (OAUTH_IS_PROXY (proxy), NULL); - priv = PROXY_GET_PRIVATE (proxy); return priv->signature_host; } @@ -655,10 +707,9 @@ void oauth_proxy_set_signature_host (OAuthProxy *proxy, const char *signature_host) { - OAuthProxyPrivate *priv; + OAuthProxyPrivate *priv = oauth_proxy_get_instance_private (proxy); g_return_if_fail (OAUTH_IS_PROXY (proxy)); - priv = PROXY_GET_PRIVATE (proxy); g_free (priv->signature_host); @@ -686,14 +737,13 @@ oauth_proxy_new_echo_proxy (OAuthProxy *proxy, gboolean binding_required) { OAuthProxy *echo_proxy; - OAuthProxyPrivate *priv, *echo_priv; + OAuthProxyPrivate *echo_priv; + OAuthProxyPrivate *priv = oauth_proxy_get_instance_private (proxy); g_return_val_if_fail (OAUTH_IS_PROXY (proxy), NULL); g_return_val_if_fail (service_url, NULL); g_return_val_if_fail (url_format, NULL); - priv = PROXY_GET_PRIVATE (proxy); - echo_proxy = g_object_new (OAUTH_TYPE_PROXY, "url-format", url_format, "binding-required", binding_required, @@ -703,7 +753,7 @@ oauth_proxy_new_echo_proxy (OAuthProxy *proxy, "token", priv->token, "token-secret", priv->token_secret, NULL); - echo_priv = PROXY_GET_PRIVATE (echo_proxy); + echo_priv = oauth_proxy_get_instance_private (echo_proxy); echo_priv->oauth_echo = TRUE; echo_priv->service_url = g_strdup (service_url); @@ -728,3 +778,26 @@ oauth_signature_method_get_type (void) return enum_type_id; } +gboolean +oauth_proxy_is_echo (OAuthProxy *self) +{ + OAuthProxyPrivate *priv = oauth_proxy_get_instance_private (self); + + return priv->oauth_echo; +} + +const char * +oauth_proxy_get_service_url (OAuthProxy *self) +{ + OAuthProxyPrivate *priv = oauth_proxy_get_instance_private (self); + + return priv->service_url; +} + +OAuthSignatureMethod +oauth_proxy_get_sign_method (OAuthProxy *self) +{ + OAuthProxyPrivate *priv = oauth_proxy_get_instance_private (self); + + return priv->method; +} diff --git a/rest/oauth-proxy.h b/rest/oauth-proxy.h index fa1a89e..2616a34 100644 --- a/rest/oauth-proxy.h +++ b/rest/oauth-proxy.h @@ -28,37 +28,7 @@ G_BEGIN_DECLS #define OAUTH_TYPE_PROXY oauth_proxy_get_type() - -#define OAUTH_PROXY(obj) \ - (G_TYPE_CHECK_INSTANCE_CAST ((obj), OAUTH_TYPE_PROXY, OAuthProxy)) - -#define OAUTH_PROXY_CLASS(klass) \ - (G_TYPE_CHECK_CLASS_CAST ((klass), OAUTH_TYPE_PROXY, OAuthProxyClass)) - -#define OAUTH_IS_PROXY(obj) \ - (G_TYPE_CHECK_INSTANCE_TYPE ((obj), OAUTH_TYPE_PROXY)) - -#define OAUTH_IS_PROXY_CLASS(klass) \ - (G_TYPE_CHECK_CLASS_TYPE ((klass), OAUTH_TYPE_PROXY)) - -#define OAUTH_PROXY_GET_CLASS(obj) \ - (G_TYPE_INSTANCE_GET_CLASS ((obj), OAUTH_TYPE_PROXY, OAuthProxyClass)) - -/** - * OAuthProxy: - * - * #OAuthProxy has no publicly available members. - */ -typedef struct { - RestProxy parent; -} OAuthProxy; - -typedef struct { - RestProxyClass parent_class; - /*< private >*/ - /* padding for future expansion */ - gpointer _padding_dummy[8]; -} OAuthProxyClass; +G_DECLARE_FINAL_TYPE (OAuthProxy, oauth_proxy, OAUTH, PROXY, RestProxy) GType oauth_signature_method_get_type (void) G_GNUC_CONST; #define OAUTH_TYPE_SIGNATURE_METHOD (oauth_signature_method_get_type()) @@ -77,68 +47,63 @@ typedef enum { HMAC_SHA1 } OAuthSignatureMethod; -GType oauth_proxy_get_type (void); - -RestProxy* oauth_proxy_new (const char *consumer_key, - const char *consumer_secret, - const gchar *url_format, - gboolean binding_required); - -RestProxy* oauth_proxy_new_with_token (const char *consumer_key, - const char *consumer_secret, - const char *token, - const char *token_secret, - const gchar *url_format, - gboolean binding_required); - -gboolean oauth_proxy_request_token (OAuthProxy *proxy, - const char *function, - const char *callback_uri, - GError **error); - -void oauth_proxy_request_token_async (OAuthProxy *proxy, - const char *function, - const char *callback_uri, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); -gboolean oauth_proxy_request_token_finish (OAuthProxy *proxy, - GAsyncResult *result, - GError **error); - -gboolean oauth_proxy_is_oauth10a (OAuthProxy *proxy); - -gboolean oauth_proxy_access_token (OAuthProxy *proxy, - const char *function, - const char *verifier, - GError **error); - -void oauth_proxy_access_token_async (OAuthProxy *proxy, - const char *function, - const char *verifier, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer user_data); -gboolean oauth_proxy_access_token_finish (OAuthProxy *proxy, - GAsyncResult *result, - GError **error); - -const char * oauth_proxy_get_token (OAuthProxy *proxy); - -void oauth_proxy_set_token (OAuthProxy *proxy, const char *token); - -const char * oauth_proxy_get_token_secret (OAuthProxy *proxy); - -void oauth_proxy_set_token_secret (OAuthProxy *proxy, const char *token_secret); -const char * oauth_proxy_get_signature_host (OAuthProxy *proxy); - -void oauth_proxy_set_signature_host (OAuthProxy *proxy, - const char *signature_host); - -RestProxy *oauth_proxy_new_echo_proxy (OAuthProxy *proxy, - const char *service_url, - const gchar *url_format, - gboolean binding_required); +RestProxy *oauth_proxy_new (const char *consumer_key, + const char *consumer_secret, + const gchar *url_format, + gboolean binding_required); +RestProxy *oauth_proxy_new_with_token (const char *consumer_key, + const char *consumer_secret, + const char *token, + const char *token_secret, + const gchar *url_format, + gboolean binding_required); +gboolean oauth_proxy_request_token (OAuthProxy *proxy, + const char *function, + const char *callback_uri, + GError **error); +void oauth_proxy_request_token_async (OAuthProxy *proxy, + const char *function, + const char *callback_uri, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data); +gboolean oauth_proxy_request_token_finish (OAuthProxy *proxy, + GAsyncResult *result, + GError **error); +gboolean oauth_proxy_is_oauth10a (OAuthProxy *proxy); +void oauth_proxy_set_oauth10a (OAuthProxy *proxy, + gboolean oauth10a); +gboolean oauth_proxy_access_token (OAuthProxy *proxy, + const char *function, + const char *verifier, + GError **error); +void oauth_proxy_access_token_async (OAuthProxy *proxy, + const char *function, + const char *verifier, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data); +gboolean oauth_proxy_access_token_finish (OAuthProxy *proxy, + GAsyncResult *result, + GError **error); +const char *oauth_proxy_get_consumer_key (OAuthProxy *proxy); +const char *oauth_proxy_get_consumer_secret (OAuthProxy *proxy); +const char *oauth_proxy_get_token (OAuthProxy *proxy); +void oauth_proxy_set_token (OAuthProxy *proxy, + const char *token); +const char *oauth_proxy_get_token_secret (OAuthProxy *proxy); +void oauth_proxy_set_token_secret (OAuthProxy *proxy, + const char *token_secret); +const char *oauth_proxy_get_signature_host (OAuthProxy *proxy); +void oauth_proxy_set_signature_host (OAuthProxy *proxy, + const char *signature_host); +RestProxy *oauth_proxy_new_echo_proxy (OAuthProxy *proxy, + const char *service_url, + const gchar *url_format, + gboolean binding_required); +gboolean oauth_proxy_is_echo (OAuthProxy *proxy); +const char *oauth_proxy_get_service_url (OAuthProxy *proxy); +OAuthSignatureMethod oauth_proxy_get_sign_method (OAuthProxy *proxy); G_END_DECLS diff --git a/rest/oauth2-proxy-call.c b/rest/oauth2-proxy-call.c deleted file mode 100644 index 2d71b45..0000000 --- a/rest/oauth2-proxy-call.c +++ /dev/null @@ -1,68 +0,0 @@ -/* - * librest - RESTful web services access - * Copyright (c) 2008, 2009, Intel Corporation. - * - * Authors: Rob Bradford <rob@linux.intel.com> - * Ross Burton <ross@linux.intel.com> - * Jonathon Jongsma <jonathon.jongsma@collabora.co.uk> - * - * This program is free software; you can redistribute it and/or modify it - * under the terms and conditions of the GNU Lesser General Public License, - * version 2.1, as published by the Free Software Foundation. - * - * This program is distributed in the hope it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for - * more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. - * - */ - -#include <string.h> -#include <libsoup/soup.h> -#include <rest/rest-proxy-call.h> -#include "oauth2-proxy-call.h" -#include "oauth2-proxy-private.h" -#include "sha1.h" - -G_DEFINE_TYPE (OAuth2ProxyCall, oauth2_proxy_call, REST_TYPE_PROXY_CALL) - -static gboolean -_prepare (RestProxyCall *call, GError **error) -{ - OAuth2Proxy *proxy = NULL; - gboolean result = TRUE; - - g_object_get (call, "proxy", &proxy, NULL); - - if (!proxy->priv->access_token) { - g_set_error (error, - REST_PROXY_CALL_ERROR, - REST_PROXY_CALL_FAILED, - "Missing access token, web service not properly authenticated"); - result = FALSE; - } else { - rest_proxy_call_add_param (call, "access_token", proxy->priv->access_token); - } - - g_object_unref (proxy); - - return result; -} - -static void -oauth2_proxy_call_class_init (OAuth2ProxyCallClass *klass) -{ - RestProxyCallClass *call_class = REST_PROXY_CALL_CLASS (klass); - - call_class->prepare = _prepare; -} - -static void -oauth2_proxy_call_init (OAuth2ProxyCall *self) -{ -} - diff --git a/rest/oauth2-proxy-call.h b/rest/oauth2-proxy-call.h deleted file mode 100644 index 805a501..0000000 --- a/rest/oauth2-proxy-call.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * librest - RESTful web services access - * Copyright (c) 2008, 2009, Intel Corporation. - * - * Authors: Rob Bradford <rob@linux.intel.com> - * Ross Burton <ross@linux.intel.com> - * Jonathon Jongsma <jonathon.jongsma@collabora.co.uk> - * - * This program is free software; you can redistribute it and/or modify it - * under the terms and conditions of the GNU Lesser General Public License, - * version 2.1, as published by the Free Software Foundation. - * - * This program is distributed in the hope it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for - * more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. - * - */ - -#ifndef _OAUTH2_PROXY_CALL -#define _OAUTH2_PROXY_CALL - -#include <rest/rest-proxy-call.h> - -G_BEGIN_DECLS - -#define OAUTH2_TYPE_PROXY_CALL oauth2_proxy_call_get_type() -G_DECLARE_DERIVABLE_TYPE (OAuth2ProxyCall, oauth2_proxy_call, OAUTH2, PROXY_CALL, RestProxyCall) - -/** - * OAuth2ProxyCall: - * - * #OAuth2ProxyCall has no publicly available members. - */ - -struct _OAuth2ProxyCallClass { - RestProxyCallClass parent_class; - /*< private >*/ - /* padding for future expansion */ - gpointer _padding_dummy[8]; -}; - -G_END_DECLS - -#endif /* _OAUTH2_PROXY_CALL */ diff --git a/rest/oauth2-proxy.c b/rest/oauth2-proxy.c deleted file mode 100644 index 3382f8b..0000000 --- a/rest/oauth2-proxy.c +++ /dev/null @@ -1,397 +0,0 @@ -/* - * librest - RESTful web services access - * Copyright (c) 2008, 2009, 2010 Intel Corporation. - * - * Authors: Rob Bradford <rob@linux.intel.com> - * Ross Burton <ross@linux.intel.com> - * Jonathon Jongsma <jonathon.jongsma@collabora.co.uk> - * - * This program is free software; you can redistribute it and/or modify it - * under the terms and conditions of the GNU Lesser General Public License, - * version 2.1, as published by the Free Software Foundation. - * - * This program is distributed in the hope it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for - * more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. - * - */ - -#include <rest/rest-proxy.h> -#include <libsoup/soup.h> -#include "oauth2-proxy.h" -#include "oauth2-proxy-private.h" -#include "oauth2-proxy-call.h" - -#define GET_PRIVATE(o) oauth2_proxy_get_instance_private(OAUTH2_PROXY(o)) - -G_DEFINE_TYPE_WITH_PRIVATE (OAuth2Proxy, oauth2_proxy, REST_TYPE_PROXY) - -GQuark -oauth2_proxy_error_quark (void) -{ - return g_quark_from_static_string ("rest-oauth2-proxy"); -} - -enum { - PROP_0, - PROP_CLIENT_ID, - PROP_AUTH_ENDPOINT, - PROP_ACCESS_TOKEN -}; - -static RestProxyCall * -_new_call (RestProxy *proxy) -{ - RestProxyCall *call; - - call = g_object_new (OAUTH2_TYPE_PROXY_CALL, - "proxy", proxy, - NULL); - - return call; -} - -static void -oauth2_proxy_get_property (GObject *object, guint property_id, - GValue *value, GParamSpec *pspec) -{ - OAuth2ProxyPrivate *priv = ((OAuth2Proxy*)object)->priv; - - switch (property_id) { - case PROP_CLIENT_ID: - g_value_set_string (value, priv->client_id); - break; - case PROP_AUTH_ENDPOINT: - g_value_set_string (value, priv->auth_endpoint); - break; - case PROP_ACCESS_TOKEN: - g_value_set_string (value, priv->access_token); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); - } -} - -static void -oauth2_proxy_set_property (GObject *object, guint property_id, - const GValue *value, GParamSpec *pspec) -{ - OAuth2ProxyPrivate *priv = ((OAuth2Proxy*)object)->priv; - - switch (property_id) { - case PROP_CLIENT_ID: - if (priv->client_id) - g_free (priv->client_id); - priv->client_id = g_value_dup_string (value); - break; - case PROP_AUTH_ENDPOINT: - if (priv->auth_endpoint) - g_free (priv->auth_endpoint); - priv->auth_endpoint = g_value_dup_string (value); - break; - case PROP_ACCESS_TOKEN: - if (priv->access_token) - g_free (priv->access_token); - priv->access_token = g_value_dup_string (value); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); - } -} - -static void -oauth2_proxy_finalize (GObject *object) -{ - OAuth2ProxyPrivate *priv = ((OAuth2Proxy*)object)->priv; - - g_free (priv->client_id); - g_free (priv->auth_endpoint); - g_free (priv->access_token); - - G_OBJECT_CLASS (oauth2_proxy_parent_class)->finalize (object); -} - -static void -oauth2_proxy_class_init (OAuth2ProxyClass *klass) -{ - GObjectClass *object_class = G_OBJECT_CLASS (klass); - RestProxyClass *proxy_class = REST_PROXY_CLASS (klass); - GParamSpec *pspec; - - object_class->get_property = oauth2_proxy_get_property; - object_class->set_property = oauth2_proxy_set_property; - object_class->finalize = oauth2_proxy_finalize; - - proxy_class->new_call = _new_call; - - pspec = g_param_spec_string ("client-id", "client-id", - "The client (application) id", NULL, - G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY|G_PARAM_STATIC_STRINGS); - g_object_class_install_property (object_class, - PROP_CLIENT_ID, - pspec); - - pspec = g_param_spec_string ("auth-endpoint", "auth-endpoint", - "The authentication endpoint url", NULL, - G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY|G_PARAM_STATIC_STRINGS); - g_object_class_install_property (object_class, - PROP_AUTH_ENDPOINT, - pspec); - - pspec = g_param_spec_string ("access-token", "access-token", - "The request or access token", NULL, - G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS); - g_object_class_install_property (object_class, - PROP_ACCESS_TOKEN, - pspec); -} - -static void -oauth2_proxy_init (OAuth2Proxy *proxy) -{ - proxy->priv = GET_PRIVATE (proxy); -} - -/** - * oauth2_proxy_new: - * @client_id: the client (application) id - * @auth_endpoint: the authentication endpoint URL - * @url_format: the endpoint URL - * @binding_required: whether the URL needs to be bound before calling - * - * Create a new #OAuth2Proxy for the specified endpoint @url_format, using the - * specified API key and secret. - * - * This proxy won't have the Token set so will be unauthorised. If the token is - * unknown then the following steps should be taken to acquire an access token: - * - Get the authentication url with oauth2_proxy_build_login_url() - * - Display this url in an embedded browser widget - * - wait for the browser widget to be redirected to the specified redirect_uri - * - extract the token from the fragment of the redirected uri (using - * convenience function oauth2_proxy_extract_access_token()) - * - set the token with oauth2_proxy_set_access_token() - * - * Set @binding_required to %TRUE if the URL contains string formatting - * operations (for example "http://foo.com/%<!-- -->s". These must be expanded - * using rest_proxy_bind() before invoking the proxy. - * - * Returns: A new #OAuth2Proxy. - */ -RestProxy * -oauth2_proxy_new (const char *client_id, - const char *auth_endpoint, - const gchar *url_format, - gboolean binding_required) -{ - return g_object_new (OAUTH2_TYPE_PROXY, - "client-id", client_id, - "auth-endpoint", auth_endpoint, - "url-format", url_format, - "binding-required", binding_required, - NULL); -} - -/** - * oauth2_proxy_new_with_token: - * @client_id: the client (application) id - * @access_token: the Access Token - * @auth_endpoint: the authentication endpoint URL - * @url_format: the endpoint URL - * @binding_required: whether the URL needs to be bound before calling - * - * Create a new #OAuth2Proxy for the specified endpoint @url_format, using the - * specified client id - * - * @access_token is used for the Access Token, so if they are still valid then - * this proxy is authorised. - * - * Set @binding_required to %TRUE if the URL contains string formatting - * operations (for example "http://foo.com/%<!-- -->s". These must be expanded - * using rest_proxy_bind() before invoking the proxy. - * - * Returns: A new #OAuth2Proxy. - */ -RestProxy * -oauth2_proxy_new_with_token (const char *client_id, - const char *access_token, - const char *auth_endpoint, - const gchar *url_format, - gboolean binding_required) -{ - return g_object_new (OAUTH2_TYPE_PROXY, - "client-id", client_id, - "access-token", access_token, - "auth-endpoint", auth_endpoint, - "url-format", url_format, - "binding-required", binding_required, - NULL); -} - -/* allocates a new string of the form "key=value" */ -static void -append_query_param (gpointer key, gpointer value, gpointer user_data) -{ - GString *params = (GString*) user_data; - char *encoded_val, *encoded_key; - char *param; - - encoded_val = g_uri_escape_string (value, NULL, TRUE); - encoded_key = g_uri_escape_string (key, NULL, TRUE); - - param = g_strdup_printf ("%s=%s", encoded_key, encoded_val); - g_free (encoded_key); - g_free (encoded_val); - - // if there's already a parameter in the string, we need to add a '&' - // separator before adding the new param - if (params->len) - g_string_append_c (params, '&'); - g_string_append (params, param); -} - -/** - * oauth2_proxy_build_login_url_full: - * @proxy: a OAuth2Proxy object - * @redirect_uri: the uri to redirect to after the user authenticates - * @extra_params: any extra parameters to add to the login url (e.g. facebook - * uses 'scope=foo,bar' to request extended permissions). - * - * Builds a url at which the user can log in to the specified OAuth2-based web - * service. In general, this url should be displayed in an embedded browser - * widget, and you should then intercept the browser's redirect to @redirect_uri - * and extract the access token from the url fragment. After the access token - * has been retrieved, call oauth2_proxy_set_access_token(). This must be done - * before making any API calls to the service. - * - * See the oauth2 spec for more details about the "user-agent" authentication - * flow. - * - * The @extra_params and @redirect_uri should not be uri-encoded, that will be - * done automatically - * - * Returns: a newly allocated uri string - */ -char * -oauth2_proxy_build_login_url_full (OAuth2Proxy *proxy, - const char* redirect_uri, - GHashTable* extra_params) -{ - char *url; - GString *params = 0; - char *encoded_uri, *encoded_id; - - g_return_val_if_fail (proxy, NULL); - g_return_val_if_fail (redirect_uri, NULL); - - if (extra_params && g_hash_table_size (extra_params) > 0) { - params = g_string_new (NULL); - g_hash_table_foreach (extra_params, append_query_param, params); - } - - encoded_uri = g_uri_escape_string (redirect_uri, NULL, TRUE); - encoded_id = g_uri_escape_string (proxy->priv->client_id, NULL, TRUE); - - url = g_strdup_printf ("%s?client_id=%s&redirect_uri=%s&type=user_agent", - proxy->priv->auth_endpoint, encoded_id, - encoded_uri); - - g_free (encoded_uri); - g_free (encoded_id); - - if (params) { - char * full_url = g_strdup_printf ("%s&%s", url, params->str); - g_free (url); - url = full_url; - g_string_free (params, TRUE); - } - - return url; -} - -/** - * oauth2_proxy_build_login_url: - * @proxy: an OAuth2Proxy object - * @redirect_uri: the uri to redirect to after the user authenticates - * - * Builds a url at which the user can log in to the specified OAuth2-based web - * service. See the documentation for oauth2_proxy_build_login_url_full() for - * detailed information. - * - * Returns: a newly allocated uri string - */ -char * -oauth2_proxy_build_login_url (OAuth2Proxy *proxy, - const char* redirect_uri) -{ - return oauth2_proxy_build_login_url_full (proxy, redirect_uri, NULL); -} - -/** - * oauth2_proxy_get_access_token: - * @proxy: an #OAuth2Proxy - * - * Get the current request or access token. - * - * Returns: the token, or %NULL if there is no token yet. This string is owned - * by #OAuth2Proxy and should not be freed. - */ -const char * -oauth2_proxy_get_access_token (OAuth2Proxy *proxy) -{ - return proxy->priv->access_token; -} - -/** - * oauth2_proxy_set_access_token: - * @proxy: an #OAuth2Proxy - * @access_token: the access token - * - * Set the access token. - */ -void -oauth2_proxy_set_access_token (OAuth2Proxy *proxy, const char *access_token) -{ - g_return_if_fail (OAUTH2_IS_PROXY (proxy)); - - if (proxy->priv->access_token) - g_free (proxy->priv->access_token); - - proxy->priv->access_token = g_strdup (access_token); -} - -/** - * oauth2_proxy_extract_access_token: - * @url: the url which contains an access token in its fragment - * - * A utility function to extract the access token from the url that results from - * the redirection after the user authenticates - */ -char * -oauth2_proxy_extract_access_token (const char *url) -{ - GHashTable *params; - char *token = NULL; - const char *fragment; - GUri *uri = g_uri_parse (url, G_URI_FLAGS_ENCODED, NULL); - - fragment = g_uri_get_fragment (uri); - if (fragment != NULL) { - params = soup_form_decode (fragment); - - if (params) { - char *encoded = g_hash_table_lookup (params, "access_token"); - if (encoded) - token = g_uri_unescape_string (encoded, NULL); - - g_hash_table_destroy (params); - } - } - g_uri_unref (uri); - - return token; -} diff --git a/rest/oauth2-proxy.h b/rest/oauth2-proxy.h deleted file mode 100644 index 0473c62..0000000 --- a/rest/oauth2-proxy.h +++ /dev/null @@ -1,95 +0,0 @@ -/* - * librest - RESTful web services access - * Copyright (c) 2008, 2009, 2010 Intel Corporation. - * - * Authors: Rob Bradford <rob@linux.intel.com> - * Ross Burton <ross@linux.intel.com> - * Jonathon Jongsma <jonathon.jongsma@collabora.co.uk> - * - * This program is free software; you can redistribute it and/or modify it - * under the terms and conditions of the GNU Lesser General Public License, - * version 2.1, as published by the Free Software Foundation. - * - * This program is distributed in the hope it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for - * more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. - * - */ - -#ifndef _OAUTH2_PROXY -#define _OAUTH2_PROXY - -#include <rest/rest-proxy.h> - -G_BEGIN_DECLS - -#define OAUTH2_TYPE_PROXY oauth2_proxy_get_type() - -#define OAUTH2_PROXY(obj) \ - (G_TYPE_CHECK_INSTANCE_CAST ((obj), OAUTH2_TYPE_PROXY, OAuth2Proxy)) - -#define OAUTH2_PROXY_CLASS(klass) \ - (G_TYPE_CHECK_CLASS_CAST ((klass), OAUTH2_TYPE_PROXY, OAuth2ProxyClass)) - -#define OAUTH2_IS_PROXY(obj) \ - (G_TYPE_CHECK_INSTANCE_TYPE ((obj), OAUTH2_TYPE_PROXY)) - -#define OAUTH2_IS_PROXY_CLASS(klass) \ - (G_TYPE_CHECK_CLASS_TYPE ((klass), OAUTH2_TYPE_PROXY)) - -#define OAUTH2_PROXY_GET_CLASS(obj) \ - (G_TYPE_INSTANCE_GET_CLASS ((obj), OAUTH2_TYPE_PROXY, OAuth2ProxyClass)) - -typedef struct _OAuth2ProxyPrivate OAuth2ProxyPrivate; - -/** - * OAuth2Proxy: - * - * #OAuth2Proxy has no publicly available members. - */ -typedef struct { - RestProxy parent; - OAuth2ProxyPrivate *priv; -} OAuth2Proxy; - -typedef struct { - RestProxyClass parent_class; - /*< private >*/ - /* padding for future expansion */ - gpointer _padding_dummy[8]; -} OAuth2ProxyClass; - -GType oauth2_proxy_get_type (void); - -RestProxy* oauth2_proxy_new (const char *client_id, - const char *auth_endpoint, - const gchar *url_format, - gboolean binding_required); - -RestProxy* oauth2_proxy_new_with_token (const char *client_id, - const char *access_token, - const char *auth_endpoint, - const gchar *url_format, - gboolean binding_required); - -char * oauth2_proxy_build_login_url_full (OAuth2Proxy *proxy, - const char* redirect_uri, - GHashTable* extra_params); - -char * oauth2_proxy_build_login_url (OAuth2Proxy *proxy, - const char* redirect_uri); - -const char * oauth2_proxy_get_access_token (OAuth2Proxy *proxy); - -void oauth2_proxy_set_access_token (OAuth2Proxy *proxy, const char *access_token); - -char * oauth2_proxy_extract_access_token (const char *url); - -G_END_DECLS - -#endif /* _OAUTH2_PROXY */ diff --git a/rest/rest-oauth2-proxy-call.c b/rest/rest-oauth2-proxy-call.c new file mode 100644 index 0000000..88840b3 --- /dev/null +++ b/rest/rest-oauth2-proxy-call.c @@ -0,0 +1,62 @@ +/* rest-oauth2-proxy-call.c + * + * Copyright 2021-2022 Günther Wagner <info@gunibert.de> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU Lesser General Public License, + * version 2.1, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for + * more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "rest-oauth2-proxy-call.h" + +G_DEFINE_TYPE (RestOAuth2ProxyCall, rest_oauth2_proxy_call, REST_TYPE_PROXY_CALL) + +static gboolean +rest_oauth2_proxy_call_prepare (RestProxyCall *call, + GError **error) +{ + RestOAuth2Proxy *proxy = NULL; + g_autoptr(GDateTime) now = NULL; + GDateTime *expiration_date = NULL; + + g_return_val_if_fail (REST_IS_OAUTH2_PROXY_CALL (call), FALSE); + + g_object_get (call, "proxy", &proxy, NULL); + + now = g_date_time_new_now_local (); + expiration_date = rest_oauth2_proxy_get_expiration_date (proxy); + + // access token expired -> refresh + if (g_date_time_compare (now, expiration_date) > 0) + { + g_set_error (error, + REST_OAUTH2_ERROR, + REST_OAUTH2_ERROR_ACCESS_TOKEN_EXPIRED, + "Access token is expired"); + return FALSE; + } + + return TRUE; +} + +static void +rest_oauth2_proxy_call_class_init (RestOAuth2ProxyCallClass *klass) +{ + RestProxyCallClass *call_class = REST_PROXY_CALL_CLASS (klass); + + call_class->prepare = rest_oauth2_proxy_call_prepare; +} + +static void +rest_oauth2_proxy_call_init (RestOAuth2ProxyCall *self) +{ +} diff --git a/rest/oauth2-proxy-private.h b/rest/rest-oauth2-proxy-call.h index 565c1c3..fafc61f 100644 --- a/rest/oauth2-proxy-private.h +++ b/rest/rest-oauth2-proxy-call.h @@ -1,10 +1,6 @@ -/* - * librest - RESTful web services access - * Copyright (c) 2008, 2009, 2010 Intel Corporation. +/* rest-oauth2-proxy-call.h * - * Authors: Rob Bradford <rob@linux.intel.com> - * Ross Burton <ross@linux.intel.com> - * Jonathon Jongsma <jonathon.jongsma@collabora.co.uk> + * Copyright 2021 Günther Wagner <info@gunibert.de> * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU Lesser General Public License, @@ -18,17 +14,20 @@ * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. - * */ -#ifndef _OAUTH2_PROXY_PRIVATE -#define _OAUTH2_PROXY_PRIVATE -#include "oauth2-proxy.h" +#pragma once + +#include <rest.h> + +G_BEGIN_DECLS + +#define REST_TYPE_OAUTH2_PROXY_CALL (rest_oauth2_proxy_call_get_type()) + +G_DECLARE_DERIVABLE_TYPE (RestOAuth2ProxyCall, rest_oauth2_proxy_call, REST, OAUTH2_PROXY_CALL, RestProxyCall) -struct _OAuth2ProxyPrivate { - char *client_id; - char *auth_endpoint; - char *access_token; +struct _RestOAuth2ProxyCallClass { + RestProxyCallClass parent_class; }; -#endif /* _OAUTH2_PROXY_PRIVATE */ +G_END_DECLS diff --git a/rest/rest-oauth2-proxy.c b/rest/rest-oauth2-proxy.c new file mode 100644 index 0000000..f15b589 --- /dev/null +++ b/rest/rest-oauth2-proxy.c @@ -0,0 +1,802 @@ +/* rest-oauth2-proxy.c + * + * Copyright 2021 Günther Wagner <info@gunibert.de> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU Lesser General Public License, + * version 2.1, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for + * more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "rest-oauth2-proxy.h" +#include "rest-oauth2-proxy-call.h" +#include "rest-utils.h" +#include "rest-private.h" +#include <json-glib/json-glib.h> + +typedef struct +{ + gchar *authurl; + gchar *tokenurl; + gchar *redirect_uri; + gchar *client_id; + gchar *client_secret; + + gchar *access_token; + gchar *refresh_token; + + GDateTime *expiration_date; +} RestOAuth2ProxyPrivate; + +G_DEFINE_TYPE_WITH_PRIVATE (RestOAuth2Proxy, rest_oauth2_proxy, REST_TYPE_PROXY) + +G_DEFINE_QUARK (rest-oauth2-error-quark, rest_oauth2_error) + +enum { + PROP_0, + PROP_AUTH_URL, + PROP_TOKEN_URL, + PROP_REDIRECT_URI, + PROP_CLIENT_ID, + PROP_CLIENT_SECRET, + PROP_ACCESS_TOKEN, + PROP_REFRESH_TOKEN, + PROP_EXPIRATION_DATE, + N_PROPS +}; + +static GParamSpec *properties [N_PROPS]; + +static void +rest_oauth2_proxy_parse_access_token (RestOAuth2Proxy *self, + GBytes *payload, + GTask *task) +{ + g_autoptr(JsonParser) parser = NULL; + g_autoptr(GError) error = NULL; + JsonNode *root; + JsonObject *root_object; + const gchar *data; + gsize size; + gint expires_in; + gint created_at; + + g_return_if_fail (REST_IS_OAUTH2_PROXY (self)); + + data = g_bytes_get_data (payload, &size); + + parser = json_parser_new (); + json_parser_load_from_data (parser, data, size, &error); + if (error != NULL) + { + g_task_return_error (task, error); + return; + } + + root = json_parser_get_root (parser); + root_object = json_node_get_object (root); + + if (json_object_has_member (root_object, "access_token")) + rest_oauth2_proxy_set_access_token (self, json_object_get_string_member (root_object, "access_token")); + if (json_object_has_member (root_object, "refresh_token")) + rest_oauth2_proxy_set_refresh_token (self, json_object_get_string_member (root_object, "refresh_token")); + + if (json_object_has_member (root_object, "expires_in") && json_object_has_member (root_object, "created_at")) + { + expires_in = json_object_get_int_member (root_object, "expires_in"); + created_at = json_object_get_int_member (root_object, "created_at"); + + rest_oauth2_proxy_set_expiration_date (self, g_date_time_new_from_unix_local (created_at+expires_in)); + } + else if (json_object_has_member (root_object, "expires_in")) + { + g_autoptr(GDateTime) now = g_date_time_new_now_utc (); + expires_in = json_object_get_int_member (root_object, "expires_in"); + rest_oauth2_proxy_set_expiration_date (self, g_date_time_add_seconds (now, expires_in)); + } + + g_task_return_boolean (task, TRUE); +} + +RestProxyCall * +rest_oauth2_proxy_new_call (RestProxy *proxy) +{ + RestOAuth2Proxy *self = (RestOAuth2Proxy *)proxy; + RestProxyCall *call; + g_autofree gchar *auth; + + g_return_val_if_fail (REST_IS_OAUTH2_PROXY (self), NULL); + + auth = g_strdup_printf ("Bearer %s", rest_oauth2_proxy_get_access_token (self)); + + call = g_object_new (REST_TYPE_OAUTH2_PROXY_CALL, "proxy", proxy, NULL); + rest_proxy_call_add_header (call, "Authorization", auth); + + return call; +} + +/** + * rest_oauth2_proxy_new: + * + * Create a new #RestOAuth2Proxy. + * + * Returns: (transfer full): a newly created #RestOAuth2Proxy + */ +RestOAuth2Proxy * +rest_oauth2_proxy_new (const gchar *authurl, + const gchar *tokenurl, + const gchar *redirecturl, + const gchar *client_id, + const gchar *client_secret, + const gchar *baseurl) +{ + return g_object_new (REST_TYPE_OAUTH2_PROXY, + "url-format", baseurl, + "auth-url", authurl, + "token-url", tokenurl, + "redirect-uri", redirecturl, + "client-id", client_id, + "client-secret", client_secret, + NULL); +} + +static void +rest_oauth2_proxy_finalize (GObject *object) +{ + RestOAuth2Proxy *self = (RestOAuth2Proxy *)object; + RestOAuth2ProxyPrivate *priv = rest_oauth2_proxy_get_instance_private (self); + + g_clear_pointer (&priv->authurl, g_free); + g_clear_pointer (&priv->tokenurl, g_free); + g_clear_pointer (&priv->redirect_uri, g_free); + g_clear_pointer (&priv->client_id, g_free); + g_clear_pointer (&priv->client_secret, g_free); + g_clear_pointer (&priv->access_token, g_free); + g_clear_pointer (&priv->refresh_token, g_free); + g_clear_pointer (&priv->expiration_date, g_date_time_unref); + + G_OBJECT_CLASS (rest_oauth2_proxy_parent_class)->finalize (object); +} + +static void +rest_oauth2_proxy_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + RestOAuth2Proxy *self = REST_OAUTH2_PROXY (object); + + switch (prop_id) + { + case PROP_AUTH_URL: + g_value_set_string (value, rest_oauth2_proxy_get_auth_url (self)); + break; + case PROP_TOKEN_URL: + g_value_set_string (value, rest_oauth2_proxy_get_token_url (self)); + break; + case PROP_REDIRECT_URI: + g_value_set_string (value, rest_oauth2_proxy_get_redirect_uri (self)); + break; + case PROP_CLIENT_ID: + g_value_set_string (value, rest_oauth2_proxy_get_client_id (self)); + break; + case PROP_CLIENT_SECRET: + g_value_set_string (value, rest_oauth2_proxy_get_client_secret (self)); + break; + case PROP_ACCESS_TOKEN: + g_value_set_string (value, rest_oauth2_proxy_get_access_token (self)); + break; + case PROP_REFRESH_TOKEN: + g_value_set_string (value, rest_oauth2_proxy_get_refresh_token (self)); + break; + case PROP_EXPIRATION_DATE: + g_value_set_boxed (value, rest_oauth2_proxy_get_expiration_date (self)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +rest_oauth2_proxy_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + RestOAuth2Proxy *self = REST_OAUTH2_PROXY (object); + + switch (prop_id) + { + case PROP_AUTH_URL: + rest_oauth2_proxy_set_auth_url (self, g_value_get_string (value)); + break; + case PROP_TOKEN_URL: + rest_oauth2_proxy_set_token_url (self, g_value_get_string (value)); + break; + case PROP_REDIRECT_URI: + rest_oauth2_proxy_set_redirect_uri (self, g_value_get_string (value)); + break; + case PROP_CLIENT_ID: + rest_oauth2_proxy_set_client_id (self, g_value_get_string (value)); + break; + case PROP_CLIENT_SECRET: + rest_oauth2_proxy_set_client_secret (self, g_value_get_string (value)); + break; + case PROP_ACCESS_TOKEN: + rest_oauth2_proxy_set_access_token (self, g_value_get_string (value)); + break; + case PROP_REFRESH_TOKEN: + rest_oauth2_proxy_set_refresh_token (self, g_value_get_string (value)); + break; + case PROP_EXPIRATION_DATE: + rest_oauth2_proxy_set_expiration_date (self, g_value_get_boxed (value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +rest_oauth2_proxy_class_init (RestOAuth2ProxyClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + RestOAuth2ProxyClass *oauth2_class = REST_OAUTH2_PROXY_CLASS (klass); + RestProxyClass *proxy_class = REST_PROXY_CLASS (klass); + + object_class->finalize = rest_oauth2_proxy_finalize; + object_class->get_property = rest_oauth2_proxy_get_property; + object_class->set_property = rest_oauth2_proxy_set_property; + oauth2_class->parse_access_token = rest_oauth2_proxy_parse_access_token; + proxy_class->new_call = rest_oauth2_proxy_new_call; + + properties [PROP_AUTH_URL] = + g_param_spec_string ("auth-url", + "AuthUrl", + "AuthUrl", + "", + (G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + + properties [PROP_TOKEN_URL] = + g_param_spec_string ("token-url", + "TokenUrl", + "TokenUrl", + "", + (G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + + properties [PROP_REDIRECT_URI] = + g_param_spec_string ("redirect-uri", + "RedirectUri", + "RedirectUri", + "", + (G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + + properties [PROP_CLIENT_ID] = + g_param_spec_string ("client-id", + "ClientId", + "ClientId", + "", + (G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + + properties [PROP_CLIENT_SECRET] = + g_param_spec_string ("client-secret", + "ClientSecret", + "ClientSecret", + "", + (G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + + properties [PROP_ACCESS_TOKEN] = + g_param_spec_string ("access-token", + "AccessToken", + "AccessToken", + NULL, + (G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + + properties [PROP_REFRESH_TOKEN] = + g_param_spec_string ("refresh-token", + "RefreshToken", + "RefreshToken", + NULL, + (G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + + properties [PROP_EXPIRATION_DATE] = + g_param_spec_boxed ("expiration-date", + "ExpirationDate", + "ExpirationDate", + G_TYPE_DATE_TIME, + (G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + + g_object_class_install_properties (object_class, N_PROPS, properties); +} + +static void +rest_oauth2_proxy_init (RestOAuth2Proxy *self) +{ +} + +/** + * rest_oauth2_proxy_build_authorization_url: + * @self: a #RestOAuth2Proxy + * @code_challenge: the code challenge (see #RestPkceCodeChallenge) + * @scope: (nullable): the requesting scope of the resource + * @state: (out): a CRSF token which should be verified from the redirect_uri + * + * + * Returns: (transfer full): the authorization url which should be shown in a WebView in order to accept/decline the request + * to authorize the application + * + * Since: 0.8 + */ +gchar * +rest_oauth2_proxy_build_authorization_url (RestOAuth2Proxy *self, + const gchar *code_challenge, + const gchar *scope, + gchar **state) +{ + RestOAuth2ProxyPrivate *priv = rest_oauth2_proxy_get_instance_private (self); + g_autoptr(GHashTable) params = NULL; + g_autoptr(GUri) auth = NULL; + g_autoptr(GUri) authorization_url = NULL; + g_autofree gchar *params_string; + + g_return_val_if_fail (REST_IS_OAUTH2_PROXY (self), NULL); + + if (state != NULL) + *state = random_string (10); + params = g_hash_table_new (g_str_hash, g_str_equal); + + g_hash_table_insert (params, "response_type", "code"); + g_hash_table_insert (params, "client_id", priv->client_id); + g_hash_table_insert (params, "redirect_uri", priv->redirect_uri); + if (state != NULL) + g_hash_table_insert (params, "state", *state); + g_hash_table_insert (params, "code_challenge", (gchar *)code_challenge); + g_hash_table_insert (params, "code_challenge_method", "S256"); + if (scope) + g_hash_table_insert (params, "scope", (gchar *)scope); + + params_string = soup_form_encode_hash (params); + auth = g_uri_parse (priv->authurl, G_URI_FLAGS_NONE, NULL); + authorization_url = g_uri_build (G_URI_FLAGS_ENCODED, + g_uri_get_scheme (auth), + NULL, + g_uri_get_host (auth), + g_uri_get_port (auth), + g_uri_get_path (auth), + params_string, + NULL); + return g_uri_to_string (authorization_url); +} + +static void +rest_oauth2_proxy_fetch_access_token_cb (SoupMessage *msg, + GBytes *body, + GError *error, + gpointer user_data) +{ + g_autoptr(GTask) task = user_data; + RestOAuth2Proxy *self; + + g_assert (G_IS_TASK (task)); + + self = g_task_get_source_object (task); + + if (error) + { + g_task_return_error (task, error); + return; + } + + REST_OAUTH2_PROXY_GET_CLASS (self)->parse_access_token (self, body, g_steal_pointer (&task)); +} + +void +rest_oauth2_proxy_fetch_access_token_async (RestOAuth2Proxy *self, + const gchar *authorization_code, + const gchar *code_verifier, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) +{ + RestOAuth2ProxyPrivate *priv = rest_oauth2_proxy_get_instance_private (self); + g_autoptr(SoupMessage) msg = NULL; + g_autoptr(GTask) task = NULL; + g_autoptr(GHashTable) params = NULL; + + g_return_if_fail (REST_IS_OAUTH2_PROXY (self)); + g_return_if_fail (authorization_code != NULL); + + task = g_task_new (self, cancellable, callback, user_data); + params = g_hash_table_new (g_str_hash, g_str_equal); + + g_hash_table_insert (params, "client_id", priv->client_id); + g_hash_table_insert (params, "grant_type", "authorization_code"); + g_hash_table_insert (params, "code", (gchar *)authorization_code); + g_hash_table_insert (params, "redirect_uri", priv->redirect_uri); + g_hash_table_insert (params, "code_verifier", (gchar *)code_verifier); + +#if WITH_SOUP_2 + msg = soup_form_request_new_from_hash (SOUP_METHOD_POST, priv->tokenurl, params); +#else + msg = soup_message_new_from_encoded_form (SOUP_METHOD_POST, priv->tokenurl, soup_form_encode_hash (params)); +#endif + + _rest_proxy_queue_message (REST_PROXY (self), +#if WITH_SOUP_2 + g_steal_pointer (&msg), +#else + msg, +#endif + cancellable, rest_oauth2_proxy_fetch_access_token_cb, g_steal_pointer (&task)); + +} + +/** + * rest_oauth2_proxy_fetch_access_token_finish: + * @self: an #RestOauth2Proxy + * @result: a #GAsyncResult provided to callback + * @error: a location for a #GError, or %NULL + * + * Returns: + */ +gboolean +rest_oauth2_proxy_fetch_access_token_finish (RestOAuth2Proxy *self, + GAsyncResult *result, + GError **error) +{ + g_return_val_if_fail (REST_IS_OAUTH2_PROXY (self), FALSE); + g_return_val_if_fail (g_task_is_valid (result, self), FALSE); + + return g_task_propagate_boolean (G_TASK (result), error); +} + +gboolean +rest_oauth2_proxy_refresh_access_token (RestOAuth2Proxy *self, + GError **error) +{ + RestOAuth2ProxyPrivate *priv = rest_oauth2_proxy_get_instance_private (self); + g_autoptr(SoupMessage) msg = NULL; + g_autoptr(GHashTable) params = NULL; + g_autoptr(GTask) task = NULL; + GBytes *payload; + + task = g_task_new (self, NULL, NULL, NULL); + + g_return_val_if_fail (REST_IS_OAUTH2_PROXY (self), FALSE); + + if (priv->refresh_token == NULL) + { + *error = g_error_new (REST_OAUTH2_ERROR, + REST_OAUTH2_ERROR_NO_REFRESH_TOKEN, + "No refresh token available"); + return FALSE; + } + + params = g_hash_table_new (g_str_hash, g_str_equal); + + g_hash_table_insert (params, "client_id", priv->client_id); + g_hash_table_insert (params, "refresh_token", priv->refresh_token); + g_hash_table_insert (params, "redirect_uri", priv->redirect_uri); + g_hash_table_insert (params, "grant_type", "refresh_token"); + +#if WITH_SOUP_2 + msg = soup_form_request_new_from_hash (SOUP_METHOD_POST, priv->tokenurl, params); +#else + msg = soup_message_new_from_encoded_form (SOUP_METHOD_POST, priv->tokenurl, soup_form_encode_hash (params)); +#endif + payload = _rest_proxy_send_message (REST_PROXY (self), msg, NULL, error); + if (error && *error) + { + return FALSE; + } + + REST_OAUTH2_PROXY_GET_CLASS (self)->parse_access_token (self, payload, g_steal_pointer (&task)); + return TRUE; +} + +static void +rest_oauth2_proxy_refresh_access_token_cb (SoupMessage *msg, + GBytes *payload, + GError *error, + gpointer user_data) +{ + g_autoptr(GTask) task = user_data; + RestOAuth2Proxy *self; + + g_assert (G_IS_TASK (task)); + + self = g_task_get_source_object (task); + + if (error) + { + g_task_return_error (task, error); + return; + } + + REST_OAUTH2_PROXY_GET_CLASS (self)->parse_access_token (self, payload, g_steal_pointer (&task)); +} + +void +rest_oauth2_proxy_refresh_access_token_async (RestOAuth2Proxy *self, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) +{ + RestOAuth2ProxyPrivate *priv = rest_oauth2_proxy_get_instance_private (self); + g_autoptr(SoupMessage) msg = NULL; + g_autoptr(GHashTable) params = NULL; + g_autoptr(GTask) task = NULL; + + task = g_task_new (self, cancellable, callback, user_data); + + g_return_if_fail (REST_IS_OAUTH2_PROXY (self)); + + if (priv->refresh_token == NULL) + { + g_task_return_new_error (task, + REST_OAUTH2_ERROR, + REST_OAUTH2_ERROR_NO_REFRESH_TOKEN, + "No refresh token available"); + return; + } + + params = g_hash_table_new (g_str_hash, g_str_equal); + + g_hash_table_insert (params, "client_id", priv->client_id); + g_hash_table_insert (params, "refresh_token", priv->refresh_token); + g_hash_table_insert (params, "redirect_uri", priv->redirect_uri); + g_hash_table_insert (params, "grant_type", "refresh_token"); + +#if WITH_SOUP_2 + msg = soup_form_request_new_from_hash (SOUP_METHOD_POST, priv->tokenurl, params); +#else + msg = soup_message_new_from_encoded_form (SOUP_METHOD_POST, priv->tokenurl, soup_form_encode_hash (params)); +#endif + _rest_proxy_queue_message (REST_PROXY (self), +#if WITH_SOUP_2 + g_steal_pointer (&msg), +#else + msg, +#endif + cancellable, + rest_oauth2_proxy_refresh_access_token_cb, + g_steal_pointer (&task)); +} + +/** + * rest_oauth2_proxy_refresh_access_token_finish: + * @self: an #RestOauth2Proxy + * @result: a #GAsyncResult provided to callback + * @error: a location for a #GError, or %NULL + * + * Returns: + */ +gboolean +rest_oauth2_proxy_refresh_access_token_finish (RestOAuth2Proxy *self, + GAsyncResult *result, + GError **error) +{ + g_return_val_if_fail (REST_IS_OAUTH2_PROXY (self), FALSE); + g_return_val_if_fail (g_task_is_valid (result, self), FALSE); + + return g_task_propagate_boolean (G_TASK (result), error); +} + +const gchar * +rest_oauth2_proxy_get_auth_url (RestOAuth2Proxy *self) +{ + RestOAuth2ProxyPrivate *priv = rest_oauth2_proxy_get_instance_private (self); + + g_return_val_if_fail (REST_IS_OAUTH2_PROXY (self), NULL); + + return priv->authurl; +} + +void +rest_oauth2_proxy_set_auth_url (RestOAuth2Proxy *self, + const gchar *authurl) +{ + RestOAuth2ProxyPrivate *priv = rest_oauth2_proxy_get_instance_private (self); + + g_return_if_fail (REST_IS_OAUTH2_PROXY (self)); + + if (g_strcmp0 (priv->authurl, authurl) != 0) + { + g_clear_pointer (&priv->authurl, g_free); + priv->authurl = g_strdup (authurl); + g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_AUTH_URL]); + } +} + +const gchar * +rest_oauth2_proxy_get_token_url (RestOAuth2Proxy *self) +{ + RestOAuth2ProxyPrivate *priv = rest_oauth2_proxy_get_instance_private (self); + + g_return_val_if_fail (REST_IS_OAUTH2_PROXY (self), NULL); + + return priv->tokenurl; +} + +void +rest_oauth2_proxy_set_token_url (RestOAuth2Proxy *self, + const gchar *tokenurl) +{ + RestOAuth2ProxyPrivate *priv = rest_oauth2_proxy_get_instance_private (self); + + g_return_if_fail (REST_IS_OAUTH2_PROXY (self)); + + if (g_strcmp0 (priv->tokenurl, tokenurl) != 0) + { + g_clear_pointer (&priv->tokenurl, g_free); + priv->tokenurl = g_strdup (tokenurl); + g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TOKEN_URL]); + } +} + +const gchar * +rest_oauth2_proxy_get_redirect_uri (RestOAuth2Proxy *self) +{ + RestOAuth2ProxyPrivate *priv = rest_oauth2_proxy_get_instance_private (self); + + g_return_val_if_fail (REST_IS_OAUTH2_PROXY (self), NULL); + + return priv->redirect_uri; +} + +void +rest_oauth2_proxy_set_redirect_uri (RestOAuth2Proxy *self, + const gchar *redirect_uri) +{ + RestOAuth2ProxyPrivate *priv = rest_oauth2_proxy_get_instance_private (self); + + g_return_if_fail (REST_IS_OAUTH2_PROXY (self)); + + if (g_strcmp0 (priv->redirect_uri, redirect_uri) != 0) + { + g_clear_pointer (&priv->redirect_uri, g_free); + priv->redirect_uri = g_strdup (redirect_uri); + g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_REDIRECT_URI]); + } +} + +const gchar * +rest_oauth2_proxy_get_client_id (RestOAuth2Proxy *self) +{ + RestOAuth2ProxyPrivate *priv = rest_oauth2_proxy_get_instance_private (self); + + g_return_val_if_fail (REST_IS_OAUTH2_PROXY (self), NULL); + + return priv->client_id; +} + +void +rest_oauth2_proxy_set_client_id (RestOAuth2Proxy *self, + const gchar *client_id) +{ + RestOAuth2ProxyPrivate *priv = rest_oauth2_proxy_get_instance_private (self); + + g_return_if_fail (REST_IS_OAUTH2_PROXY (self)); + + if (g_strcmp0 (priv->client_id, client_id) != 0) + { + g_clear_pointer (&priv->client_id, g_free); + priv->client_id = g_strdup (client_id); + g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_CLIENT_ID]); + } +} + +const gchar * +rest_oauth2_proxy_get_client_secret (RestOAuth2Proxy *self) +{ + RestOAuth2ProxyPrivate *priv = rest_oauth2_proxy_get_instance_private (self); + + g_return_val_if_fail (REST_IS_OAUTH2_PROXY (self), NULL); + + return priv->client_secret; +} + +void +rest_oauth2_proxy_set_client_secret (RestOAuth2Proxy *self, + const gchar *client_secret) +{ + RestOAuth2ProxyPrivate *priv = rest_oauth2_proxy_get_instance_private (self); + + g_return_if_fail (REST_IS_OAUTH2_PROXY (self)); + + if (g_strcmp0 (priv->client_secret, client_secret) != 0) + { + g_clear_pointer (&priv->client_secret, g_free); + priv->client_secret = g_strdup (client_secret); + g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_CLIENT_SECRET]); + } +} + +const gchar * +rest_oauth2_proxy_get_access_token (RestOAuth2Proxy *self) +{ + RestOAuth2ProxyPrivate *priv = rest_oauth2_proxy_get_instance_private (self); + + g_return_val_if_fail (REST_IS_OAUTH2_PROXY (self), NULL); + + return priv->access_token; +} + +void +rest_oauth2_proxy_set_access_token (RestOAuth2Proxy *self, + const gchar *access_token) +{ + RestOAuth2ProxyPrivate *priv = rest_oauth2_proxy_get_instance_private (self); + + g_return_if_fail (REST_IS_OAUTH2_PROXY (self)); + + if (g_strcmp0 (priv->access_token, access_token) != 0) + { + g_clear_pointer (&priv->access_token, g_free); + priv->access_token = g_strdup (access_token); + g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ACCESS_TOKEN]); + } +} + +const gchar * +rest_oauth2_proxy_get_refresh_token (RestOAuth2Proxy *self) +{ + RestOAuth2ProxyPrivate *priv = rest_oauth2_proxy_get_instance_private (self); + + g_return_val_if_fail (REST_IS_OAUTH2_PROXY (self), NULL); + + return priv->refresh_token; +} + +void +rest_oauth2_proxy_set_refresh_token (RestOAuth2Proxy *self, + const gchar *refresh_token) +{ + RestOAuth2ProxyPrivate *priv = rest_oauth2_proxy_get_instance_private (self); + + g_return_if_fail (REST_IS_OAUTH2_PROXY (self)); + + if (g_strcmp0 (priv->refresh_token, refresh_token) != 0) + { + g_clear_pointer (&priv->refresh_token, g_free); + priv->refresh_token = g_strdup (refresh_token); + g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_REFRESH_TOKEN]); + } +} + +GDateTime * +rest_oauth2_proxy_get_expiration_date (RestOAuth2Proxy *self) +{ + RestOAuth2ProxyPrivate *priv = rest_oauth2_proxy_get_instance_private (self); + g_return_val_if_fail (REST_IS_OAUTH2_PROXY (self), NULL); + + return priv->expiration_date; +} + +void +rest_oauth2_proxy_set_expiration_date (RestOAuth2Proxy *self, + GDateTime *expiration_date) +{ + RestOAuth2ProxyPrivate *priv = rest_oauth2_proxy_get_instance_private (self); + + g_return_if_fail (REST_IS_OAUTH2_PROXY (self)); + + g_clear_pointer (&priv->expiration_date, g_date_time_unref); + priv->expiration_date = g_date_time_ref (expiration_date); + g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_EXPIRATION_DATE]); +} diff --git a/rest/rest-oauth2-proxy.h b/rest/rest-oauth2-proxy.h new file mode 100644 index 0000000..dd4148e --- /dev/null +++ b/rest/rest-oauth2-proxy.h @@ -0,0 +1,101 @@ +/* rest-oauth2-proxy.h + * + * Copyright 2021 Günther Wagner <info@gunibert.de> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU Lesser General Public License, + * version 2.1, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for + * more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#pragma once + +#include <rest/rest-proxy.h> + +G_BEGIN_DECLS + +#define REST_TYPE_OAUTH2_PROXY (rest_oauth2_proxy_get_type()) + +G_DECLARE_DERIVABLE_TYPE (RestOAuth2Proxy, rest_oauth2_proxy, REST, OAUTH2_PROXY, RestProxy) + +struct _RestOAuth2ProxyClass +{ + RestProxyClass parent_class; + + void (*parse_access_token) (RestOAuth2Proxy *self, + GBytes *payload, + GTask *task); + + gpointer padding[8]; +}; + +enum { + REST_OAUTH2_ERROR_NO_REFRESH_TOKEN, + REST_OAUTH2_ERROR_ACCESS_TOKEN_EXPIRED, +}; + +#define REST_OAUTH2_ERROR rest_oauth2_error_quark () +GQuark rest_oauth2_error_quark (); + +RestOAuth2Proxy *rest_oauth2_proxy_new (const gchar *authurl, + const gchar *tokenurl, + const gchar *redirecturl, + const gchar *client_id, + const gchar *client_secret, + const gchar *baseurl); +gchar *rest_oauth2_proxy_build_authorization_url (RestOAuth2Proxy *self, + const gchar *code_challenge, + const gchar *scope, + gchar **state); +void rest_oauth2_proxy_fetch_access_token_async (RestOAuth2Proxy *self, + const gchar *authorization_code, + const gchar *code_verifier, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data); +gboolean rest_oauth2_proxy_fetch_access_token_finish (RestOAuth2Proxy *self, + GAsyncResult *result, + GError **error); +gboolean rest_oauth2_proxy_refresh_access_token (RestOAuth2Proxy *self, + GError **error); +void rest_oauth2_proxy_refresh_access_token_async (RestOAuth2Proxy *self, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data); +gboolean rest_oauth2_proxy_refresh_access_token_finish (RestOAuth2Proxy *self, + GAsyncResult *result, + GError **error); +const gchar *rest_oauth2_proxy_get_auth_url (RestOAuth2Proxy *self); +void rest_oauth2_proxy_set_auth_url (RestOAuth2Proxy *self, + const gchar *tokenurl); +const gchar *rest_oauth2_proxy_get_token_url (RestOAuth2Proxy *self); +void rest_oauth2_proxy_set_token_url (RestOAuth2Proxy *self, + const gchar *tokenurl); +const gchar *rest_oauth2_proxy_get_redirect_uri (RestOAuth2Proxy *self); +void rest_oauth2_proxy_set_redirect_uri (RestOAuth2Proxy *self, + const gchar *redirect_uri); +const gchar *rest_oauth2_proxy_get_client_id (RestOAuth2Proxy *self); +void rest_oauth2_proxy_set_client_id (RestOAuth2Proxy *self, + const gchar *client_id); +const gchar *rest_oauth2_proxy_get_client_secret (RestOAuth2Proxy *self); +void rest_oauth2_proxy_set_client_secret (RestOAuth2Proxy *self, + const gchar *client_secret); +const gchar *rest_oauth2_proxy_get_access_token (RestOAuth2Proxy *self); +void rest_oauth2_proxy_set_access_token (RestOAuth2Proxy *self, + const gchar *access_token); +const gchar *rest_oauth2_proxy_get_refresh_token (RestOAuth2Proxy *self); +void rest_oauth2_proxy_set_refresh_token (RestOAuth2Proxy *self, + const gchar *refresh_token); +GDateTime *rest_oauth2_proxy_get_expiration_date (RestOAuth2Proxy *self); +void rest_oauth2_proxy_set_expiration_date (RestOAuth2Proxy *self, + GDateTime *expiration_date); + +G_END_DECLS diff --git a/rest/rest-param.c b/rest/rest-param.c index 8ad105f..1f6f73c 100644 --- a/rest/rest-param.c +++ b/rest/rest-param.c @@ -89,7 +89,7 @@ rest_param_new_full (const char *name, param = g_slice_new0 (RestParam); if (use == REST_MEMORY_COPY) { - data = g_memdup (data, length); + data = g_memdup2 (data, length); use = REST_MEMORY_TAKE; } diff --git a/rest/rest-params.c b/rest/rest-params.c index f246cc2..f2ac286 100644 --- a/rest/rest-params.c +++ b/rest/rest-params.c @@ -30,18 +30,7 @@ * @see_also: #RestParam, #RestProxyCall. */ -/* - * RestParams is an alias for GHashTable achieved by opaque types in the public - * headers and casting internally. This has several limitations, mainly - * supporting multiple parameters with the same name and preserving the ordering - * of parameters. - * - * These are not requirements for the bulk of the web services, but this - * limitation does mean librest can't be used for a few web services. - * - * TODO: this should be a list to support multiple parameters with the same - * name. - */ +G_DEFINE_BOXED_TYPE (RestParams, rest_params, rest_params_ref, rest_params_unref) /** * rest_params_new: @@ -53,11 +42,13 @@ RestParams * rest_params_new (void) { - /* The key is a string that is owned by the RestParam, so we don't need to - explicitly free it on removal. */ - return (RestParams *) - g_hash_table_new_full (g_str_hash, g_str_equal, - NULL, (GDestroyNotify)rest_param_unref); + RestParams *self; + + self = g_slice_new0 (RestParams); + self->ref_count = 1; + self->params = NULL; + + return self; } /** @@ -67,13 +58,73 @@ rest_params_new (void) * Destroy the #RestParams and the #RestParam objects that it contains. **/ void -rest_params_free (RestParams *params) +rest_params_free (RestParams *self) { - GHashTable *hash = (GHashTable *)params; + g_assert (self); + g_assert_cmpint (self->ref_count, ==, 0); - g_return_if_fail (params); + g_list_free_full (g_steal_pointer (&self->params), (GDestroyNotify) rest_param_unref); + + g_slice_free (RestParams, self); +} + +/** + * rest_params_copy: + * @self: a #RestParams + * + * Makes a deep copy of a #RestParams. + * + * Returns: (transfer full): A newly created #RestParams with the same + * contents as @self + */ +RestParams * +rest_params_copy (RestParams *self) +{ + RestParams *copy; + + g_return_val_if_fail (self, NULL); + g_return_val_if_fail (self->ref_count, NULL); + + copy = rest_params_new (); + copy->params = g_list_copy_deep (self->params, (GCopyFunc) rest_param_ref, NULL); + + return copy; +} + +/** + * rest_params_ref: + * @self: A #RestParams + * + * Increments the reference count of @self by one. + * + * Returns: (transfer full): @self + */ +RestParams * +rest_params_ref (RestParams *self) +{ + g_return_val_if_fail (self, NULL); + g_return_val_if_fail (self->ref_count, NULL); + + g_atomic_int_inc (&self->ref_count); - g_hash_table_destroy (hash); + return self; +} + +/** + * rest_params_unref: + * @self: A #RestParams + * + * Decrements the reference count of @self by one, freeing the structure when + * the reference count reaches zero. + */ +void +rest_params_unref (RestParams *self) +{ + g_return_if_fail (self); + g_return_if_fail (self->ref_count); + + if (g_atomic_int_dec_and_test (&self->ref_count)) + rest_params_free (self); } /** @@ -84,14 +135,25 @@ rest_params_free (RestParams *params) * Add @param to @params. **/ void -rest_params_add (RestParams *params, RestParam *param) +rest_params_add (RestParams *self, + RestParam *param) { - GHashTable *hash = (GHashTable *)params; - - g_return_if_fail (params); + g_return_if_fail (self); g_return_if_fail (param); - g_hash_table_replace (hash, (gpointer)rest_param_get_name (param), param); + self->params = g_list_append (self->params, param); +} + +static gint +rest_params_find (gconstpointer self, + gconstpointer name) +{ + const RestParam *e = self; + const char *n = name; + const char *n2 = rest_param_get_name ((RestParam *)e); + + if (g_strcmp0 (n2, n) == 0) return 0; + return -1; } /** @@ -105,14 +167,13 @@ rest_params_add (RestParams *params, RestParam *param) * doesn't exist **/ RestParam * -rest_params_get (RestParams *params, const char *name) +rest_params_get (RestParams *self, + const char *name) { - GHashTable *hash = (GHashTable *)params; - - g_return_val_if_fail (params, NULL); + g_return_val_if_fail (self, NULL); g_return_val_if_fail (name, NULL); - return g_hash_table_lookup (hash, name); + return g_list_find_custom (self->params, name, rest_params_find)->data; } /** @@ -123,14 +184,16 @@ rest_params_get (RestParams *params, const char *name) * Remove the #RestParam called @name. **/ void -rest_params_remove (RestParams *params, const char *name) +rest_params_remove (RestParams *self, + const char *name) { - GHashTable *hash = (GHashTable *)params; + GList *elem = NULL; - g_return_if_fail (params); + g_return_if_fail (self); g_return_if_fail (name); - g_hash_table_remove (hash, name); + elem = g_list_find_custom (self->params, name, rest_params_find); + self->params = g_list_remove (self->params, elem->data); } /** @@ -143,27 +206,22 @@ rest_params_remove (RestParams *params, const char *name) * Returns: %TRUE if all of the parameters are simple strings, %FALSE otherwise. **/ gboolean -rest_params_are_strings (RestParams *params) +rest_params_are_strings (RestParams *self) { - GHashTable *hash = (GHashTable *)params; - GHashTableIter iter; - RestParam *param; - - g_return_val_if_fail (params, FALSE); + g_return_val_if_fail (self, FALSE); - g_hash_table_iter_init (&iter, hash); - while (g_hash_table_iter_next (&iter, NULL, (gpointer)¶m)) { - if (!rest_param_is_string (param)) - return FALSE; - } + for (GList *cur = self->params; cur; cur = g_list_next (cur)) + { + if (!rest_param_is_string (cur->data)) + return FALSE; + } return TRUE; - } /** * rest_params_as_string_hash_table: - * @params: a valid #RestParams + * @self: a valid #RestParams * * Create a new #GHashTable which contains the name and value of all string * (content type of text/plain) parameters. @@ -174,23 +232,19 @@ rest_params_are_strings (RestParams *params) * Returns: (element-type utf8 Rest.Param) (transfer container): a new #GHashTable. **/ GHashTable * -rest_params_as_string_hash_table (RestParams *params) +rest_params_as_string_hash_table (RestParams *self) { - GHashTable *hash, *strings; - GHashTableIter iter; - const char *name = NULL; - RestParam *param = NULL; + GHashTable *strings; - g_return_val_if_fail (params, NULL); + g_return_val_if_fail (self, NULL); - hash = (GHashTable *)params; strings = g_hash_table_new (g_str_hash, g_str_equal); - g_hash_table_iter_init (&iter, hash); - while (g_hash_table_iter_next (&iter, (gpointer)&name, (gpointer)¶m)) { - if (rest_param_is_string (param)) - g_hash_table_insert (strings, (gpointer)name, (gpointer)rest_param_get_content (param)); - } + for (GList *cur = self->params; cur; cur = g_list_next (cur)) + { + if (rest_param_is_string (cur->data)) + g_hash_table_insert (strings, (gpointer)rest_param_get_name (cur->data), (gpointer)rest_param_get_content (cur->data)); + } return strings; } @@ -214,12 +268,14 @@ rest_params_as_string_hash_table (RestParams *params) * ]| **/ void -rest_params_iter_init (RestParamsIter *iter, RestParams *params) +rest_params_iter_init (RestParamsIter *iter, + RestParams *params) { g_return_if_fail (iter); g_return_if_fail (params); - g_hash_table_iter_init ((GHashTableIter *)iter, (GHashTable *)params); + iter->params = params; + iter->position = -1; } /** @@ -235,9 +291,21 @@ rest_params_iter_init (RestParamsIter *iter, RestParams *params) * Returns: %FALSE if the end of the #RestParams has been reached, %TRUE otherwise. **/ gboolean -rest_params_iter_next (RestParamsIter *iter, const char **name, RestParam **param) +rest_params_iter_next (RestParamsIter *iter, + const char **name, + RestParam **param) { + GList *cur = NULL; + g_return_val_if_fail (iter, FALSE); - return g_hash_table_iter_next ((GHashTableIter *)iter, (gpointer)name, (gpointer)param); + iter->position++; + cur = g_list_nth (iter->params->params, iter->position); + + if (cur == NULL) return FALSE; + + *param = cur->data; + *name = rest_param_get_name (*param); + return TRUE; } + diff --git a/rest/rest-params.h b/rest/rest-params.h index caace9d..9f53f77 100644 --- a/rest/rest-params.h +++ b/rest/rest-params.h @@ -20,34 +20,52 @@ * */ -#ifndef _REST_PARAMS -#define _REST_PARAMS +#pragma once #include <glib-object.h> #include "rest-param.h" G_BEGIN_DECLS -typedef struct _RestParams RestParams; -typedef struct _GHashTableIter RestParamsIter; - -RestParams * rest_params_new (void); - -void rest_params_free (RestParams *params); +#define REST_TYPE_PARAMS (rest_params_get_type ()) -void rest_params_add (RestParams *params, RestParam *param); +typedef struct _RestParams RestParams; +typedef struct _RestParamsIter RestParamsIter; -RestParam *rest_params_get (RestParams *params, const char *name); +struct _RestParams +{ + /*< private >*/ + guint ref_count; -void rest_params_remove (RestParams *params, const char *name); + GList *params; +}; -gboolean rest_params_are_strings (RestParams *params); +struct _RestParamsIter +{ + /*< private >*/ + RestParams *params; + gint position; +}; -GHashTable * rest_params_as_string_hash_table (RestParams *params); +GType rest_params_get_type (void) G_GNUC_CONST; +RestParams *rest_params_new (void); +RestParams *rest_params_copy (RestParams *self); +RestParams *rest_params_ref (RestParams *self); +void rest_params_unref (RestParams *self); +void rest_params_add (RestParams *params, + RestParam *param); +RestParam *rest_params_get (RestParams *params, + const char *name); +void rest_params_remove (RestParams *params, + const char *name); +gboolean rest_params_are_strings (RestParams *params); +GHashTable *rest_params_as_string_hash_table (RestParams *params); +void rest_params_iter_init (RestParamsIter *iter, + RestParams *params); +gboolean rest_params_iter_next (RestParamsIter *iter, + const char **name, + RestParam **param); -void rest_params_iter_init (RestParamsIter *iter, RestParams *params); -gboolean rest_params_iter_next (RestParamsIter *iter, const char **name, RestParam **param); +G_DEFINE_AUTOPTR_CLEANUP_FUNC (RestParams, rest_params_unref) G_END_DECLS - -#endif /* _REST_PARAMS */ diff --git a/rest/rest-pkce-code-challenge.c b/rest/rest-pkce-code-challenge.c new file mode 100644 index 0000000..0d936f8 --- /dev/null +++ b/rest/rest-pkce-code-challenge.c @@ -0,0 +1,121 @@ +/* rest-pkce-code-challenge.c + * + * Copyright 2021 Günther Wagner <info@gunibert.de> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU Lesser General Public License, + * version 2.1, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for + * more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "rest-pkce-code-challenge.h" +#include "rest-utils.h" + +G_DEFINE_BOXED_TYPE (RestPkceCodeChallenge, rest_pkce_code_challenge, rest_pkce_code_challenge_copy, rest_pkce_code_challenge_free) + +struct _RestPkceCodeChallenge +{ + gchar *code_verifier; + gchar *code_challenge; +}; + +/** + * rest_pkce_code_challenge_new_random: + * + * Creates a new #RestPkceCodeChallenge. + * + * Returns: (transfer full): A newly created #RestPkceCodeChallenge + */ +RestPkceCodeChallenge * +rest_pkce_code_challenge_new_random (void) +{ + RestPkceCodeChallenge *self; + gint length = g_random_int_range (43, 128); + gsize digest_len = 200; + guchar code_verifier_sha256[200]; + GChecksum *sha256 = g_checksum_new (G_CHECKSUM_SHA256); + + self = g_slice_new0 (RestPkceCodeChallenge); + self->code_verifier = random_string (length); + g_checksum_update (sha256, (guchar *)self->code_verifier, -1); + g_checksum_get_digest (sha256, (guchar *)&code_verifier_sha256, &digest_len); + + self->code_challenge = g_base64_encode (code_verifier_sha256, digest_len); + g_strdelimit (self->code_challenge, "=", '\0'); + g_strdelimit (self->code_challenge, "+", '-'); + g_strdelimit (self->code_challenge, "/", '_'); + + return self; +} + +/** + * rest_pkce_code_challenge_copy: + * @self: a #RestPkceCodeChallenge + * + * Makes a deep copy of a #RestPkceCodeChallenge. + * + * Returns: (transfer full): A newly created #RestPkceCodeChallenge with the same + * contents as @self + */ +RestPkceCodeChallenge * +rest_pkce_code_challenge_copy (RestPkceCodeChallenge *self) +{ + RestPkceCodeChallenge *copy; + + g_return_val_if_fail (self, NULL); + + copy = g_slice_new0 (RestPkceCodeChallenge); + copy->code_verifier = self->code_verifier; + copy->code_challenge = self->code_challenge; + + return copy; +} + +/** + * rest_pkce_code_challenge_free: + * @self: a #RestPkceCodeChallenge + * + * Frees a #RestPkceCodeChallenge allocated using rest_pkce_code_challenge_new() + * or rest_pkce_code_challenge_copy(). + */ +void +rest_pkce_code_challenge_free (RestPkceCodeChallenge *self) +{ + g_return_if_fail (self); + + g_slice_free (RestPkceCodeChallenge, self); +} + +/** + * rest_pkce_code_challenge_get_challenge: + * + * Returns the Code Challenge for the Pkce verification. + * + * Returns: the code_challenge + */ +const gchar * +rest_pkce_code_challenge_get_challenge (RestPkceCodeChallenge *self) +{ + return self->code_challenge; +} + +/** + * rest_pkce_code_challenge_get_verifier: + * + * Returns the Code Verifier for the Pkce verification. + * + * Returns: the code_verifier + */ +const gchar * +rest_pkce_code_challenge_get_verifier (RestPkceCodeChallenge *self) +{ + return self->code_verifier; +} diff --git a/rest/rest-pkce-code-challenge.h b/rest/rest-pkce-code-challenge.h new file mode 100644 index 0000000..9ce3907 --- /dev/null +++ b/rest/rest-pkce-code-challenge.h @@ -0,0 +1,44 @@ +/* rest-pkce-code-challenge.h + * + * Copyright 2021 Günther Wagner <info@gunibert.de> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU Lesser General Public License, + * version 2.1, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for + * more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#pragma once + +#include <glib-object.h> + +G_BEGIN_DECLS + +#define REST_TYPE_PKCE_CODE_CHALLENGE (rest_pkce_code_challenge_get_type ()) + +/** + * RestPkceCodeChallenge: + * + * In order to play a Pkce Code Verification during a OAuth2 authorization + * you need this structure which handles the algorithmic part. + */ +typedef struct _RestPkceCodeChallenge RestPkceCodeChallenge; + +GType rest_pkce_code_challenge_get_type (void) G_GNUC_CONST; +RestPkceCodeChallenge *rest_pkce_code_challenge_new_random (void); +RestPkceCodeChallenge *rest_pkce_code_challenge_copy (RestPkceCodeChallenge *self); +void rest_pkce_code_challenge_free (RestPkceCodeChallenge *self); +const gchar *rest_pkce_code_challenge_get_challenge (RestPkceCodeChallenge *self); +const gchar *rest_pkce_code_challenge_get_verifier (RestPkceCodeChallenge *self); + +G_DEFINE_AUTOPTR_CLEANUP_FUNC (RestPkceCodeChallenge, rest_pkce_code_challenge_free) + +G_END_DECLS diff --git a/rest/rest-proxy-call.c b/rest/rest-proxy-call.c index 62b00da..791a7dd 100644 --- a/rest/rest-proxy-call.c +++ b/rest/rest-proxy-call.c @@ -148,7 +148,7 @@ rest_proxy_call_dispose (GObject *object) g_clear_object (&priv->cancellable); } - g_clear_pointer (&priv->params, rest_params_free); + g_clear_pointer (&priv->params, rest_params_unref); g_clear_pointer (&priv->headers, g_hash_table_unref); g_clear_pointer (&priv->response_headers, g_hash_table_unref); g_clear_object (&priv->proxy); @@ -238,6 +238,8 @@ rest_proxy_call_set_method (RestProxyCall *call, * @call: The #RestProxyCall * * Get the HTTP method to use when making the call, for example GET or POST. + * + * Returns: (transfer none): the HTTP method */ const char * rest_proxy_call_get_method (RestProxyCall *call) @@ -1005,19 +1007,23 @@ _call_message_call_completed_cb (SoupMessage *message, GError *error, gpointer user_data) { - GTask *task = user_data; + g_autoptr(GTask) task = user_data; RestProxyCall *call; call = REST_PROXY_CALL (g_task_get_source_object (task)); + if (error) + { + g_task_return_error (task, error); + return; + } + finish_call (call, message, payload, &error); if (error != NULL) g_task_return_error (task, error); else g_task_return_boolean (task, TRUE); - - g_object_unref (task); } /** @@ -1158,6 +1164,8 @@ _continuous_call_message_sent_cb (GObject *source, * * You may unref the call after calling this function since there is an * internal reference, or you may unref in the callback. + * + * Returns: %TRUE on success */ gboolean rest_proxy_call_continuous (RestProxyCall *call, @@ -1491,11 +1499,7 @@ rest_proxy_call_get_payload_length (RestProxyCall *call) g_return_val_if_fail (REST_IS_PROXY_CALL (call), 0); payload = GET_PRIVATE (call)->payload; -#ifdef WITH_SOUP_2 - return payload ? g_bytes_get_size (payload) - 1 : 0; -#else return payload ? g_bytes_get_size (payload) : 0; -#endif } /** diff --git a/rest/rest-proxy.c b/rest/rest-proxy.c index 171f6cb..d8dbfc6 100644 --- a/rest/rest-proxy.c +++ b/rest/rest-proxy.c @@ -31,8 +31,6 @@ #include "rest-private.h" -#define GET_PRIVATE(o) rest_proxy_get_instance_private(REST_PROXY(o)) - typedef struct _RestProxyPrivate RestProxyPrivate; struct _RestProxyPrivate { @@ -74,16 +72,14 @@ enum { static guint signals[LAST_SIGNAL] = { 0 }; -static gboolean _rest_proxy_simple_run_valist (RestProxy *proxy, - char **payload, - goffset *len, - GError **error, - va_list params); - -static RestProxyCall *_rest_proxy_new_call (RestProxy *proxy); - -static gboolean _rest_proxy_bind_valist (RestProxy *proxy, - va_list params); +static gboolean _rest_proxy_simple_run_valist (RestProxy *proxy, + char **payload, + goffset *len, + GError **error, + va_list params); +static RestProxyCall *_rest_proxy_new_call (RestProxy *proxy); +static gboolean _rest_proxy_bind_valist (RestProxy *proxy, + va_list params); GQuark rest_proxy_error_quark (void) @@ -97,7 +93,8 @@ rest_proxy_get_property (GObject *object, GValue *value, GParamSpec *pspec) { - RestProxyPrivate *priv = GET_PRIVATE (object); + RestProxy *self = REST_PROXY (object); + RestProxyPrivate *priv = rest_proxy_get_instance_private (self); switch (property_id) { case PROP_URL_FORMAT: @@ -145,7 +142,8 @@ rest_proxy_set_property (GObject *object, const GValue *value, GParamSpec *pspec) { - RestProxyPrivate *priv = GET_PRIVATE (object); + RestProxy *self = REST_PROXY (object); + RestProxyPrivate *priv = rest_proxy_get_instance_private (self); switch (property_id) { case PROP_URL_FORMAT: @@ -199,7 +197,8 @@ rest_proxy_set_property (GObject *object, static void rest_proxy_dispose (GObject *object) { - RestProxyPrivate *priv = GET_PRIVATE (object); + RestProxy *self = REST_PROXY (object); + RestProxyPrivate *priv = rest_proxy_get_instance_private (self); g_clear_object (&priv->session); @@ -226,7 +225,7 @@ authenticate (RestProxy *self, gboolean retrying, SoupSession *session) { - RestProxyPrivate *priv = GET_PRIVATE (self); + RestProxyPrivate *priv = rest_proxy_get_instance_private (self); RestProxyAuth *rest_auth; gboolean try_auth; @@ -241,7 +240,8 @@ authenticate (RestProxy *self, static void rest_proxy_constructed (GObject *object) { - RestProxyPrivate *priv = GET_PRIVATE (object); + RestProxy *self = REST_PROXY (object); + RestProxyPrivate *priv = rest_proxy_get_instance_private (self); if (!priv->disable_cookies) { SoupSessionFeature *cookie_jar = @@ -270,7 +270,8 @@ rest_proxy_constructed (GObject *object) static void rest_proxy_finalize (GObject *object) { - RestProxyPrivate *priv = GET_PRIVATE (object); + RestProxy *self = REST_PROXY (object); + RestProxyPrivate *priv = rest_proxy_get_instance_private (self); g_free (priv->url); g_free (priv->url_format); @@ -440,14 +441,17 @@ transform_tls_database_to_ssl_ca_file (GBinding *binding, static void rest_proxy_init (RestProxy *self) { - RestProxyPrivate *priv = GET_PRIVATE (self); + RestProxyPrivate *priv = rest_proxy_get_instance_private (self); +#ifdef REST_SYSTEM_CA_FILE GTlsDatabase *tls_database; +#endif #ifndef WITH_SOUP_2 priv->ssl_strict = TRUE; #endif priv->session = soup_session_new (); + soup_session_remove_feature_by_type (priv->session, SOUP_TYPE_AUTH_MANAGER); #ifdef REST_SYSTEM_CA_FILE /* with ssl-strict (defaults TRUE) setting ssl-ca-file forces all @@ -532,7 +536,7 @@ static gboolean _rest_proxy_bind_valist (RestProxy *proxy, va_list params) { - RestProxyPrivate *priv = GET_PRIVATE (proxy); + RestProxyPrivate *priv = rest_proxy_get_instance_private (proxy); g_return_val_if_fail (proxy != NULL, FALSE); g_return_val_if_fail (priv->url_format != NULL, FALSE); @@ -583,7 +587,7 @@ rest_proxy_set_user_agent (RestProxy *proxy, const gchar * rest_proxy_get_user_agent (RestProxy *proxy) { - RestProxyPrivate *priv = GET_PRIVATE (proxy); + RestProxyPrivate *priv = rest_proxy_get_instance_private (proxy); g_return_val_if_fail (REST_IS_PROXY (proxy), NULL); @@ -613,9 +617,10 @@ rest_proxy_get_user_agent (RestProxy *proxy) * Since: 0.7.92 */ void -rest_proxy_add_soup_feature (RestProxy *proxy, SoupSessionFeature *feature) +rest_proxy_add_soup_feature (RestProxy *proxy, + SoupSessionFeature *feature) { - RestProxyPrivate *priv = GET_PRIVATE (proxy); + RestProxyPrivate *priv = rest_proxy_get_instance_private (proxy); g_return_if_fail (REST_IS_PROXY(proxy)); g_return_if_fail (feature != NULL); @@ -659,7 +664,7 @@ rest_proxy_new_call (RestProxy *proxy) gboolean _rest_proxy_get_binding_required (RestProxy *proxy) { - RestProxyPrivate *priv = GET_PRIVATE (proxy); + RestProxyPrivate *priv = rest_proxy_get_instance_private (proxy); g_return_val_if_fail (REST_IS_PROXY (proxy), FALSE); @@ -669,14 +674,14 @@ _rest_proxy_get_binding_required (RestProxy *proxy) const gchar * _rest_proxy_get_bound_url (RestProxy *proxy) { - RestProxyPrivate *priv = GET_PRIVATE (proxy); + RestProxyPrivate *priv = rest_proxy_get_instance_private (proxy); g_return_val_if_fail (REST_IS_PROXY (proxy), NULL); if (!priv->url && !priv->binding_required) - { - priv->url = g_strdup (priv->url_format); - } + { + priv->url = g_strdup (priv->url_format); + } return priv->url; } @@ -763,7 +768,7 @@ message_finished_cb (SoupSession *session, GError *error = NULL; body = g_bytes_new (message->response_body->data, - message->response_body->length + 1); + message->response_body->length); data->callback (message, body, error, data->user_data); g_free (data); } @@ -791,7 +796,7 @@ _rest_proxy_queue_message (RestProxy *proxy, RestMessageFinishedCallback callback, gpointer user_data) { - RestProxyPrivate *priv = GET_PRIVATE (proxy); + RestProxyPrivate *priv = rest_proxy_get_instance_private (proxy); RestMessageQueueData *data; g_return_if_fail (REST_IS_PROXY (proxy)); @@ -841,7 +846,7 @@ _rest_proxy_send_message_async (RestProxy *proxy, GAsyncReadyCallback callback, gpointer user_data) { - RestProxyPrivate *priv = GET_PRIVATE (proxy); + RestProxyPrivate *priv = rest_proxy_get_instance_private (proxy); GTask *task; task = g_task_new (proxy, cancellable, callback, user_data); @@ -868,7 +873,7 @@ _rest_proxy_cancel_message (RestProxy *proxy, SoupMessage *message) { #ifdef WITH_SOUP_2 - RestProxyPrivate *priv = GET_PRIVATE (proxy); + RestProxyPrivate *priv = rest_proxy_get_instance_private (proxy); g_return_if_fail (REST_IS_PROXY (proxy)); g_return_if_fail (SOUP_IS_MESSAGE (message)); @@ -885,7 +890,7 @@ _rest_proxy_send_message (RestProxy *proxy, GCancellable *cancellable, GError **error) { - RestProxyPrivate *priv = GET_PRIVATE (proxy); + RestProxyPrivate *priv = rest_proxy_get_instance_private (proxy); GBytes *body; g_return_val_if_fail (REST_IS_PROXY (proxy), NULL); @@ -894,7 +899,7 @@ _rest_proxy_send_message (RestProxy *proxy, #ifdef WITH_SOUP_2 soup_session_send_message (priv->session, message); body = g_bytes_new (message->response_body->data, - message->response_body->length + 1); + message->response_body->length); #else body = soup_session_send_and_read (priv->session, message, diff --git a/rest/rest-proxy.h b/rest/rest-proxy.h index bb6d7b0..4b2a813 100644 --- a/rest/rest-proxy.h +++ b/rest/rest-proxy.h @@ -160,41 +160,32 @@ typedef enum { GQuark rest_proxy_error_quark (void); -RestProxy *rest_proxy_new (const gchar *url_format, - gboolean binding_required); - -RestProxy * -rest_proxy_new_with_authentication (const gchar *url_format, - gboolean binding_required, - const gchar *username, - const gchar *password); - -gboolean rest_proxy_bind (RestProxy *proxy, - ...); - -gboolean rest_proxy_bind_valist (RestProxy *proxy, - va_list params); - -void rest_proxy_set_user_agent (RestProxy *proxy, const char *user_agent); - -const gchar *rest_proxy_get_user_agent (RestProxy *proxy); - -void rest_proxy_add_soup_feature (RestProxy *proxy, - SoupSessionFeature *feature); - -RestProxyCall *rest_proxy_new_call (RestProxy *proxy); - -G_GNUC_NULL_TERMINATED -gboolean rest_proxy_simple_run (RestProxy *proxy, - gchar **payload, - goffset *len, - GError **error, - ...); -gboolean rest_proxy_simple_run_valist (RestProxy *proxy, - gchar **payload, - goffset *len, - GError **error, - va_list params); +RestProxy *rest_proxy_new (const gchar *url_format, + gboolean binding_required); +RestProxy *rest_proxy_new_with_authentication (const gchar *url_format, + gboolean binding_required, + const gchar *username, + const gchar *password); +gboolean rest_proxy_bind (RestProxy *proxy, + ...); +gboolean rest_proxy_bind_valist (RestProxy *proxy, + va_list params); +void rest_proxy_set_user_agent (RestProxy *proxy, + const char *user_agent); +const gchar *rest_proxy_get_user_agent (RestProxy *proxy); +void rest_proxy_add_soup_feature (RestProxy *proxy, + SoupSessionFeature *feature); +RestProxyCall *rest_proxy_new_call (RestProxy *proxy); +gboolean rest_proxy_simple_run (RestProxy *proxy, + gchar **payload, + goffset *len, + GError **error, + ...) G_GNUC_NULL_TERMINATED; +gboolean rest_proxy_simple_run_valist (RestProxy *proxy, + gchar **payload, + goffset *len, + GError **error, + va_list params); G_END_DECLS #endif /* _REST_PROXY */ diff --git a/rest/rest-utils.c b/rest/rest-utils.c new file mode 100644 index 0000000..939d49e --- /dev/null +++ b/rest/rest-utils.c @@ -0,0 +1,44 @@ +/* rest-utils.c + * + * Copyright 2021 Günther Wagner <info@gunibert.de> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU Lesser General Public License, + * version 2.1, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for + * more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "rest-utils.h" + +/** + * random_string: + * @length: the length of the random string + * + * Creates a random string from a given alphabeth with length @length + * + * Returns: (transfer full): a random string + */ +gchar * +random_string (guint length) +{ + g_autoptr(GRand) rand = g_rand_new (); + gchar *buffer = g_slice_alloc0 (sizeof (gchar) * length + 1); + gchar alphabeth[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~"; + + for (guint i = 0; i < length; i++) + { + buffer[i] = alphabeth[g_rand_int (rand) % (sizeof (alphabeth) - 1)]; + } + buffer[length] = '\0'; + + return buffer; +} + diff --git a/rest/rest-utils.h b/rest/rest-utils.h new file mode 100644 index 0000000..cd61145 --- /dev/null +++ b/rest/rest-utils.h @@ -0,0 +1,27 @@ +/* rest-utils.h + * + * Copyright 2021 Günther Wagner <info@gunibert.de> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU Lesser General Public License, + * version 2.1, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for + * more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#pragma once + +#include <glib.h> + +G_BEGIN_DECLS + +gchar *random_string (guint length); + +G_END_DECLS diff --git a/rest/rest-xml-node.h b/rest/rest-xml-node.h index 335bd64..05a4056 100644 --- a/rest/rest-xml-node.h +++ b/rest/rest-xml-node.h @@ -21,8 +21,7 @@ * */ -#ifndef _REST_XML_NODE -#define _REST_XML_NODE +#pragma once #include <glib-object.h> @@ -39,6 +38,8 @@ G_BEGIN_DECLS * @attrs: a #GHashTable of string name to string values for the attributes of * the element. * @next: the sibling #RestXmlNode with the same name + * + * The #RestXmlNode contains a parsed XmlNode for easy consumption */ typedef struct _RestXmlNode RestXmlNode; struct _RestXmlNode { @@ -54,20 +55,21 @@ struct _RestXmlNode { GType rest_xml_node_get_type (void); -RestXmlNode *rest_xml_node_ref (RestXmlNode *node); -void rest_xml_node_unref (RestXmlNode *node); -const gchar *rest_xml_node_get_attr (RestXmlNode *node, - const gchar *attr_name); -RestXmlNode *rest_xml_node_find (RestXmlNode *start, - const gchar *tag); +RestXmlNode *rest_xml_node_ref (RestXmlNode *node); +void rest_xml_node_unref (RestXmlNode *node); +const gchar *rest_xml_node_get_attr (RestXmlNode *node, + const gchar *attr_name); +RestXmlNode *rest_xml_node_find (RestXmlNode *start, + const gchar *tag); +RestXmlNode *rest_xml_node_add_child (RestXmlNode *parent, + const char *tag); +char *rest_xml_node_print (RestXmlNode *node); +void rest_xml_node_add_attr (RestXmlNode *node, + const char *attribute, + const char *value); +void rest_xml_node_set_content (RestXmlNode *node, + const char *value); -RestXmlNode *rest_xml_node_add_child (RestXmlNode *parent, const char *tag); -char *rest_xml_node_print (RestXmlNode *node); -void rest_xml_node_add_attr (RestXmlNode *node, - const char *attribute, - const char *value); -void rest_xml_node_set_content (RestXmlNode *node, const char *value); +G_DEFINE_AUTOPTR_CLEANUP_FUNC (RestXmlNode, rest_xml_node_unref) G_END_DECLS - -#endif /* _REST_XML_NODE */ diff --git a/rest/rest-xml-parser.h b/rest/rest-xml-parser.h index cceda02..cee2570 100644 --- a/rest/rest-xml-parser.h +++ b/rest/rest-xml-parser.h @@ -20,8 +20,7 @@ * */ -#ifndef _REST_XML_PARSER -#define _REST_XML_PARSER +#pragma once #include <glib-object.h> #include <rest/rest-xml-node.h> @@ -31,15 +30,19 @@ G_BEGIN_DECLS #define REST_TYPE_XML_PARSER rest_xml_parser_get_type() G_DECLARE_DERIVABLE_TYPE (RestXmlParser, rest_xml_parser, REST, XML_PARSER, GObject) +/** + * RestXmlParser: + * + * A Xml Parser for Rest Responses + */ + struct _RestXmlParserClass { GObjectClass parent_class; }; -RestXmlParser *rest_xml_parser_new (void); -RestXmlNode *rest_xml_parser_parse_from_data (RestXmlParser *parser, - const gchar *data, - goffset len); +RestXmlParser *rest_xml_parser_new (void); +RestXmlNode *rest_xml_parser_parse_from_data (RestXmlParser *parser, + const gchar *data, + goffset len); G_END_DECLS - -#endif /* _REST_XML_PARSER */ diff --git a/rest/rest.h b/rest/rest.h new file mode 100644 index 0000000..9bc964c --- /dev/null +++ b/rest/rest.h @@ -0,0 +1,35 @@ +/* rest.h + * + * Copyright 2021 Günther Wagner <info@gunibert.de> + * + * This file is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 3 of the + * License, or (at your option) any later version. + * + * This file 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * SPDX-License-Identifier: LGPL-3.0-or-later + */ + +#pragma once + +#include <glib.h> + +G_BEGIN_DECLS + +#define REST_INSIDE +# include "rest-proxy.h" +# include "rest-proxy-call.h" +# include "rest-oauth2-proxy.h" +# include "rest-utils.h" +# include "rest-pkce-code-challenge.h" +#undef REST_INSIDE + +G_END_DECLS |