| 1 |
/* |
| 2 |
* librest - RESTful web services access |
| 3 |
* Copyright (c) 2008, 2009, Intel Corporation. |
| 4 |
* |
| 5 |
* Authors: Rob Bradford <[email protected]> |
| 6 |
* Ross Burton <[email protected]> |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or modify it |
| 9 |
* under the terms and conditions of the GNU Lesser General Public License, |
| 10 |
* version 2.1, as published by the Free Software Foundation. |
| 11 |
* |
| 12 |
* This program is distributed in the hope it will be useful, but WITHOUT ANY |
| 13 |
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 14 |
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
| 15 |
* more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public License |
| 18 |
* along with this program; if not, write to the Free Software Foundation, |
| 19 |
* Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
/* |
| 24 |
* TODO: |
| 25 |
* |
| 26 |
* Convenience API for authentication so that the user doesn't have to parse the |
| 27 |
* XML themselves. |
| 28 |
*/ |
| 29 |
|
| 30 |
#include <config.h> |
| 31 |
#include <stdlib.h> |
| 32 |
#include <string.h> |
| 33 |
#include <rest/rest-proxy.h> |
| 34 |
#include <libsoup/soup.h> |
| 35 |
#include "flickr-proxy.h" |
| 36 |
#include "flickr-proxy-call.h" |
| 37 |
|
| 38 |
typedef struct { |
| 39 |
char *api_key; |
| 40 |
char *shared_secret; |
| 41 |
char *token; |
| 42 |
} FlickrProxyPrivate; |
| 43 |
|
| 44 |
G_DEFINE_TYPE_WITH_PRIVATE (FlickrProxy, flickr_proxy, REST_TYPE_PROXY) |
| 45 |
|
| 46 |
enum { |
| 47 |
PROP_0, |
| 48 |
PROP_API_KEY, |
| 49 |
PROP_SHARED_SECRET, |
| 50 |
PROP_TOKEN, |
| 51 |
N_PROPS |
| 52 |
}; |
| 53 |
|
| 54 |
static GParamSpec *properties [N_PROPS]; |
| 55 |
|
| 56 |
G_DEFINE_QUARK (rest-flickr-proxy-error-quark, flickr_proxy_error) |
| 57 |
|
| 58 |
static RestProxyCall * |
| 59 |
_new_call (RestProxy *self) |
| 60 |
{ |
| 61 |
RestProxyCall *call; |
| 62 |
|
| 63 |
call = g_object_new (FLICKR_TYPE_PROXY_CALL, |
| 64 |
"proxy", self, |
| 65 |
NULL); |
| 66 |
|
| 67 |
return call; |
| 68 |
} |
| 69 |
|
| 70 |
static void |
| 71 |
flickr_proxy_get_property (GObject *object, |
| 72 |
guint property_id, |
| 73 |
GValue *value, |
| 74 |
GParamSpec *pspec) |
| 75 |
{ |
| 76 |
FlickrProxy *self = FLICKR_PROXY (object); |
| 77 |
FlickrProxyPrivate *priv = flickr_proxy_get_instance_private (self); |
| 78 |
|
| 79 |
switch (property_id) { |
| 80 |
case PROP_API_KEY: |
| 81 |
g_value_set_string (value, priv->api_key); |
| 82 |
break; |
| 83 |
case PROP_SHARED_SECRET: |
| 84 |
g_value_set_string (value, priv->shared_secret); |
| 85 |
break; |
| 86 |
case PROP_TOKEN: |
| 87 |
g_value_set_string (value, priv->token); |
| 88 |
break; |
| 89 |
default: |
| 90 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
static void |
| 95 |
flickr_proxy_set_property (GObject *object, |
| 96 |
guint property_id, |
| 97 |
const GValue *value, |
| 98 |
GParamSpec *pspec) |
| 99 |
{ |
| 100 |
FlickrProxy *self = FLICKR_PROXY (object); |
| 101 |
FlickrProxyPrivate *priv = flickr_proxy_get_instance_private (self); |
| 102 |
|
| 103 |
switch (property_id) { |
| 104 |
case PROP_API_KEY: |
| 105 |
if (priv->api_key) |
| 106 |
g_free (priv->api_key); |
| 107 |
priv->api_key = g_value_dup_string (value); |
| 108 |
break; |
| 109 |
case PROP_SHARED_SECRET: |
| 110 |
if (priv->shared_secret) |
| 111 |
g_free (priv->shared_secret); |
| 112 |
priv->shared_secret = g_value_dup_string (value); |
| 113 |
break; |
| 114 |
case PROP_TOKEN: |
| 115 |
if (priv->token) |
| 116 |
g_free (priv->token); |
| 117 |
priv->token = g_value_dup_string (value); |
| 118 |
break; |
| 119 |
default: |
| 120 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
static void |
| 125 |
flickr_proxy_finalize (GObject *object) |
| 126 |
{ |
| 127 |
FlickrProxy *self = FLICKR_PROXY (object); |
| 128 |
FlickrProxyPrivate *priv = flickr_proxy_get_instance_private (self); |
| 129 |
|
| 130 |
g_clear_pointer (&priv->api_key, g_free); |
| 131 |
g_clear_pointer (&priv->shared_secret, g_free); |
| 132 |
g_clear_pointer (&priv->token, g_free); |
| 133 |
|
| 134 |
G_OBJECT_CLASS (flickr_proxy_parent_class)->finalize (object); |
| 135 |
} |
| 136 |
|
| 137 |
static void |
| 138 |
flickr_proxy_class_init (FlickrProxyClass *klass) |
| 139 |
{ |
| 140 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 141 |
RestProxyClass *proxy_class = REST_PROXY_CLASS (klass); |
| 142 |
|
| 143 |
object_class->get_property = flickr_proxy_get_property; |
| 144 |
object_class->set_property = flickr_proxy_set_property; |
| 145 |
object_class->finalize = flickr_proxy_finalize; |
| 146 |
|
| 147 |
proxy_class->new_call = _new_call; |
| 148 |
|
| 149 |
properties [PROP_API_KEY] = |
| 150 |
g_param_spec_string ("api-key", |
| 151 |
"api-key", |
| 152 |
"The API key", |
| 153 |
NULL, |
| 154 |
(G_PARAM_READWRITE | |
| 155 |
G_PARAM_CONSTRUCT_ONLY | |
| 156 |
G_PARAM_STATIC_STRINGS)); |
| 157 |
|
| 158 |
properties [PROP_SHARED_SECRET] = |
| 159 |
g_param_spec_string ("shared-secret", |
| 160 |
"shared-secret", |
| 161 |
"The shared secret", |
| 162 |
NULL, |
| 163 |
(G_PARAM_READWRITE | |
| 164 |
G_PARAM_CONSTRUCT_ONLY | |
| 165 |
G_PARAM_STATIC_STRINGS)); |
| 166 |
|
| 167 |
properties [PROP_TOKEN] = |
| 168 |
g_param_spec_string ("token", |
| 169 |
"token", |
| 170 |
"The request or access token", |
| 171 |
NULL, |
| 172 |
(G_PARAM_READWRITE | |
| 173 |
G_PARAM_STATIC_STRINGS)); |
| 174 |
|
| 175 |
g_object_class_install_properties (object_class, N_PROPS, properties); |
| 176 |
} |
| 177 |
|
| 178 |
static void |
| 179 |
flickr_proxy_init (FlickrProxy *self) |
| 180 |
{ |
| 181 |
} |
| 182 |
|
| 183 |
RestProxy * |
| 184 |
flickr_proxy_new (const char *api_key, |
| 185 |
const char *shared_secret) |
| 186 |
{ |
| 187 |
return flickr_proxy_new_with_token (api_key, |
| 188 |
shared_secret, |
| 189 |
NULL); |
| 190 |
} |
| 191 |
|
| 192 |
RestProxy * |
| 193 |
flickr_proxy_new_with_token (const char *api_key, |
| 194 |
const char *shared_secret, |
| 195 |
const char *token) |
| 196 |
{ |
| 197 |
return g_object_new (FLICKR_TYPE_PROXY, |
| 198 |
"api-key", api_key, |
| 199 |
"shared-secret", shared_secret, |
| 200 |
"token", token, |
| 201 |
"url-format", "https://%s.flickr.com/services/%s/", |
| 202 |
"binding-required", TRUE, |
| 203 |
NULL); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* flickr_proxy_get_api_key: |
| 208 |
* @proxy: an #FlickrProxy |
| 209 |
* |
| 210 |
* Get the API key. |
| 211 |
* |
| 212 |
* Returns: the API key. This string is owned by #FlickrProxy and should not be |
| 213 |
* freed. |
| 214 |
*/ |
| 215 |
const char * |
| 216 |
flickr_proxy_get_api_key (FlickrProxy *self) |
| 217 |
{ |
| 218 |
FlickrProxyPrivate *priv = flickr_proxy_get_instance_private (self); |
| 219 |
|
| 220 |
g_return_val_if_fail (FLICKR_IS_PROXY (self), NULL); |
| 221 |
|
| 222 |
return priv->api_key; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* flickr_proxy_get_shared_secret: |
| 227 |
* @proxy: an #FlickrProxy |
| 228 |
* |
| 229 |
* Get the shared secret for authentication. |
| 230 |
* |
| 231 |
* Returns: the shared secret. This string is owned by #FlickrProxy and should not be |
| 232 |
* freed. |
| 233 |
*/ |
| 234 |
const char * |
| 235 |
flickr_proxy_get_shared_secret (FlickrProxy *self) |
| 236 |
{ |
| 237 |
FlickrProxyPrivate *priv = flickr_proxy_get_instance_private (self); |
| 238 |
|
| 239 |
g_return_val_if_fail (FLICKR_IS_PROXY (self), NULL); |
| 240 |
|
| 241 |
return priv->shared_secret; |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* flickr_proxy_get_token: |
| 246 |
* @proxy: an #FlickrProxy |
| 247 |
* |
| 248 |
* Get the current token. |
| 249 |
* |
| 250 |
* Returns: (nullable): the token, or %NULL if there is no token yet. |
| 251 |
* This string is owned by #FlickrProxy and should not be freed. |
| 252 |
*/ |
| 253 |
const char * |
| 254 |
flickr_proxy_get_token (FlickrProxy *self) |
| 255 |
{ |
| 256 |
FlickrProxyPrivate *priv = flickr_proxy_get_instance_private (self); |
| 257 |
|
| 258 |
g_return_val_if_fail (FLICKR_IS_PROXY (self), NULL); |
| 259 |
|
| 260 |
return priv->token; |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* flickr_proxy_set_token: |
| 265 |
* @proxy: an #FlickrProxy |
| 266 |
* @token: the access token |
| 267 |
* |
| 268 |
* Set the token. |
| 269 |
*/ |
| 270 |
void |
| 271 |
flickr_proxy_set_token (FlickrProxy *self, |
| 272 |
const char *token) |
| 273 |
{ |
| 274 |
FlickrProxyPrivate *priv; |
| 275 |
|
| 276 |
g_return_if_fail (FLICKR_IS_PROXY (self)); |
| 277 |
|
| 278 |
priv = flickr_proxy_get_instance_private (self); |
| 279 |
|
| 280 |
if (g_strcmp0 (priv->token, token) != 0) |
| 281 |
{ |
| 282 |
g_clear_pointer (&priv->token, g_free); |
| 283 |
priv->token = g_strdup (token); |
| 284 |
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TOKEN]); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* flickr_proxy_sign: |
| 290 |
* @proxy: an #FlickrProxy |
| 291 |
* @params: (element-type utf8 utf8): the request parameters |
| 292 |
* |
| 293 |
* Get the md5 checksum of the request. |
| 294 |
* |
| 295 |
* Returns: The md5 checksum of the request |
| 296 |
*/ |
| 297 |
char * |
| 298 |
flickr_proxy_sign (FlickrProxy *self, |
| 299 |
GHashTable *params) |
| 300 |
{ |
| 301 |
FlickrProxyPrivate *priv; |
| 302 |
GList *keys; |
| 303 |
char *md5; |
| 304 |
GChecksum *checksum; |
| 305 |
|
| 306 |
g_return_val_if_fail (FLICKR_IS_PROXY (self), NULL); |
| 307 |
g_return_val_if_fail (params, NULL); |
| 308 |
|
| 309 |
priv = flickr_proxy_get_instance_private (self); |
| 310 |
|
| 311 |
checksum = g_checksum_new (G_CHECKSUM_MD5); |
| 312 |
g_checksum_update (checksum, (guchar *)priv->shared_secret, -1); |
| 313 |
|
| 314 |
keys = g_hash_table_get_keys (params); |
| 315 |
keys = g_list_sort (keys, (GCompareFunc)strcmp); |
| 316 |
|
| 317 |
while (keys) { |
| 318 |
const char *key, *value; |
| 319 |
|
| 320 |
key = keys->data; |
| 321 |
value = g_hash_table_lookup (params, key); |
| 322 |
|
| 323 |
g_checksum_update (checksum, (guchar *)key, -1); |
| 324 |
g_checksum_update (checksum, (guchar *)value, -1); |
| 325 |
|
| 326 |
keys = g_list_delete_link (keys, keys); |
| 327 |
} |
| 328 |
|
| 329 |
md5 = g_strdup (g_checksum_get_string (checksum)); |
| 330 |
g_checksum_free (checksum); |
| 331 |
|
| 332 |
return md5; |
| 333 |
} |
| 334 |
|
| 335 |
char * |
| 336 |
flickr_proxy_build_login_url (FlickrProxy *self, |
| 337 |
const char *frob, |
| 338 |
const char *perms) |
| 339 |
{ |
| 340 |
FlickrProxyPrivate *priv; |
| 341 |
GUri *uri; |
| 342 |
GHashTable *params; |
| 343 |
char *sig, *s; |
| 344 |
char *query; |
| 345 |
|
| 346 |
g_return_val_if_fail (FLICKR_IS_PROXY (self), NULL); |
| 347 |
|
| 348 |
priv = flickr_proxy_get_instance_private (self); |
| 349 |
|
| 350 |
params = g_hash_table_new (g_str_hash, g_str_equal); |
| 351 |
|
| 352 |
g_hash_table_insert (params, "api_key", priv->api_key); |
| 353 |
g_hash_table_insert (params, "perms", (gpointer)perms); |
| 354 |
|
| 355 |
if (frob) |
| 356 |
g_hash_table_insert (params, "frob", (gpointer)frob); |
| 357 |
|
| 358 |
sig = flickr_proxy_sign (self, params); |
| 359 |
g_hash_table_insert (params, "api_sig", sig); |
| 360 |
query = soup_form_encode_hash (params); |
| 361 |
|
| 362 |
uri = g_uri_build (G_URI_FLAGS_ENCODED, |
| 363 |
"http", |
| 364 |
NULL, |
| 365 |
"flickr.com", |
| 366 |
-1, |
| 367 |
"/services/auth/", |
| 368 |
query, |
| 369 |
NULL); |
| 370 |
|
| 371 |
s = g_uri_to_string (uri); |
| 372 |
|
| 373 |
g_free (query); |
| 374 |
g_free (sig); |
| 375 |
g_hash_table_destroy (params); |
| 376 |
g_uri_unref (uri); |
| 377 |
|
| 378 |
return s; |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* flickr_proxy_is_successful: |
| 383 |
* @root: The root node of a parsed Flickr response |
| 384 |
* @error: #GError to set if the response was an error |
| 385 |
* |
| 386 |
* Examines the Flickr response and if it not a successful reply, set @error and |
| 387 |
* return FALSE. |
| 388 |
* |
| 389 |
* Returns: %TRUE if this response is successful, %FALSE otherwise. |
| 390 |
*/ |
| 391 |
gboolean |
| 392 |
flickr_proxy_is_successful (RestXmlNode *root, |
| 393 |
GError **error) |
| 394 |
{ |
| 395 |
RestXmlNode *node; |
| 396 |
|
| 397 |
g_return_val_if_fail (root, FALSE); |
| 398 |
|
| 399 |
if (strcmp (root->name, "rsp") != 0) { |
| 400 |
g_set_error (error, FLICKR_PROXY_ERROR, 0, |
| 401 |
"Unexpected response from Flickr (root node %s)", |
| 402 |
root->name); |
| 403 |
return FALSE; |
| 404 |
} |
| 405 |
|
| 406 |
if (strcmp (rest_xml_node_get_attr (root, "stat"), "ok") != 0) { |
| 407 |
node = rest_xml_node_find (root, "err"); |
| 408 |
g_set_error_literal (error,FLICKR_PROXY_ERROR, |
| 409 |
atoi (rest_xml_node_get_attr (node, "code")), |
| 410 |
rest_xml_node_get_attr (node, "msg")); |
| 411 |
return FALSE; |
| 412 |
} |
| 413 |
|
| 414 |
return TRUE; |
| 415 |
} |
| 416 |
|
| 417 |
/** |
| 418 |
* flickr_proxy_new_upload: |
| 419 |
* @proxy: a valid #FlickrProxy |
| 420 |
* |
| 421 |
* Create a new #RestProxyCall that can be used for uploading. |
| 422 |
* |
| 423 |
* See http://www.flickr.com/services/api/upload.api.html for details on |
| 424 |
* uploading to Flickr. |
| 425 |
* |
| 426 |
* Returns: (type FlickrProxyCall) (transfer full): a new #FlickrProxyCall |
| 427 |
*/ |
| 428 |
RestProxyCall * |
| 429 |
flickr_proxy_new_upload (FlickrProxy *self) |
| 430 |
{ |
| 431 |
g_return_val_if_fail (FLICKR_IS_PROXY (self), NULL); |
| 432 |
|
| 433 |
return g_object_new (FLICKR_TYPE_PROXY_CALL, |
| 434 |
"proxy", self, |
| 435 |
"upload", TRUE, |
| 436 |
NULL); |
| 437 |
} |
| 438 |
|
| 439 |
/** |
| 440 |
* flickr_proxy_new_upload_for_file: |
| 441 |
* @proxy: a valid #FlickrProxy |
| 442 |
* @filename: the file to upload |
| 443 |
* @error: #GError to set on error |
| 444 |
|
| 445 |
* Create a new #RestProxyCall that can be used for uploading. @filename will |
| 446 |
* be set as the "photo" parameter for you, avoiding you from having to open the |
| 447 |
* file and determine the MIME type. |
| 448 |
* |
| 449 |
* Note that this function can in theory block. |
| 450 |
* |
| 451 |
* See http://www.flickr.com/services/api/upload.api.html for details on |
| 452 |
* uploading to Flickr. |
| 453 |
* |
| 454 |
* Returns: (type FlickrProxyCall) (transfer full): a new #FlickrProxyCall |
| 455 |
*/ |
| 456 |
RestProxyCall * |
| 457 |
flickr_proxy_new_upload_for_file (FlickrProxy *self, |
| 458 |
const char *filename, |
| 459 |
GError **error) |
| 460 |
{ |
| 461 |
GMappedFile *map; |
| 462 |
GError *err = NULL; |
| 463 |
char *basename = NULL, *content_type = NULL; |
| 464 |
RestParam *param; |
| 465 |
RestProxyCall *call = NULL; |
| 466 |
|
| 467 |
g_return_val_if_fail (FLICKR_IS_PROXY (self), NULL); |
| 468 |
g_return_val_if_fail (filename, NULL); |
| 469 |
|
| 470 |
/* Open the file */ |
| 471 |
map = g_mapped_file_new (filename, FALSE, &err); |
| 472 |
if (err) { |
| 473 |
g_propagate_error (error, err); |
| 474 |
return NULL; |
| 475 |
} |
| 476 |
|
| 477 |
/* Get the file information */ |
| 478 |
basename = g_path_get_basename (filename); |
| 479 |
content_type = g_content_type_guess (filename, |
| 480 |
(const guchar*) g_mapped_file_get_contents (map), |
| 481 |
g_mapped_file_get_length (map), |
| 482 |
NULL); |
| 483 |
|
| 484 |
/* Make the call */ |
| 485 |
call = flickr_proxy_new_upload (self); |
| 486 |
param = rest_param_new_with_owner ("photo", |
| 487 |
g_mapped_file_get_contents (map), |
| 488 |
g_mapped_file_get_length (map), |
| 489 |
content_type, |
| 490 |
basename, |
| 491 |
map, |
| 492 |
(GDestroyNotify)g_mapped_file_unref); |
| 493 |
rest_proxy_call_add_param_full (call, param); |
| 494 |
|
| 495 |
g_free (basename); |
| 496 |
g_free (content_type); |
| 497 |
|
| 498 |
return call; |
| 499 |
} |
| 500 |
|
| 501 |
#if BUILD_TESTS |
| 502 |
void |
| 503 |
test_flickr_error (void) |
| 504 |
{ |
| 505 |
RestXmlParser *parser; |
| 506 |
RestXmlNode *root; |
| 507 |
GError *error; |
| 508 |
const char test_1[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" |
| 509 |
"<rsp stat=\"ok\"><auth></auth></rsp>"; |
| 510 |
const char test_2[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" |
| 511 |
"<foobar/>"; |
| 512 |
const char test_3[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" |
| 513 |
"<rsp stat=\"fail\"><err code=\"108\" msg=\"Invalid frob\" /></rsp>"; |
| 514 |
|
| 515 |
parser = rest_xml_parser_new (); |
| 516 |
|
| 517 |
root = rest_xml_parser_parse_from_data (parser, test_1, sizeof (test_1) - 1); |
| 518 |
error = NULL; |
| 519 |
flickr_proxy_is_successful (root, &error); |
| 520 |
g_assert_no_error (error); |
| 521 |
rest_xml_node_unref (root); |
| 522 |
|
| 523 |
error = NULL; |
| 524 |
root = rest_xml_parser_parse_from_data (parser, test_2, sizeof (test_2) - 1); |
| 525 |
flickr_proxy_is_successful (root, &error); |
| 526 |
g_assert_error (error, FLICKR_PROXY_ERROR, 0); |
| 527 |
g_error_free (error); |
| 528 |
rest_xml_node_unref (root); |
| 529 |
|
| 530 |
error = NULL; |
| 531 |
root = rest_xml_parser_parse_from_data (parser, test_3, sizeof (test_3) - 1); |
| 532 |
flickr_proxy_is_successful (root, &error); |
| 533 |
g_assert_error (error, FLICKR_PROXY_ERROR, 108); |
| 534 |
g_error_free (error); |
| 535 |
rest_xml_node_unref (root); |
| 536 |
} |
| 537 |
#endif |