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_widget_set_halign(label, GTK_ALIGN_START);
79 gtk_widget_set_valign(label, GTK_ALIGN_CENTER);
80 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
81 gtk_label_set_max_width_chars(GTK_LABEL(label), 40);
82 return label;
83}
84
85static void webkitAuthenticationDialogInitialize(WebKitAuthenticationDialog* authDialog)
86{
87 GtkWidget* vBox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 20);
88
89 GtkWidget* box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
90 gtk_style_context_add_class(gtk_widget_get_style_context(box), GTK_STYLE_CLASS_TITLEBAR);
91 gtk_widget_set_size_request(box, -1, 16);
92 GtkWidget* title = gtk_label_new(_("Authentication Required"));
93 gtk_widget_set_margin_top(title, 6);
94 gtk_widget_set_margin_bottom(title, 6);
95 gtk_style_context_add_class(gtk_widget_get_style_context(title), GTK_STYLE_CLASS_TITLE);
96 gtk_box_set_center_widget(GTK_BOX(box), title);
97 gtk_widget_show(title);
98 gtk_box_pack_start(GTK_BOX(vBox), box, TRUE, FALSE, 0);
99 gtk_widget_show(box);
100
101 GtkWidget* buttonBox = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
102 gtk_button_box_set_layout(GTK_BUTTON_BOX(buttonBox), GTK_BUTTONBOX_EXPAND);
103 gtk_widget_set_hexpand(buttonBox, TRUE);
104 gtk_style_context_add_class(gtk_widget_get_style_context(buttonBox), "dialog-action-area");
105
106 GtkWidget* button = gtk_button_new_with_mnemonic(_("_Cancel"));
107 g_signal_connect(button, "clicked", G_CALLBACK(cancelButtonClicked), authDialog);
108 gtk_box_pack_start(GTK_BOX(buttonBox), button, FALSE, TRUE, 0);
109 gtk_widget_show(button);
110
111 WebKitAuthenticationDialogPrivate* priv = authDialog->priv;
112 button = gtk_button_new_with_mnemonic(_("_Authenticate"));
113 priv->defaultButton = button;
114 g_signal_connect(button, "clicked", G_CALLBACK(okButtonClicked), authDialog);
115 gtk_widget_set_can_default(button, TRUE);
116 gtk_box_pack_end(GTK_BOX(buttonBox), button, FALSE, TRUE, 0);
117 gtk_widget_show(button);
118
119 GtkWidget* authBox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
120 gtk_widget_set_margin_start(authBox, 10);
121 gtk_widget_set_margin_end(authBox, 10);
122
123 const WebCore::AuthenticationChallenge& challenge = webkitAuthenticationRequestGetAuthenticationChallenge(priv->request.get())->core();
124 // Prompt on the HTTP authentication dialog.
125 GUniquePtr<char> prompt(g_strdup_printf(_("Authentication required by %s:%i"),
126 challenge.protectionSpace().host().utf8().data(), challenge.protectionSpace().port()));
127 GtkWidget* label = createLabelWithLineWrap(prompt.get());
128 gtk_widget_show(label);
129 gtk_box_pack_start(GTK_BOX(authBox), label, FALSE, FALSE, 0);
130
131 String realm = challenge.protectionSpace().realm();
132 if (!realm.isEmpty()) {
133 // Label on the HTTP authentication dialog. %s is a (probably English) message from the website.
134 GUniquePtr<char> message(g_strdup_printf(_("The site says: ā€œ%sā€"), realm.utf8().data()));
135 label = createLabelWithLineWrap(message.get());
136 gtk_widget_show(label);
137 gtk_box_pack_start(GTK_BOX(authBox), label, FALSE, FALSE, 0);
138 }
139
140 // Check button on the HTTP authentication dialog.
141 priv->rememberCheckButton = gtk_check_button_new_with_mnemonic(_("_Remember password"));
142 gtk_label_set_line_wrap(GTK_LABEL(gtk_bin_get_child(GTK_BIN(priv->rememberCheckButton))), TRUE);
143
144 priv->loginEntry = gtk_entry_new();
145 gtk_widget_set_hexpand(priv->loginEntry, TRUE);
146 gtk_entry_set_activates_default(GTK_ENTRY(priv->loginEntry), TRUE);
147 gtk_widget_show(priv->loginEntry);
148
149 // Entry on the HTTP authentication dialog.
150 GtkWidget* loginLabel = gtk_label_new_with_mnemonic(_("_Username"));
151 gtk_label_set_mnemonic_widget(GTK_LABEL(loginLabel), priv->loginEntry);
152 gtk_widget_set_halign(loginLabel, GTK_ALIGN_END);
153 gtk_style_context_add_class(gtk_widget_get_style_context(loginLabel), GTK_STYLE_CLASS_DIM_LABEL);
154 gtk_widget_show(loginLabel);
155
156 priv->passwordEntry = gtk_entry_new();
157 gtk_widget_set_hexpand(priv->passwordEntry, TRUE);
158 gtk_entry_set_activates_default(GTK_ENTRY(priv->passwordEntry), TRUE);
159 gtk_widget_show(priv->passwordEntry);
160
161 // Entry on the HTTP authentication dialog.
162 GtkWidget* passwordLabel = gtk_label_new_with_mnemonic(_("_Password"));
163 gtk_label_set_mnemonic_widget(GTK_LABEL(passwordLabel), priv->passwordEntry);
164 gtk_widget_set_halign(passwordLabel, GTK_ALIGN_END);
165 gtk_style_context_add_class(gtk_widget_get_style_context(passwordLabel), GTK_STYLE_CLASS_DIM_LABEL);
166 gtk_widget_show(passwordLabel);
167
168 GtkWidget* grid = gtk_grid_new();
169 gtk_grid_set_column_spacing(GTK_GRID(grid), 6);
170 gtk_grid_set_row_spacing(GTK_GRID(grid), 6);
171 gtk_grid_attach(GTK_GRID(grid), loginLabel, 0, 0, 1, 1);
172 gtk_grid_attach(GTK_GRID(grid), priv->loginEntry, 1, 0, 1, 1);
173 gtk_grid_attach(GTK_GRID(grid), passwordLabel, 0, 1, 1, 1);
174 gtk_grid_attach(GTK_GRID(grid), priv->passwordEntry, 1, 1, 1, 1);
175 gtk_grid_attach(GTK_GRID(grid), priv->rememberCheckButton, 1, 2, 1, 1);
176 gtk_widget_show(grid);
177 gtk_box_pack_start(GTK_BOX(authBox), grid, FALSE, FALSE, 0);
178
179 gtk_entry_set_visibility(GTK_ENTRY(priv->passwordEntry), FALSE);
180 gtk_widget_set_visible(priv->rememberCheckButton, priv->credentialStorageMode != DisallowPersistentStorage && !realm.isEmpty());
181
182 const WebCore::Credential& credentialFromPersistentStorage = challenge.proposedCredential();
183 if (!credentialFromPersistentStorage.isEmpty()) {
184 gtk_entry_set_text(GTK_ENTRY(priv->loginEntry), credentialFromPersistentStorage.user().utf8().data());
185 gtk_entry_set_text(GTK_ENTRY(priv->passwordEntry), credentialFromPersistentStorage.password().utf8().data());
186 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->rememberCheckButton), TRUE);
187 }
188
189 gtk_box_pack_start(GTK_BOX(vBox), authBox, TRUE, TRUE, 0);
190 gtk_widget_show(authBox);
191
192 gtk_box_pack_end(GTK_BOX(vBox), buttonBox, FALSE, TRUE, 0);
193 gtk_widget_show(buttonBox);
194
195 gtk_container_add(GTK_CONTAINER(authDialog), vBox);
196 gtk_widget_show(vBox);
197
198 authDialog->priv->authenticationCancelledID = g_signal_connect(authDialog->priv->request.get(), "cancelled", G_CALLBACK(authenticationCancelled), authDialog);
199}
200
201static void webkitAuthenticationDialogMap(GtkWidget* widget)
202{
203 WebKitAuthenticationDialogPrivate* priv = WEBKIT_AUTHENTICATION_DIALOG(widget)->priv;
204 gtk_widget_grab_focus(priv->loginEntry);
205 gtk_widget_grab_default(priv->defaultButton);
206
207 GTK_WIDGET_CLASS(webkit_authentication_dialog_parent_class)->map(widget);
208}
209
210static void webkitAuthenticationDialogDispose(GObject* object)
211{
212 WebKitAuthenticationDialogPrivate* priv = WEBKIT_AUTHENTICATION_DIALOG(object)->priv;
213 if (priv->authenticationCancelledID) {
214 g_signal_handler_disconnect(priv->request.get(), priv->authenticationCancelledID);
215 priv->authenticationCancelledID = 0;
216 }
217
218 G_OBJECT_CLASS(webkit_authentication_dialog_parent_class)->dispose(object);
219}
220
221static void webkit_authentication_dialog_class_init(WebKitAuthenticationDialogClass* klass)
222{
223 GObjectClass* objectClass = G_OBJECT_CLASS(klass);
224 objectClass->dispose = webkitAuthenticationDialogDispose;
225
226 GtkWidgetClass* widgetClass = GTK_WIDGET_CLASS(klass);
227 widgetClass->map = webkitAuthenticationDialogMap;
228}
229
230GtkWidget* webkitAuthenticationDialogNew(WebKitAuthenticationRequest* request, CredentialStorageMode mode)
231{
232 WebKitAuthenticationDialog* authDialog = WEBKIT_AUTHENTICATION_DIALOG(g_object_new(WEBKIT_TYPE_AUTHENTICATION_DIALOG, NULL));
233 authDialog->priv->request = request;
234 authDialog->priv->credentialStorageMode = mode;
235 webkitAuthenticationDialogInitialize(authDialog);
236 return GTK_WIDGET(authDialog);
237}
238