Commit a8fff6d5 authored by Paolo Bacchilega's avatar Paolo Bacchilega

picasaweb: remember application authorization

when an auth error occurs ask for a new access token

[bug #784142]
parent 6b31d47f
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
#define PICASA_WEB_REDIRECT_URI "urn:ietf:wg:oauth:2.0:oob" #define PICASA_WEB_REDIRECT_URI "urn:ietf:wg:oauth:2.0:oob"
#define PICASA_WEB_REDIRECT_TITLE "Success code=" #define PICASA_WEB_REDIRECT_TITLE "Success code="
#define PICASA_WEB_SERVICE_ERROR_TOKEN_EXPIRED 190 #define PICASA_WEB_SERVICE_ERROR_TOKEN_EXPIRED 190
#define PICASA_WEB_SERVICE_ERROR_UNAUTHORIZED 401
#define GTHUMB_PICASA_WEB_CLIENT_ID "499958842898.apps.googleusercontent.com" #define GTHUMB_PICASA_WEB_CLIENT_ID "499958842898.apps.googleusercontent.com"
#define GTHUMB_PICASA_WEB_CLIENT_SECRET "-DdIqzDxVRc_Wkobuf-2g-of" #define GTHUMB_PICASA_WEB_CLIENT_SECRET "-DdIqzDxVRc_Wkobuf-2g-of"
...@@ -87,6 +88,7 @@ struct _PicasaWebServicePrivate { ...@@ -87,6 +88,7 @@ struct _PicasaWebServicePrivate {
guint64 quota_limit; guint64 quota_limit;
guint64 quota_used; guint64 quota_used;
PostPhotosData *post_photos; PostPhotosData *post_photos;
int n_auth_errors;
}; };
...@@ -174,6 +176,7 @@ picasa_web_utils_parse_json_response (SoupMessage *msg, ...@@ -174,6 +176,7 @@ picasa_web_utils_parse_json_response (SoupMessage *msg,
/* -- _picasa_web_service_get_refresh_token -- */ /* -- _picasa_web_service_get_refresh_token -- */
static void static void
_picasa_web_service_get_refresh_token_ready_cb (SoupSession *session, _picasa_web_service_get_refresh_token_ready_cb (SoupSession *session,
SoupMessage *msg, SoupMessage *msg,
...@@ -299,6 +302,7 @@ picasa_web_service_get_authorization_url (PicasaWebService *self) ...@@ -299,6 +302,7 @@ picasa_web_service_get_authorization_url (PicasaWebService *self)
g_hash_table_insert (data_set, "client_id", GTHUMB_PICASA_WEB_CLIENT_ID); g_hash_table_insert (data_set, "client_id", GTHUMB_PICASA_WEB_CLIENT_ID);
g_hash_table_insert (data_set, "redirect_uri", PICASA_WEB_REDIRECT_URI); g_hash_table_insert (data_set, "redirect_uri", PICASA_WEB_REDIRECT_URI);
g_hash_table_insert (data_set, "scope", "https://picasaweb.google.com/data/ https://www.googleapis.com/auth/userinfo.profile"); g_hash_table_insert (data_set, "scope", "https://picasaweb.google.com/data/ https://www.googleapis.com/auth/userinfo.profile");
g_hash_table_insert (data_set, "access_type", "offline");
link = g_string_new ("https://accounts.google.com/o/oauth2/auth?"); link = g_string_new ("https://accounts.google.com/o/oauth2/auth?");
keys = g_hash_table_get_keys (data_set); keys = g_hash_table_get_keys (data_set);
...@@ -424,12 +428,36 @@ _picasa_web_service_get_access_token_finish (PicasaWebService *service, ...@@ -424,12 +428,36 @@ _picasa_web_service_get_access_token_finish (PicasaWebService *service,
/* -- picasa_web_service_get_user_info -- */ /* -- picasa_web_service_get_user_info -- */
typedef struct {
PicasaWebService *service;
GCancellable *cancellable;
GAsyncReadyCallback callback;
gpointer user_data;
} AccessTokenData;
static void
access_token_data_free (AccessTokenData *data)
{
_g_object_unref (data->cancellable);
g_free (data);
}
static void
picasa_web_service_get_user_info (WebService *base,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static void static void
picasa_web_service_get_user_info_ready_cb (SoupSession *session, picasa_web_service_get_user_info_ready_cb (SoupSession *session,
SoupMessage *msg, SoupMessage *msg,
gpointer user_data) gpointer user_data)
{ {
PicasaWebService *self = user_data; AccessTokenData *data = user_data;
PicasaWebService *self = data->service;
GSimpleAsyncResult *result; GSimpleAsyncResult *result;
GError *error = NULL; GError *error = NULL;
JsonNode *node; JsonNode *node;
...@@ -451,36 +479,37 @@ picasa_web_service_get_user_info_ready_cb (SoupSession *session, ...@@ -451,36 +479,37 @@ picasa_web_service_get_user_info_ready_cb (SoupSession *session,
_g_object_unref (account); _g_object_unref (account);
json_node_free (node); json_node_free (node);
} }
else else {
g_simple_async_result_set_from_error (result, error); if (error->code == PICASA_WEB_SERVICE_ERROR_UNAUTHORIZED) {
self->priv->n_auth_errors += 1;
if (self->priv->n_auth_errors == 1) {
OAuthAccount *account;
g_simple_async_result_complete_in_idle (result); /* reset the account token to force asking the
} * access token again. */
account = web_service_get_current_account (WEB_SERVICE (self));
if (account != NULL)
_g_strset (&account->token, NULL);
typedef struct { picasa_web_service_get_user_info (WEB_SERVICE (self),
PicasaWebService *service; data->cancellable,
GCancellable *cancellable; data->callback,
GAsyncReadyCallback callback; data->user_data);
gpointer user_data; access_token_data_free (data);
} AccessTokenData; return;
}
}
g_simple_async_result_set_from_error (result, error);
}
self->priv->n_auth_errors = 0;
g_simple_async_result_complete_in_idle (result);
static void access_token_data_free (data);
access_token_data_free (AccessTokenData *data)
{
_g_object_unref (data->cancellable);
g_free (data);
} }
static void
picasa_web_service_get_user_info (WebService *base,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static void static void
access_token_ready_cb (GObject *source_object, access_token_ready_cb (GObject *source_object,
GAsyncResult *result, GAsyncResult *result,
...@@ -493,6 +522,9 @@ access_token_ready_cb (GObject *source_object, ...@@ -493,6 +522,9 @@ access_token_ready_cb (GObject *source_object,
if (! _picasa_web_service_get_access_token_finish (self, result, &error)) { if (! _picasa_web_service_get_access_token_finish (self, result, &error)) {
GSimpleAsyncResult *result; GSimpleAsyncResult *result;
if (error->code == PICASA_WEB_SERVICE_ERROR_UNAUTHORIZED)
self->priv->n_auth_errors += 1;
result = g_simple_async_result_new (G_OBJECT (self), result = g_simple_async_result_new (G_OBJECT (self),
data->callback, data->callback,
data->user_data, data->user_data,
...@@ -522,8 +554,8 @@ picasa_web_service_get_user_info (WebService *base, ...@@ -522,8 +554,8 @@ picasa_web_service_get_user_info (WebService *base,
{ {
PicasaWebService *self = PICASA_WEB_SERVICE (base); PicasaWebService *self = PICASA_WEB_SERVICE (base);
OAuthAccount *account; OAuthAccount *account;
GHashTable *data_set;
SoupMessage *msg; SoupMessage *msg;
AccessTokenData *data;
account = web_service_get_current_account (WEB_SERVICE (self)); account = web_service_get_current_account (WEB_SERVICE (self));
if (account != NULL) { if (account != NULL) {
...@@ -531,8 +563,16 @@ picasa_web_service_get_user_info (WebService *base, ...@@ -531,8 +563,16 @@ picasa_web_service_get_user_info (WebService *base,
_g_strset (&self->priv->access_token, account->token); _g_strset (&self->priv->access_token, account->token);
} }
data_set = g_hash_table_new (g_str_hash, g_str_equal); data = g_new0 (AccessTokenData, 1);
data->service = self;
data->cancellable = _g_object_ref (cancellable);
data->callback = callback;
data->user_data = user_data;
if (self->priv->access_token != NULL) { if (self->priv->access_token != NULL) {
GHashTable *data_set;
data_set = g_hash_table_new (g_str_hash, g_str_equal);
msg = soup_form_request_new_from_hash ("GET", "https://www.googleapis.com/oauth2/v2/userinfo", data_set); msg = soup_form_request_new_from_hash ("GET", "https://www.googleapis.com/oauth2/v2/userinfo", data_set);
_picasa_web_service_add_headers (self, msg); _picasa_web_service_add_headers (self, msg);
_web_service_send_message (WEB_SERVICE (self), _web_service_send_message (WEB_SERVICE (self),
...@@ -542,26 +582,18 @@ picasa_web_service_get_user_info (WebService *base, ...@@ -542,26 +582,18 @@ picasa_web_service_get_user_info (WebService *base,
user_data, user_data,
picasa_web_service_get_user_info, picasa_web_service_get_user_info,
picasa_web_service_get_user_info_ready_cb, picasa_web_service_get_user_info_ready_cb,
self); data);
g_hash_table_destroy (data_set);
} }
else { else {
/* Get the access token from the refresh token */ /* Get the access token from the refresh token */
AccessTokenData *data;
data = g_new0 (AccessTokenData, 1);
data->service = self;
data->cancellable = _g_object_ref (cancellable);
data->callback = callback;
data->user_data = user_data;
_picasa_web_service_get_access_token (self, _picasa_web_service_get_access_token (self,
self->priv->refresh_token, self->priv->refresh_token,
cancellable, cancellable,
access_token_ready_cb, access_token_ready_cb,
data); data);
} }
g_hash_table_destroy (data_set);
} }
...@@ -591,6 +623,7 @@ picasa_web_service_init (PicasaWebService *self) ...@@ -591,6 +623,7 @@ picasa_web_service_init (PicasaWebService *self)
self->priv->quota_limit = 0; self->priv->quota_limit = 0;
self->priv->quota_used = 0; self->priv->quota_used = 0;
self->priv->post_photos = NULL; self->priv->post_photos = NULL;
self->priv->n_auth_errors = 0;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment