summaryrefslogtreecommitdiff
path: root/gfbgraph/gfbgraph-goa-authorizer.c
blob: 3a8d3b9ee63a76fde049317e72c645c373a2383e (plain)
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
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 8; tab-width: 8 -*-  */
/*
 * libgfbgraph - GObject library for Facebook Graph API
 * Copyright (C) 2013 Álvaro Peña <alvaropg@gmail.com>
 *
 * This library is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * libgfbgraph is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * SECTION:gfbgraph-goa-authorizer
 * @short_description: GFBGraph GOA authorization interface
 * @stability: Unstable
 * @include: gfbgraph/gfbgraph.h
 *
 * #GFBGraphGoaAuthorizer provides an implementation of the #GFBGraphAuthorizer interface
 * for authorization using GNOME Online Accounts (GOA).
 **/

#include "gfbgraph-authorizer.h"
#include "gfbgraph-goa-authorizer.h"

enum {
        PROP_O,

        PROP_GOA_OBJECT
};

struct _GFBGraphGoaAuthorizerPrivate {
        GMutex mutex;
        GoaObject *goa_object;
        gchar *access_token;
};

static void gfbgraph_goa_authorizer_class_init            (GFBGraphGoaAuthorizerClass *klass);
static void gfbgraph_goa_authorizer_init                  (GFBGraphGoaAuthorizer *object);
static void gfbgraph_goa_authorizer_finalize              (GObject *object);
static void gfbgraph_goa_authorizer_dispose               (GObject *object);
static void gfbgraph_goa_authorizer_set_property          (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
static void gfbgraph_goa_authorizer_get_property          (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);

static void gfbgraph_goa_authorizer_iface_init            (GFBGraphAuthorizerInterface *iface);
void        gfbgraph_goa_authorizer_process_call          (GFBGraphAuthorizer *iface, RestProxyCall *call);
void        gfbgraph_goa_authorizer_process_message       (GFBGraphAuthorizer *iface, SoupMessage *message);
gboolean    gfbgraph_goa_authorizer_refresh_authorization (GFBGraphAuthorizer *iface, GCancellable *cancellable, GError **error);

static void gfbgraph_goa_authorizer_set_goa_object        (GFBGraphGoaAuthorizer *self, GoaObject *goa_object);

#define GFBGRAPH_GOA_AUTHORIZER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), GFBGRAPH_TYPE_GOA_AUTHORIZER, GFBGraphGoaAuthorizerPrivate))

static GObjectClass *parent_class = NULL;

G_DEFINE_TYPE_WITH_CODE (GFBGraphGoaAuthorizer, gfbgraph_goa_authorizer, G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (GFBGRAPH_TYPE_AUTHORIZER, gfbgraph_goa_authorizer_iface_init));

static void
gfbgraph_goa_authorizer_class_init (GFBGraphGoaAuthorizerClass *klass)
{
	GObjectClass *gobject_class;
	gobject_class = (GObjectClass*) klass;

	parent_class            = g_type_class_peek_parent (klass);
	gobject_class->finalize = gfbgraph_goa_authorizer_finalize;
        gobject_class->get_property = gfbgraph_goa_authorizer_get_property;
        gobject_class->set_property = gfbgraph_goa_authorizer_set_property;
        gobject_class->dispose = gfbgraph_goa_authorizer_dispose;

        /**
         * GFBGraphGoaAuthorizer:goa-object:
         *
         * The GOA account providing authentication.
         *
         **/
        g_object_class_install_property (gobject_class,
                                         PROP_GOA_OBJECT,
                                         g_param_spec_object ("goa-object",
                                                              "GoaObject",
                                                              "The GOA account to authenticate.",
                                                              GOA_TYPE_OBJECT,
                                                              G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));

	g_type_class_add_private (gobject_class, sizeof(GFBGraphGoaAuthorizerPrivate));
}

static void
gfbgraph_goa_authorizer_init (GFBGraphGoaAuthorizer *object)
{
	object->priv = GFBGRAPH_GOA_AUTHORIZER_GET_PRIVATE(object);
        g_mutex_init (&object->priv->mutex);
}

static void
gfbgraph_goa_authorizer_finalize (GObject *object)
{
	G_OBJECT_CLASS(parent_class)->finalize (object);
}

static void
gfbgraph_goa_authorizer_dispose (GObject *object)
{
        GFBGraphGoaAuthorizerPrivate *priv;

        priv = GFBGRAPH_GOA_AUTHORIZER_GET_PRIVATE (object);

        g_clear_object (&priv->goa_object);

        G_OBJECT_CLASS (parent_class)->dispose (object);
}

static void
gfbgraph_goa_authorizer_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
        switch (prop_id) {
                case PROP_GOA_OBJECT:
                        gfbgraph_goa_authorizer_set_goa_object (GFBGRAPH_GOA_AUTHORIZER (object), GOA_OBJECT (g_value_get_object (value)));
                        break;
                default:
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                        break;
        }
}

static void
gfbgraph_goa_authorizer_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
        GFBGraphGoaAuthorizerPrivate *priv;

        priv = GFBGRAPH_GOA_AUTHORIZER_GET_PRIVATE (object);

        switch (prop_id) {
                case PROP_GOA_OBJECT:
                        g_value_set_object (value, priv->goa_object);
                        break;
                default:
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                        break;
        }
}

static void
gfbgraph_goa_authorizer_iface_init (GFBGraphAuthorizerInterface *iface)
{
        iface->process_call = gfbgraph_goa_authorizer_process_call;
        iface->process_message = gfbgraph_goa_authorizer_process_message;
        iface->refresh_authorization = gfbgraph_goa_authorizer_refresh_authorization;
}

void
gfbgraph_goa_authorizer_process_call (GFBGraphAuthorizer *iface, RestProxyCall *call)
{
        GFBGraphGoaAuthorizerPrivate *priv = GFBGRAPH_GOA_AUTHORIZER_GET_PRIVATE (GFBGRAPH_GOA_AUTHORIZER (iface));

        g_mutex_lock (&priv->mutex);

        if (priv->access_token != NULL)
                rest_proxy_call_add_param (call, "access_token", priv->access_token);

        g_mutex_unlock (&priv->mutex);
}

void
gfbgraph_goa_authorizer_process_message (GFBGraphAuthorizer *iface, SoupMessage *message)
{
        gchar *auth_value;
        SoupURI *uri;
        GFBGraphGoaAuthorizerPrivate *priv;

        priv = GFBGRAPH_GOA_AUTHORIZER_GET_PRIVATE (GFBGRAPH_GOA_AUTHORIZER (iface));

        g_mutex_lock (&priv->mutex);

        uri = soup_message_get_uri (message);
        auth_value = g_strconcat ("access_token=", priv->access_token, NULL);
        soup_uri_set_query (uri, auth_value);

        g_free (auth_value);

        g_mutex_unlock (&priv->mutex);
}

gboolean
gfbgraph_goa_authorizer_refresh_authorization (GFBGraphAuthorizer *iface, GCancellable *cancellable, GError **error)
{
        GFBGraphGoaAuthorizerPrivate *priv;
        GoaAccount *account;
        GoaOAuth2Based *oauth2_based;
        gboolean ret_val;

        priv = GFBGRAPH_GOA_AUTHORIZER_GET_PRIVATE (GFBGRAPH_GOA_AUTHORIZER (iface));
        ret_val = FALSE;

        g_mutex_lock (&priv->mutex);

        g_free (priv->access_token);
        priv->access_token = NULL;

        account = goa_object_peek_account (priv->goa_object);
        oauth2_based = goa_object_peek_oauth2_based (priv->goa_object);

        if (goa_account_call_ensure_credentials_sync (account, NULL, cancellable, error))
                if (goa_oauth2_based_call_get_access_token_sync (oauth2_based, &priv->access_token, NULL, cancellable, error))
                        ret_val = TRUE;

        g_mutex_unlock (&priv->mutex);
        return ret_val;
}

static void
gfbgraph_goa_authorizer_set_goa_object (GFBGraphGoaAuthorizer *self, GoaObject *goa_object)
{
        GoaAccount *account;
        GoaOAuth2Based *oauth2_based;
        GFBGraphGoaAuthorizerPrivate *priv;

        g_return_if_fail (GOA_IS_OBJECT (goa_object));

        priv = GFBGRAPH_GOA_AUTHORIZER_GET_PRIVATE (self);

        oauth2_based = goa_object_peek_oauth2_based (goa_object);
        g_return_if_fail (oauth2_based != NULL && GOA_IS_OAUTH2_BASED (oauth2_based));

        account = goa_object_peek_account (goa_object);
        g_return_if_fail (account != NULL && GOA_IS_ACCOUNT (account));

        g_object_ref (goa_object);
        priv->goa_object = goa_object;
}

/**
 * gfbgraph_goa_authorizer_new:
 * @goa_object: A #GoaObject representing a Facebook account.
 *
 * Creates a new #GFBGraphGoaAuthorizer using @goa_object as account.
 *
 * Returns: (transfer full): A new #GFBGraphGoaAuthorizer. Use g_object_unref() to free it.
 */
GFBGraphGoaAuthorizer*
gfbgraph_goa_authorizer_new (GoaObject *goa_object)
{
	return GFBGRAPH_GOA_AUTHORIZER (g_object_new (GFBGRAPH_TYPE_GOA_AUTHORIZER, "goa-object", goa_object, NULL));
}