1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
|
/*
* 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.
*
*/
/*
* TODO:
*
* Convenience API for authentication so that the user doesn't have to parse the
* XML themselves.
*
* Function to parse a payload and return either a xml document or a GError.
*/
#include <config.h>
#include <stdlib.h>
#include <string.h>
#include <rest/rest-proxy.h>
#include <libsoup/soup.h>
#include "flickr-proxy.h"
#include "flickr-proxy-private.h"
#include "flickr-proxy-call.h"
G_DEFINE_TYPE (FlickrProxy, flickr_proxy, REST_TYPE_PROXY)
enum {
PROP_0,
PROP_API_KEY,
PROP_SHARED_SECRET,
PROP_TOKEN,
};
GQuark
flickr_proxy_error_quark (void)
{
return g_quark_from_static_string ("rest-flickr-proxy");
}
static RestProxyCall *
_new_call (RestProxy *proxy)
{
RestProxyCall *call;
call = g_object_new (FLICKR_TYPE_PROXY_CALL,
"proxy", proxy,
NULL);
return call;
}
static void
flickr_proxy_get_property (GObject *object, guint property_id,
GValue *value, GParamSpec *pspec)
{
FlickrProxyPrivate *priv = PROXY_GET_PRIVATE (object);
switch (property_id) {
case PROP_API_KEY:
g_value_set_string (value, priv->api_key);
break;
case PROP_SHARED_SECRET:
g_value_set_string (value, priv->shared_secret);
break;
case PROP_TOKEN:
g_value_set_string (value, priv->token);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
flickr_proxy_set_property (GObject *object, guint property_id,
const GValue *value, GParamSpec *pspec)
{
FlickrProxyPrivate *priv = PROXY_GET_PRIVATE (object);
switch (property_id) {
case PROP_API_KEY:
if (priv->api_key)
g_free (priv->api_key);
priv->api_key = g_value_dup_string (value);
break;
case PROP_SHARED_SECRET:
if (priv->shared_secret)
g_free (priv->shared_secret);
priv->shared_secret = g_value_dup_string (value);
break;
case PROP_TOKEN:
if (priv->token)
g_free (priv->token);
priv->token = g_value_dup_string (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
flickr_proxy_finalize (GObject *object)
{
FlickrProxyPrivate *priv = PROXY_GET_PRIVATE (object);
g_free (priv->api_key);
g_free (priv->shared_secret);
g_free (priv->token);
G_OBJECT_CLASS (flickr_proxy_parent_class)->finalize (object);
}
#ifndef G_PARAM_STATIC_STRINGS
#define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
#endif
static void
flickr_proxy_class_init (FlickrProxyClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
RestProxyClass *proxy_class = REST_PROXY_CLASS (klass);
GParamSpec *pspec;
g_type_class_add_private (klass, sizeof (FlickrProxyPrivate));
object_class->get_property = flickr_proxy_get_property;
object_class->set_property = flickr_proxy_set_property;
object_class->finalize = flickr_proxy_finalize;
proxy_class->new_call = _new_call;
pspec = g_param_spec_string ("api-key", "api-key",
"The API key", NULL,
G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY|G_PARAM_STATIC_STRINGS);
g_object_class_install_property (object_class,
PROP_API_KEY,
pspec);
pspec = g_param_spec_string ("shared-secret", "shared-secret",
"The shared secret", NULL,
G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY|G_PARAM_STATIC_STRINGS);
g_object_class_install_property (object_class,
PROP_SHARED_SECRET,
pspec);
pspec = g_param_spec_string ("token", "token",
"The request or access token", NULL,
G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS);
g_object_class_install_property (object_class,
PROP_TOKEN,
pspec);
}
static void
flickr_proxy_init (FlickrProxy *self)
{
self->priv = PROXY_GET_PRIVATE (self);
}
RestProxy *
flickr_proxy_new (const char *api_key,
const char *shared_secret)
{
return flickr_proxy_new_with_token (api_key,
shared_secret,
NULL);
}
RestProxy *
flickr_proxy_new_with_token (const char *api_key,
const char *shared_secret,
const char *token)
{
return g_object_new (FLICKR_TYPE_PROXY,
"api-key", api_key,
"shared-secret", shared_secret,
"token", token,
"url-format", "http://api.flickr.com/services/rest/",
"binding-required", FALSE,
NULL);
}
/**
* flickr_proxy_get_api_key:
* @proxy: an #FlickrProxy
*
* Get the API key.
*
* Returns: the API key. This string is owned by #FlickrProxy and should not be
* freed.
*/
const char *
flickr_proxy_get_api_key (FlickrProxy *proxy)
{
FlickrProxyPrivate *priv = PROXY_GET_PRIVATE (proxy);
return priv->api_key;
}
/**
* flickr_proxy_get_shared_secret:
* @proxy: an #FlickrProxy
*
* Get the shared secret for authentication.
*
* Returns: the shared secret. This string is owned by #FlickrProxy and should not be
* freed.
*/
const char *
flickr_proxy_get_shared_secret (FlickrProxy *proxy)
{
FlickrProxyPrivate *priv = PROXY_GET_PRIVATE (proxy);
return priv->shared_secret;
}
/**
* flickr_proxy_get_token:
* @proxy: an #FlickrProxy
*
* Get the current token.
*
* Returns: the token, or %NULL if there is no token yet. This string is owned
* by #FlickrProxy and should not be freed.
*/
const char *
flickr_proxy_get_token (FlickrProxy *proxy)
{
FlickrProxyPrivate *priv = PROXY_GET_PRIVATE (proxy);
return priv->token;
}
/**
* flickr_proxy_set_token:
* @proxy: an #FlickrProxy
* @token: the access token
*
* Set the token.
*/
void
flickr_proxy_set_token (FlickrProxy *proxy, const char *token)
{
FlickrProxyPrivate *priv;
g_return_if_fail (FLICKR_IS_PROXY (proxy));
priv = PROXY_GET_PRIVATE (proxy);
if (priv->token)
g_free (priv->token);
priv->token = g_strdup (token);
}
char *
flickr_proxy_sign (FlickrProxy *proxy, GHashTable *params)
{
FlickrProxyPrivate *priv;
GString *s;
GList *keys;
char *md5;
g_return_val_if_fail (FLICKR_IS_PROXY (proxy), NULL);
g_return_val_if_fail (params, NULL);
priv = PROXY_GET_PRIVATE (proxy);
s = g_string_new (priv->shared_secret);
keys = g_hash_table_get_keys (params);
keys = g_list_sort (keys, (GCompareFunc)strcmp);
while (keys) {
const char *key;
const char *value;
key = keys->data;
value = g_hash_table_lookup (params, key);
g_string_append_printf (s, "%s%s", key, value);
keys = g_list_delete_link (keys, keys);
}
md5 = g_compute_checksum_for_string (G_CHECKSUM_MD5, s->str, s->len);
g_string_free (s, TRUE);
return md5;
}
char *
flickr_proxy_build_login_url (FlickrProxy *proxy, const char *frob)
{
SoupURI *uri;
GHashTable *params;
char *sig, *s;
g_return_val_if_fail (FLICKR_IS_PROXY (proxy), NULL);
uri = soup_uri_new ("http://flickr.com/services/auth/");
params = g_hash_table_new (g_str_hash, g_str_equal);
g_hash_table_insert (params, "api_key", proxy->priv->api_key);
/* TODO: parameter */
g_hash_table_insert (params, "perms", "read");
if (frob)
g_hash_table_insert (params, "frob", (gpointer)frob);
sig = flickr_proxy_sign (proxy, params);
g_hash_table_insert (params, "api_sig", sig);
soup_uri_set_query_from_form (uri, params);
s = soup_uri_to_string (uri, FALSE);
g_free (sig);
g_hash_table_destroy (params);
soup_uri_free (uri);
return s;
}
/**
* flickr_proxy_is_successful:
* @root: The root node of a parsed Flickr response
* @error: #GError to set if the response was an error
*
* Examines the Flickr response and if it not a successful reply, set @error and
* return FALSE.
*
* Returns: %TRUE if this response is successful, %FALSE otherwise.
*/
gboolean
flickr_proxy_is_successful (RestXmlNode *root, GError **error)
{
RestXmlNode *node;
g_return_val_if_fail (root, FALSE);
if (strcmp (root->name, "rsp") != 0) {
g_set_error (error, FLICKR_PROXY_ERROR, 0,
"Unexpected response from Flickr (root node %s)",
root->name);
return FALSE;
}
if (strcmp (rest_xml_node_get_attr (root, "stat"), "ok") != 0) {
node = rest_xml_node_find (root, "err");
g_set_error_literal (error,FLICKR_PROXY_ERROR,
atoi (rest_xml_node_get_attr (node, "code")),
rest_xml_node_get_attr (node, "msg"));
return FALSE;
}
return TRUE;
}
#if BUILD_TESTS
void
test_flickr_error (void)
{
RestXmlParser *parser;
RestXmlNode *root;
GError *error;
const char test_1[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<rsp stat=\"ok\"><auth></auth></rsp>";
const char test_2[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<foobar/>";
const char test_3[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<rsp stat=\"fail\"><err code=\"108\" msg=\"Invalid frob\" /></rsp>";
parser = rest_xml_parser_new ();
root = rest_xml_parser_parse_from_data (parser, test_1, sizeof (test_1) - 1);
error = NULL;
flickr_proxy_is_successful (root, &error);
g_assert_no_error (error);
rest_xml_node_unref (root);
error = NULL;
root = rest_xml_parser_parse_from_data (parser, test_2, sizeof (test_2) - 1);
flickr_proxy_is_successful (root, &error);
g_assert_error (error, FLICKR_PROXY_ERROR, 0);
g_error_free (error);
rest_xml_node_unref (root);
error = NULL;
root = rest_xml_parser_parse_from_data (parser, test_3, sizeof (test_3) - 1);
flickr_proxy_is_successful (root, &error);
g_assert_error (error, FLICKR_PROXY_ERROR, 108);
g_error_free (error);
rest_xml_node_unref (root);
}
#endif
|