1/*
2 * Copyright (C) 2012 Igalia S.L.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "config.h"
21#include "WebKitAuthenticationDialog.h"
22
23#include "AuthenticationDecisionListener.h"
24#include "WebKitAuthenticationRequestPrivate.h"
25#include "WebKitCredentialPrivate.h"
26#include "WebKitWebView.h"
27#include <glib/gi18n-lib.h>
28#include <wtf/glib/GRefPtr.h>
29#include <wtf/glib/GUniquePtr.h>
30#include <wtf/glib/WTFGType.h>
31#include <wtf/text/CString.h>
32
33using namespace WebKit;
34
35struct _WebKitAuthenticationDialogPrivate {
36 GRefPtr<WebKitAuthenticationRequest> request;
37 CredentialStorageMode credentialStorageMode;
38 GtkWidget* loginEntry;
39 GtkWidget* passwordEntry;
40 GtkWidget* rememberCheckButton;
41 GtkWidget* defaultButton;
42 unsigned long authenticationCancelledID;
43};
44
45WEBKIT_DEFINE_TYPE(WebKitAuthenticationDialog, webkit_authentication_dialog, WEBKIT_TYPE_WEB_VIEW_DIALOG)
46
47static void okButtonClicked(GtkButton*, WebKitAuthenticationDialog* authDialog)
48{
49 WebKitAuthenticationDialogPrivate* priv = authDialog->priv;
50 const char* username = gtk_entry_get_text(GTK_ENTRY(priv->loginEntry));
51 const char* password = gtk_entry_get_text(GTK_ENTRY(priv->passwordEntry));
52 bool rememberPassword = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(priv->rememberCheckButton));
53
54 WebCore::CredentialPersistence persistence = rememberPassword && priv->credentialStorageMode == AllowPersistentStorage ?
55 WebCore::CredentialPersistencePermanent : WebCore::CredentialPersistenceForSession;
56
57 // FIXME: Use a stack allocated WebKitCredential.
58 WebKitCredential* credential = webkitCredentialCreate(WebCore::Credential(String::fromUTF8(username), String::fromUTF8(password), persistence));
59 webkit_authentication_request_authenticate(priv->request.get(), credential);
60 webkit_credential_free(credential);
61 gtk_widget_destroy(GTK_WIDGET(authDialog));
62}
63
64static void cancelButtonClicked(GtkButton*, WebKitAuthenticationDialog* authDialog)
65{
66 webkit_authentication_request_authenticate(authDialog->priv->request.get(), nullptr);
67 gtk_widget_destroy(GTK_WIDGET(authDialog));
68}
69
70static void authenticationCancelled(WebKitAuthenticationRequest*, WebKitAuthenticationDialog* authDialog)
71{
72 gtk_widget_destroy(GTK_WIDGET(authDialog));
73}
74
75static GtkWidget* createLabelWithLineWrap(const char* text)
76{
77 GtkWidget* label = gtk_label_new(text);
78 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
79 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
80 gtk_label_set_max_width_chars(GTK_LABEL(label), 40);
81 return label;
82}
83
84static void webkitAuthenticationDialogInitialize(WebKitAuthenticationDialog* authDialog)
85{
86 GtkWidget* vBox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 20);
87
88 GtkWidget* box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
89 gtk_style_context_add_class(gtk_widget_get_style_context(box), GTK_STYLE_CLASS_TITLEBAR);
90 gtk_widget_set_size_request(box, -1, 16);
91 GtkWidget* title = gtk_label_new(_("Authentication Required"));
92 gtk_widget_set_margin_top(title, 6);
93 gtk_widget_set_margin_bottom(title, 6);
94 gtk_style_context_add_class(gtk_widget_get_style_context(title), GTK_STYLE_CLASS_TITLE);
95 gtk_box_set_center_widget(GTK_BOX(box), title);
96 gtk_widget_show(title);
97 gtk_box_pack_start(GTK_BOX(vBox), box, TRUE, FALSE, 0);
98 gtk_widget_show(box);
99
100 GtkWidget* buttonBox = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
101 gtk_button_box_set_layout(GTK_BUTTON_BOX(buttonBox), GTK_BUTTONBOX_EXPAND);
102 gtk_widget_set_hexpand(buttonBox, TRUE);
103 gtk_style_context_add_class(gtk_widget_get_style_context(buttonBox), "dialog-action-area");
104
105 GtkWidget* button = gtk_button_new_with_mnemonic(_("_Cancel"));
106 g_signal_connect(button, "clicked", G_CALLBACK(cancelButtonClicked), authDialog);
107 gtk_box_pack_start(GTK_BOX(buttonBox), button, FALSE, TRUE, 0);
108 gtk_widget_show(button);
109
110 WebKitAuthenticationDialogPrivate* priv = authDialog->priv;
111 button = gtk_button_new_with_mnemonic(_("_Authenticate"));
112 priv->defaultButton = button;
113 g_signal_connect(button, "clicked", G_CALLBACK(okButtonClicked), authDialog);
114 gtk_widget_set_can_default(button, TRUE);
115 gtk_box_pack_end(GTK_BOX(buttonBox), button, FALSE, TRUE, 0);
116 gtk_widget_show(button);
117
118 GtkWidget* authBox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
119 gtk_widget_set_margin_start(authBox, 10);
120 gtk_widget_set_margin_end(authBox, 10);
121
122 const WebCore::AuthenticationChallenge& challenge = webkitAuthenticationRequestGetAuthenticationChallenge(priv->request.get())->core();
123 // Prompt on the HTTP authentication dialog.
124 GUniquePtr<char> prompt(g_strdup_printf(_("Authentication required by %s:%i"),
125 challenge.protectionSpace().host().utf8().data(), challenge.protectionSpace().port()));
126 GtkWidget* label = createLabelWithLineWrap(prompt.get());
127 gtk_widget_show(label);
128 gtk_box_pack_start(GTK_BOX(authBox), label, FALSE, FALSE, 0);
129
130 String realm = challenge.protectionSpace().realm();
131 if (!realm.isEmpty()) {
132 // Label on the HTTP authentication dialog. %s is a (probably English) message from the website.
133 GUniquePtr<char> message(g_strdup_printf(_("The site says: “%s”"), realm.utf8().data()));
134 label = createLabelWithLineWrap(message.get());
135 gtk_widget_show(label);
136 gtk_box_pack_start(GTK_BOX(authBox), label, FALSE, FALSE, 0);
137 }
138
139 // Check button on the HTTP authentication dialog.
140 priv->rememberCheckButton = gtk_check_button_new_with_mnemonic(_("_Remember password"));
141 gtk_label_set_line_wrap(GTK_LABEL(gtk_bin_get_child(GTK_BIN(priv->rememberCheckButton))), TRUE);
142
143 priv->loginEntry = gtk_entry_new();
144 gtk_widget_set_hexpand(priv->loginEntry, TRUE);
145 gtk_entry_set_activates_default(GTK_ENTRY(priv->loginEntry), TRUE);
146 gtk_widget_show(priv->loginEntry);
147
148 // Entry on the HTTP authentication dialog.
149 GtkWidget* loginLabel = gtk_label_new_with_mnemonic(_("_Username"));
150 gtk_label_set_mnemonic_widget(GTK_LABEL(loginLabel), priv->loginEntry);
151 gtk_widget_set_halign(loginLabel, GTK_ALIGN_END);
152 gtk_style_context_add_class(gtk_widget_get_style_context(loginLabel), GTK_STYLE_CLASS_DIM_LABEL);
153 gtk_widget_show(loginLabel);
154
155 priv->passwordEntry = gtk_entry_new();
156 gtk_widget_set_hexpand(priv->passwordEntry, TRUE);
157 gtk_entry_set_activates_default(GTK_ENTRY(priv->passwordEntry), TRUE);
158 gtk_widget_show(priv->passwordEntry);
159
160 // Entry on the HTTP authentication dialog.
161 GtkWidget* passwordLabel = gtk_label_new_with_mnemonic(_("_Password"));
162 gtk_label_set_mnemonic_widget(GTK_LABEL(passwordLabel), priv->passwordEntry);
163 gtk_widget_set_halign(passwordLabel, GTK_ALIGN_END);
164 gtk_style_context_add_class(gtk_widget_get_style_context(passwordLabel), GTK_STYLE_CLASS_DIM_LABEL);
165 gtk_widget_show(passwordLabel);
166
167 GtkWidget* grid = gtk_grid_new();
168 gtk_grid_set_column_spacing(GTK_GRID(grid), 6);
169 gtk_grid_set_row_spacing(GTK_GRID(grid), 6);
170 gtk_grid_attach(GTK_GRID(grid), loginLabel, 0, 0, 1, 1);
171 gtk_grid_attach(GTK_GRID(grid), priv->loginEntry, 1, 0, 1, 1);
172 gtk_grid_attach(GTK_GRID(grid), passwordLabel, 0, 1, 1, 1);
173 gtk_grid_attach(GTK_GRID(grid), priv->passwordEntry, 1, 1, 1, 1);
174 gtk_grid_attach(GTK_GRID(grid), priv->rememberCheckButton, 1, 2, 1, 1);
175 gtk_widget_show(grid);
176 gtk_box_pack_start(GTK_BOX(authBox), grid, FALSE, FALSE, 0);
177
178 gtk_entry_set_visibility(GTK_ENTRY(priv->passwordEntry), FALSE);
179 gtk_widget_set_visible(priv->rememberCheckButton, priv->credentialStorageMode != DisallowPersistentStorage && !realm.isEmpty());
180
181 const WebCore::Credential& credentialFromPersistentStorage = challenge.proposedCredential();
182 if (!credentialFromPersistentStorage.isEmpty()) {
183 gtk_entry_set_text(GTK_ENTRY(priv->loginEntry), credentialFromPersistentStorage.user().utf8().data());
184 gtk_entry_set_text(GTK_ENTRY(priv->passwordEntry), credentialFromPersistentStorage.password().utf8().data());
185 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->rememberCheckButton), TRUE);
186 }
187
188 gtk_box_pack_start(GTK_BOX(vBox), authBox, TRUE, TRUE, 0);
189 gtk_widget_show(authBox);
190
191 gtk_box_pack_end(GTK_BOX(vBox), buttonBox, FALSE, TRUE, 0);
192 gtk_widget_show(buttonBox);
193
194 gtk_container_add(GTK_CONTAINER(authDialog), vBox);
195 gtk_widget_show(vBox);
196
197 authDialog->priv->authenticationCancelledID = g_signal_connect(authDialog->priv->request.get(), "cancelled", G_CALLBACK(authenticationCancelled), authDialog);
198}
199
200static void webkitAuthenticationDialogMap(GtkWidget* widget)
201{
202 WebKitAuthenticationDialogPrivate* priv = WEBKIT_AUTHENTICATION_DIALOG(widget)->priv;
203 gtk_widget_grab_focus(priv->loginEntry);
204 gtk_widget_grab_default(priv->defaultButton);
205
206 GTK_WIDGET_CLASS(webkit_authentication_dialog_parent_class)->map(widget);
207}
208
209static void webkitAuthenticationDialogDispose(GObject* object)
210{
211 WebKitAuthenticationDialogPrivate* priv = WEBKIT_AUTHENTICATION_DIALOG(object)->priv;
212 if (priv->authenticationCancelledID) {
213 g_signal_handler_disconnect(priv->request.get(), priv->authenticationCancelledID);
214 priv->authenticationCancelledID = 0;
215 }
216
217 G_OBJECT_CLASS(webkit_authentication_dialog_parent_class)->dispose(object);
218}
219
220static void webkit_authentication_dialog_class_init(WebKitAuthenticationDialogClass* klass)
221{
222 GObjectClass* objectClass = G_OBJECT_CLASS(klass);
223 objectClass->dispose = webkitAuthenticationDialogDispose;
224
225 GtkWidgetClass* widgetClass = GTK_WIDGET_CLASS(klass);
226 widgetClass->map = webkitAuthenticationDialogMap;
227}
228
229GtkWidget* webkitAuthenticationDialogNew(WebKitAuthenticationRequest* request, CredentialStorageMode mode)
230{
231 WebKitAuthenticationDialog* authDialog = WEBKIT_AUTHENTICATION_DIALOG(g_object_new(WEBKIT_TYPE_AUTHENTICATION_DIALOG, NULL));
232 authDialog->priv->request = request;
233 authDialog->priv->credentialStorageMode = mode;
234 webkitAuthenticationDialogInitialize(authDialog);
235 return GTK_WIDGET(authDialog);
236}
237