2 Copyright (C) 2009 Igalia S.L.
3 Copyright (C) 2011 Samsung Electronics
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 #include "ewk_auth_soup.h"
26 #include "ewk_logging.h"
27 #include <glib-object.h>
29 #include <libsoup/soup.h>
36 * #Ewk_Soup_Auth_Dialog is a #SoupSessionFeature that you can attach to your
37 * #SoupSession to provide a simple authentication dialog while
38 * handling HTTP basic auth. It is built as a simple C-only module
42 typedef struct _Ewk_Auth_Data {
48 static Ewk_Auth_Show_Dialog_Callback ewk_auth_show_dialog_callback = 0;
50 static void ewk_auth_soup_dialog_session_feature_init(SoupSessionFeatureInterface*, gpointer);
51 static void attach(SoupSessionFeature*, SoupSession*);
52 static void detach(SoupSessionFeature*, SoupSession*);
53 static void free_auth_data(Ewk_Auth_Data*);
55 G_DEFINE_TYPE_WITH_CODE(Ewk_Soup_Auth_Dialog, ewk_auth_soup_dialog, G_TYPE_OBJECT,
56 G_IMPLEMENT_INTERFACE(SOUP_TYPE_SESSION_FEATURE, ewk_auth_soup_dialog_session_feature_init))
58 static void ewk_auth_soup_dialog_class_init(Ewk_Soup_Auth_DialogClass *klass)
62 static void ewk_auth_soup_dialog_init(Ewk_Soup_Auth_Dialog *instance)
66 static void ewk_auth_soup_dialog_session_feature_init(SoupSessionFeatureInterface *feature_interface, gpointer interface_data)
68 feature_interface->attach = attach;
69 feature_interface->detach = detach;
72 void ewk_auth_soup_show_dialog_callback_set(Ewk_Auth_Show_Dialog_Callback callback)
74 ewk_auth_show_dialog_callback = callback;
77 void ewk_auth_soup_credentials_set(const char *username, const char *password, void *data)
82 Ewk_Auth_Data *auth_data = (Ewk_Auth_Data *)data;
83 soup_auth_authenticate(auth_data->auth, username, password);
84 soup_session_unpause_message(auth_data->session, auth_data->msg);
85 free_auth_data(auth_data);
88 static void session_authenticate(SoupSession *session, SoupMessage *msg, SoupAuth *auth, gboolean retrying, gpointer /* user_data */)
91 Ewk_Auth_Data *auth_data;
94 if (!ewk_auth_show_dialog_callback)
97 auth_data = (Ewk_Auth_Data *)calloc(1, sizeof(Ewk_Auth_Data));
100 CRITICAL("could not allocate Ewk_Auth_Data");
104 soup_session_pause_message(session, msg);
105 // We need to make sure the message sticks around when pausing it.
107 g_object_ref(session);
110 auth_data->msg = msg;
111 auth_data->auth = auth;
112 auth_data->session = session;
114 uri = soup_message_get_uri(auth_data->msg);
115 realm = soup_auth_get_realm(auth_data->auth);
117 // Call application method responsible for showing authentication dialog and sending back authorization data.
118 ewk_auth_show_dialog_callback(realm, soup_uri_to_string(uri, TRUE), auth_data);
121 static void free_auth_data(Ewk_Auth_Data *auth_data)
123 g_object_unref(auth_data->msg);
124 g_object_unref(auth_data->session);
125 g_object_unref(auth_data->auth);
129 static void attach(SoupSessionFeature *manager, SoupSession *session)
131 g_signal_connect(session, "authenticate", G_CALLBACK(session_authenticate), manager);
134 static void detach(SoupSessionFeature *manager, SoupSession *session)
136 g_signal_handlers_disconnect_by_func(session, (void *)session_authenticate, manager);