Perform basic authentication with the server upon start
[milk] / src / milk-auth.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License as
4  * published by the Free Software Foundation; either version 2 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  * General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public
13  * License along with this program; if not, write to the
14  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
15  * Boston, MA  02110-1301  USA
16  *
17  * Authors: Travis Reitter <treitter@gmail.com>
18  */
19
20 #include <config.h>
21
22 #include <glib.h>
23 #include <glib/gi18n.h>
24 #include <gtk/gtk.h>
25 #include <hildon/hildon.h>
26
27 #include <rtm-glib/rtm-error.h>
28 #include <rtm-glib/rtm-glib.h>
29
30 #include "milk-auth.h"
31
32 G_DEFINE_TYPE (MilkAuth, milk_auth, G_TYPE_OBJECT);
33
34 /* less expensive than G_TYPE_INSTANCE_GET_PRIVATE */
35 #define MILK_AUTH_PRIVATE(o) ((MILK_AUTH ((o)))->priv)
36
37 #define RTM_API_KEY "81f5c6c904aeafbbc914d9845d250ea8"
38 #define RTM_SHARED_SECRET "b08b15419378f913"
39
40 struct _MilkAuthPrivate
41 {
42         RtmGlib *rtm_glib;
43         char *api_key;
44         char *shared_secret;
45 };
46
47 enum {
48         PROP_API_KEY = 1,
49         PROP_SHARED_SECRET,
50 };
51
52 static MilkAuth *default_auth = NULL;
53
54
55 static void
56 milk_auth_get_property (GObject    *object,
57                         guint       property_id,
58                         GValue     *value,
59                         GParamSpec *pspec)
60 {
61         MilkAuthPrivate *priv = MILK_AUTH_PRIVATE (object);
62
63         switch (property_id)
64         {
65                 case PROP_API_KEY:
66                         g_value_set_string (value, priv->api_key);
67                 break;
68
69                 case PROP_SHARED_SECRET:
70                         g_value_set_string (value, priv->shared_secret);
71                 break;
72
73                 default:
74                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id,
75                                         pspec);
76         }
77 }
78
79 static void
80 milk_auth_set_property (GObject      *object,
81                         guint         property_id,
82                         const GValue *value,
83                         GParamSpec   *pspec)
84 {
85         MilkAuthPrivate *priv;
86         MilkAuth *auth;
87
88         auth = MILK_AUTH (object);
89         priv = MILK_AUTH_PRIVATE (auth);
90
91         switch (property_id)
92         {
93                 case PROP_API_KEY:
94                         priv->api_key = g_value_dup_string (value);
95                 break;
96
97                 case PROP_SHARED_SECRET:
98                         priv->shared_secret = g_value_dup_string (value);
99                 break;
100
101                 default:
102                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id,
103                                         pspec);
104         }
105 }
106
107 void
108 milk_auth_run_demo (MilkAuth *auth)
109 {
110         MilkAuthPrivate *priv;
111
112         /* FIXME: clean this up */
113         GError *error = NULL;
114         /* FIXME: cut this
115         RtmGlib *rtm;
116         */
117         gchar *frob;
118         gchar *url;
119         gchar *auth_token;
120         gchar *username;
121         /* FIXME: cut this
122         GList *glist;
123         GList *item;
124         RtmTask *task;
125         RtmList *rtm_list;
126         gchar *timeline;
127         gchar *transaction_id;
128         RtmLocation *location;
129         gchar *list_id_sent = NULL;
130         RtmTimeZone *time_zone;
131         gchar *time;
132         */
133
134         priv = MILK_AUTH_PRIVATE (auth);
135
136         if (rtm_glib_test_echo (priv->rtm_glib, &error)) {
137                 g_print ("Test echo OK!\n");
138         } else {
139                 g_print ("Test echo FAIL!\n");
140         }
141         if (error != NULL) {
142                 g_error ("%s", rtm_error_get_message (error));
143         }
144
145         /* FIXME: cut this
146         glist = rtm_glib_time_zones_get_list (priv->rtm_glib, &error);
147         if (error != NULL) {
148                 g_error ("%s", rtm_error_get_message (error));
149         }
150         for (item = glist; item; item = g_list_next (item)) {
151                 time_zone = (RtmTimeZone *) item->data;
152                 g_print ("%s", rtm_time_zone_to_string (time_zone));
153         }
154         g_list_free (glist);
155
156         time = rtm_glib_time_parse (priv->rtm_glib, "02/10/2009 10:25", NULL, FALSE, &error);
157         if (error != NULL) {
158                 g_error ("%s", rtm_error_get_message (error));
159         }
160         g_print ("Time: %s\n", time);
161         g_free (time);
162
163         time = rtm_glib_time_convert (priv->rtm_glib, "Europe/Madrid", NULL, NULL, &error);
164         if (error != NULL) {
165                 g_error ("%s", rtm_error_get_message (error));
166         }
167         g_print ("Time: %s\n", time);
168         g_free (time);
169         */
170
171         frob = rtm_glib_auth_get_frob (priv->rtm_glib, &error);
172         if (error != NULL) {
173                 g_error ("%s", rtm_error_get_message (error));
174         }
175         g_print ("Frob: %s\n", frob);
176
177         url = rtm_glib_auth_get_login_url (priv->rtm_glib, frob, "delete");
178         g_print ("URL: %s\n", url);
179
180         getchar ();
181
182         auth_token = rtm_glib_auth_get_token (priv->rtm_glib, frob, &error);
183         if (error != NULL) {
184                 g_error ("%s", rtm_error_get_message (error));
185         }
186
187         if (!rtm_glib_auth_check_token (priv->rtm_glib, auth_token, NULL)) {
188                 g_error ("auth_token not valid!\n");
189                 goto auth_response_cb_error_OUT;
190         }
191         if (error != NULL) {
192                 g_error ("%s", rtm_error_get_message (error));
193         }
194         username = rtm_glib_test_login (priv->rtm_glib, auth_token, &error);
195
196         g_free (auth_token);
197
198         if (error != NULL) {
199                 g_error ("%s", rtm_error_get_message (error));
200         }
201
202         /* FIXME: work this in where appropriate */
203 #if 0
204         glist = rtm_glib_tasks_get_list (priv->rtm_glib, NULL, NULL, NULL, &error);
205         if (error != NULL) {
206                 g_error ("%s", rtm_error_get_message (error));
207         }
208         for (item = glist; item; item = g_list_next (item)) {
209                 task = (RtmTask *) item->data;
210                 g_print ("%s", rtm_task_to_string (task));
211         }
212         g_list_free (glist);
213
214         glist = rtm_glib_lists_get_list (priv->rtm_glib, &error);
215         if (error != NULL) {
216                 g_error ("%s", rtm_error_get_message (error));
217         }
218         for (item = glist; item; item = g_list_next (item)) {
219                 rtm_list = (RtmList *) item->data;
220                 if (g_strcmp0 (rtm_list_get_name (rtm_list), "Sent") == 0) {
221                         list_id_sent = rtm_list_get_id (rtm_list);
222                 }
223                 g_print ("%s", rtm_list_to_string (rtm_list));
224         }
225         g_list_free (glist);
226
227         timeline = rtm_glib_timelines_create (priv->rtm_glib, &error);
228         if (error != NULL) {
229                 g_error ("%s", rtm_error_get_message (error));
230         }
231         g_print ("timeline: %s", timeline);
232
233         task = rtm_glib_tasks_add (priv->rtm_glib, timeline, "test-rtm-glib", NULL, FALSE, &error);
234         if (error != NULL) {
235                 g_error ("%s", rtm_error_get_message (error));
236         }
237         if (task != NULL) {
238                 g_print ("First task added! task_id: %s\n", rtm_task_get_id (task));
239         } else {
240                 g_print ("First task NOT added!\n");
241         }
242
243 #endif
244 }
245
246
247 static void
248 milk_auth_constructed (GObject *object)
249 {
250         MilkAuthPrivate *priv = MILK_AUTH_PRIVATE (object);
251
252         priv->rtm_glib = rtm_glib_new (priv->api_key, priv->shared_secret);
253 }
254
255 static void
256 milk_auth_finalize (GObject *object)
257 {
258         MilkAuthPrivate *priv = MILK_AUTH_PRIVATE (object);
259
260         g_object_unref (priv->rtm_glib);
261
262         g_free (priv->api_key);
263         g_free (priv->shared_secret);
264 }
265
266 static void
267 milk_auth_class_init (MilkAuthClass *klass)
268 {
269         GObjectClass *object_class = G_OBJECT_CLASS (klass);
270
271         g_type_class_add_private (klass, sizeof (MilkAuthPrivate));
272
273         object_class->get_property = milk_auth_get_property;
274         object_class->set_property = milk_auth_set_property;
275         object_class->constructed = milk_auth_constructed;
276         object_class->finalize = milk_auth_finalize;
277
278         /* FIXME: make these read-only */
279         g_object_class_install_property
280                 (object_class,
281                  PROP_API_KEY,
282                  g_param_spec_string
283                          ("api-key",
284                           "API authentication key",
285                           "Milk's API authentication key.",
286                           RTM_API_KEY,
287                           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
288                           G_PARAM_STATIC_STRINGS));
289
290         g_object_class_install_property
291                 (object_class,
292                  PROP_SHARED_SECRET,
293                  g_param_spec_string
294                          ("shared-secret",
295                           "Shared secret",
296                           "Milk's shared secret with the server.",
297                           RTM_SHARED_SECRET,
298                           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
299                           G_PARAM_STATIC_STRINGS));
300 }
301
302 static void
303 milk_auth_init (MilkAuth *self)
304 {
305         MilkAuthPrivate *priv;
306
307         self->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (
308                         self, MILK_TYPE_AUTH, MilkAuthPrivate);
309 }
310
311 MilkAuth*
312 milk_auth_get_default ()
313 {
314         if (!default_auth)
315                 default_auth = g_object_new (MILK_TYPE_AUTH, NULL);
316
317         return default_auth;
318 }