Changeset 153882 in webkit


Ignore:
Timestamp:
Aug 9, 2013 3:48:49 AM (11 years ago)
Author:
commit-queue@webkit.org
Message:

[GTK] [WebKit2] Add an 'authenticate' signal to WebKitWebView
https://bugs.webkit.org/show_bug.cgi?id=99352

Patch by Brian Holt <brian.holt@samsung.com> on 2013-08-09
Reviewed by Carlos Garcia Campos.

Add a new public class to the API, WebKitAuthenticationRequest, to be
emitted along with a new WebKitWebView::authenticate signal to
let client applications to provide their own authentication
when the user is challenged with HTTP authentication. The
credential is exposed through a new class WebKitCredential.

  • GNUmakefile.list.am:
  • UIProcess/API/gtk/WebKitAuthenticationDialog.cpp:

(okButtonClicked): Use new API for authentication.
(cancelButtonClicked): Ditto.
(webkitAuthenticationDialogInitialize):Get
authenticationChallenge from request object.
(webkitAuthenticationDialogNew): Ditto.

  • UIProcess/API/gtk/WebKitAuthenticationDialog.h:
  • UIProcess/API/gtk/WebKitAuthenticationRequest.cpp: Added new

class representing a HTTP authentication request.
(webkitAuthenticationRequestDispose):
(webkit_authentication_request_class_init):
(webkitAuthenticationRequestCreate):
(webkitAuthenticationRequestGetAuthenticationChallenge):
(webkit_authentication_request_can_save_credentials):
(webkit_authentication_request_get_proposed_credential):
(webkit_authentication_request_get_host):
(webkit_authentication_request_get_port):
(webkit_authentication_request_get_realm):
(webkit_authentication_request_get_scheme):
(webkit_authentication_request_is_for_proxy):
(webkit_authentication_request_is_retry):
(webkit_authentication_request_authenticate):
(webkit_authentication_request_cancel):

  • UIProcess/API/gtk/WebKitAuthenticationRequest.h: Added.
  • UIProcess/API/gtk/WebKitAuthenticationRequestPrivate.h: Added.
  • UIProcess/API/gtk/WebKitCredential.cpp: Added new class to

support credentials for HTTP authentication.
(_WebKitCredential::_WebKitCredential):
(webkitCredentialCreate):
(webkitCredentialGetCredential):
(webkit_credential_new):
(webkit_credential_copy):
(webkit_credential_free):
(webkit_credential_get_username):
(webkit_credential_get_password):
(webkit_credential_has_password):
(webkit_credential_get_persistence):

  • UIProcess/API/gtk/WebKitCredential.h: Added.
  • UIProcess/API/gtk/WebKitCredentialPrivate.h: Added.
  • UIProcess/API/gtk/WebKitWebView.cpp: Added authenticate signal.

(webkitWebViewAuthenticate): Default signal handler for authenticate.
(webkit_web_view_class_init): Ditto.
(webkitWebViewHandleAuthenticationChallenge): Create an
authentication request and emit it with authenticate signal.

  • UIProcess/API/gtk/WebKitWebView.h: Replaced reserved function

pointer slot with for WebViewClass with function pointer to
authenticate signal handler.

  • UIProcess/API/gtk/docs/webkit2gtk-docs.sgml: Added new section for

the authentication request.

  • UIProcess/API/gtk/docs/webkit2gtk-sections.txt: Ditto.
  • UIProcess/API/gtk/docs/webkit2gtk.types: Added authentication

request and credential types.

  • UIProcess/API/gtk/tests/TestWebKitWebView.cpp: Created a new

unit test for HTTP authentication. A libsoup server callback
handler is installed that creates soup messages with appropriate
headers and body to test HTTP authentication.
(testWebViewAuthenticationRequest): Test the authentication
request and credential API.
(testWebViewAuthenticationCancel): Test cancelled authentication.
(testWebViewAuthenticationFailure): Test repeated authentication failures.
(testWebViewAuthenticationNoCredential): Test continue without
credentials.
(testWebViewAuthenticationStorage): Test credential storage.
(testWebViewAuthenticationSuccess): Test successful authentication.
(beforeAll): Added tests.

  • UIProcess/API/gtk/webkit2.h: Added WebKitAuthenticationRequest and

WebKitCredential headers.

Location:
trunk/Source/WebKit2
Files:
6 added
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit2/ChangeLog

    r153879 r153882  
     12013-08-09  Brian Holt  <brian.holt@samsung.com>
     2
     3        [GTK] [WebKit2] Add an 'authenticate' signal to WebKitWebView
     4        https://bugs.webkit.org/show_bug.cgi?id=99352
     5
     6        Reviewed by Carlos Garcia Campos.
     7
     8        Add a new public class to the API, WebKitAuthenticationRequest, to be
     9        emitted along with a new WebKitWebView::authenticate signal to
     10        let client applications to provide their own authentication
     11        when the user is challenged with HTTP authentication. The
     12        credential is exposed through a new class WebKitCredential.
     13
     14        * GNUmakefile.list.am:
     15        * UIProcess/API/gtk/WebKitAuthenticationDialog.cpp:
     16        (okButtonClicked): Use new API for authentication.
     17        (cancelButtonClicked): Ditto.
     18        (webkitAuthenticationDialogInitialize):Get
     19        authenticationChallenge from request object.
     20        (webkitAuthenticationDialogNew): Ditto.
     21        * UIProcess/API/gtk/WebKitAuthenticationDialog.h:
     22        * UIProcess/API/gtk/WebKitAuthenticationRequest.cpp: Added new
     23        class representing a HTTP authentication request.
     24        (webkitAuthenticationRequestDispose):
     25        (webkit_authentication_request_class_init):
     26        (webkitAuthenticationRequestCreate):
     27        (webkitAuthenticationRequestGetAuthenticationChallenge):
     28        (webkit_authentication_request_can_save_credentials):
     29        (webkit_authentication_request_get_proposed_credential):
     30        (webkit_authentication_request_get_host):
     31        (webkit_authentication_request_get_port):
     32        (webkit_authentication_request_get_realm):
     33        (webkit_authentication_request_get_scheme):
     34        (webkit_authentication_request_is_for_proxy):
     35        (webkit_authentication_request_is_retry):
     36        (webkit_authentication_request_authenticate):
     37        (webkit_authentication_request_cancel):
     38        * UIProcess/API/gtk/WebKitAuthenticationRequest.h: Added.
     39        * UIProcess/API/gtk/WebKitAuthenticationRequestPrivate.h: Added.
     40        * UIProcess/API/gtk/WebKitCredential.cpp: Added new class to
     41        support credentials for HTTP authentication.
     42        (_WebKitCredential::_WebKitCredential):
     43        (webkitCredentialCreate):
     44        (webkitCredentialGetCredential):
     45        (webkit_credential_new):
     46        (webkit_credential_copy):
     47        (webkit_credential_free):
     48        (webkit_credential_get_username):
     49        (webkit_credential_get_password):
     50        (webkit_credential_has_password):
     51        (webkit_credential_get_persistence):
     52        * UIProcess/API/gtk/WebKitCredential.h: Added.
     53        * UIProcess/API/gtk/WebKitCredentialPrivate.h: Added.
     54        * UIProcess/API/gtk/WebKitWebView.cpp: Added authenticate signal.
     55        (webkitWebViewAuthenticate): Default signal handler for authenticate.
     56        (webkit_web_view_class_init): Ditto.
     57        (webkitWebViewHandleAuthenticationChallenge): Create an
     58        authentication request and emit it with authenticate signal.
     59        * UIProcess/API/gtk/WebKitWebView.h: Replaced reserved function
     60        pointer slot with for WebViewClass with function pointer to
     61        authenticate signal handler.
     62        * UIProcess/API/gtk/docs/webkit2gtk-docs.sgml: Added new section for
     63        the authentication request.
     64        * UIProcess/API/gtk/docs/webkit2gtk-sections.txt: Ditto.
     65        * UIProcess/API/gtk/docs/webkit2gtk.types: Added authentication
     66        request and credential types.
     67        * UIProcess/API/gtk/tests/TestWebKitWebView.cpp: Created a new
     68        unit test for HTTP authentication. A libsoup server callback
     69        handler is installed that creates soup messages with appropriate
     70        headers and body to test HTTP authentication.
     71        (testWebViewAuthenticationRequest): Test the authentication
     72        request and credential API.
     73        (testWebViewAuthenticationCancel): Test cancelled authentication.
     74        (testWebViewAuthenticationFailure): Test repeated authentication failures.
     75        (testWebViewAuthenticationNoCredential): Test continue without
     76        credentials.
     77        (testWebViewAuthenticationStorage): Test credential storage.
     78        (testWebViewAuthenticationSuccess): Test successful authentication.
     79        (beforeAll): Added tests.
     80        * UIProcess/API/gtk/webkit2.h: Added WebKitAuthenticationRequest and
     81        WebKitCredential headers.
     82
    1832013-08-09  Tim Horton  <timothy_horton@apple.com>
    284
  • trunk/Source/WebKit2/GNUmakefile.list.am

    r153460 r153882  
    9595
    9696webkit2gtk_h_api += \
     97        $(WebKit2)/UIProcess/API/gtk/WebKitAuthenticationRequest.h \
    9798        $(WebKit2)/UIProcess/API/gtk/WebKitBackForwardList.h \
    9899        $(WebKit2)/UIProcess/API/gtk/WebKitBackForwardListItem.h \
     
    101102        $(WebKit2)/UIProcess/API/gtk/WebKitContextMenuItem.h \
    102103        $(WebKit2)/UIProcess/API/gtk/WebKitCookieManager.h \
     104        $(WebKit2)/UIProcess/API/gtk/WebKitCredential.h \
    103105        $(WebKit2)/UIProcess/API/gtk/WebKitDefines.h \
    104106        $(WebKit2)/UIProcess/API/gtk/WebKitDownload.h \
     
    654656        Source/WebKit2/UIProcess/API/gtk/WebKitAuthenticationDialog.cpp \
    655657        Source/WebKit2/UIProcess/API/gtk/WebKitAuthenticationDialog.h \
     658        Source/WebKit2/UIProcess/API/gtk/WebKitAuthenticationRequest.cpp \
     659        Source/WebKit2/UIProcess/API/gtk/WebKitAuthenticationRequest.h \
     660        Source/WebKit2/UIProcess/API/gtk/WebKitAuthenticationRequestPrivate.h \
    656661        Source/WebKit2/UIProcess/API/gtk/WebKitBackForwardList.h \
    657662        Source/WebKit2/UIProcess/API/gtk/WebKitBackForwardList.cpp \
     
    673678        Source/WebKit2/UIProcess/API/gtk/WebKitCookieManager.cpp \
    674679        Source/WebKit2/UIProcess/API/gtk/WebKitCookieManagerPrivate.h \
     680        Source/WebKit2/UIProcess/API/gtk/WebKitCredential.cpp \
     681        Source/WebKit2/UIProcess/API/gtk/WebKitCredential.h \
     682        Source/WebKit2/UIProcess/API/gtk/WebKitCredentialPrivate.h \
    675683        Source/WebKit2/UIProcess/API/gtk/WebKitDefines.h \
    676684        Source/WebKit2/UIProcess/API/gtk/WebKitDownload.cpp \
  • trunk/Source/WebKit2/UIProcess/API/gtk/WebKitAuthenticationDialog.cpp

    r148294 r153882  
    2222
    2323#include "AuthenticationDecisionListener.h"
    24 #include "WebCredential.h"
     24#include "WebKitAuthenticationRequestPrivate.h"
     25#include "WebKitCredentialPrivate.h"
    2526#include "WebKitPrivate.h"
    2627
     
    2829
    2930struct _WebKitAuthenticationDialogPrivate {
    30     RefPtr<AuthenticationChallengeProxy> authenticationChallenge;
    31 
     31    GRefPtr<WebKitAuthenticationRequest> request;
    3232    GtkWidget* authWidget;
    3333    GtkWidget* defaultButton;
     
    3737WEBKIT_DEFINE_TYPE(WebKitAuthenticationDialog, webkit_authentication_dialog, GTK_TYPE_EVENT_BOX)
    3838
    39 static void webkitAuthenticationDialogAuthenticate(WebKitAuthenticationDialog* authDialog, WebCredential* credential)
    40 {
    41     WebKitAuthenticationDialogPrivate* priv = authDialog->priv;
    42     priv->authenticationChallenge->listener()->useCredential(credential);
    43     gtk_widget_destroy(GTK_WIDGET(authDialog));
    44 }
    45 
    4639static void okButtonClicked(GtkButton*, WebKitAuthenticationDialog* authDialog)
    4740{
    4841    WebKitAuthenticationDialogPrivate* priv = authDialog->priv;
    49     RefPtr<WebCredential> webCredential = WebCredential::create(webkitAuthenticationWidgetCreateCredential(WEBKIT_AUTHENTICATION_WIDGET(priv->authWidget)));
    50     webkitAuthenticationDialogAuthenticate(authDialog, webCredential.get());
     42    WebKitCredential* credential = webkitCredentialCreate(webkitAuthenticationWidgetCreateCredential(WEBKIT_AUTHENTICATION_WIDGET(priv->authWidget)));
     43    webkit_authentication_request_authenticate(priv->request.get(), credential);
     44    webkit_credential_free(credential);
     45    gtk_widget_destroy(GTK_WIDGET(authDialog));
    5146}
    5247
    5348static void cancelButtonClicked(GtkButton*, WebKitAuthenticationDialog* authDialog)
    5449{
    55     webkitAuthenticationDialogAuthenticate(authDialog, 0);
     50    webkit_authentication_request_authenticate(authDialog->priv->request.get(), 0);
     51    gtk_widget_destroy(GTK_WIDGET(authDialog));
    5652}
    5753
     
    8177    gtk_widget_show(button);
    8278
    83     authDialog->priv->authWidget = webkitAuthenticationWidgetNew(authDialog->priv->authenticationChallenge->core(), credentialStorageMode);
     79    authDialog->priv->authWidget = webkitAuthenticationWidgetNew(webkitAuthenticationRequestGetAuthenticationChallenge(authDialog->priv->request.get())->core(), credentialStorageMode);
    8480    gtk_box_pack_start(GTK_BOX(vBox), authDialog->priv->authWidget, TRUE, TRUE, 0);
    8581    gtk_widget_show(authDialog->priv->authWidget);
     
    139135}
    140136
    141 GtkWidget* webkitAuthenticationDialogNew(AuthenticationChallengeProxy* authenticationChallenge, CredentialStorageMode mode)
     137GtkWidget* webkitAuthenticationDialogNew(WebKitAuthenticationRequest* request, CredentialStorageMode mode)
    142138{
    143139    WebKitAuthenticationDialog* authDialog = WEBKIT_AUTHENTICATION_DIALOG(g_object_new(WEBKIT_TYPE_AUTHENTICATION_DIALOG, NULL));
    144     authDialog->priv->authenticationChallenge = authenticationChallenge;
     140    authDialog->priv->request = request;
    145141    webkitAuthenticationDialogInitialize(authDialog, mode);
    146142    return GTK_WIDGET(authDialog);
  • trunk/Source/WebKit2/UIProcess/API/gtk/WebKitAuthenticationDialog.h

    r148294 r153882  
    2121#define WebKitAuthenticationDialog_h
    2222
    23 #include "AuthenticationChallengeProxy.h"
     23#include "WebKitAuthenticationRequest.h"
    2424#include "WebKitAuthenticationWidget.h"
    2525#include <gtk/gtk.h>
     
    4949
    5050GType webkit_authentication_dialog_get_type();
    51 GtkWidget* webkitAuthenticationDialogNew(WebKit::AuthenticationChallengeProxy*, CredentialStorageMode);
     51GtkWidget* webkitAuthenticationDialogNew(WebKitAuthenticationRequest*, CredentialStorageMode);
    5252
    5353G_END_DECLS
  • trunk/Source/WebKit2/UIProcess/API/gtk/WebKitWebView.cpp

    r152354 r153882  
    2929#include "WebData.h"
    3030#include "WebKitAuthenticationDialog.h"
     31#include "WebKitAuthenticationRequestPrivate.h"
    3132#include "WebKitBackForwardListPrivate.h"
    3233#include "WebKitContextMenuClient.h"
     
    120121    WEB_PROCESS_CRASHED,
    121122
     123    AUTHENTICATE,
     124
    122125    LAST_SIGNAL
    123126};
     
    428431        g_signal_handler_disconnect(webkit_web_context_get_favicon_database(priv->context), priv->faviconChangedHandlerID);
    429432    priv->faviconChangedHandlerID = 0;
     433}
     434
     435static gboolean webkitWebViewAuthenticate(WebKitWebView* webView, WebKitAuthenticationRequest* request)
     436{
     437    CredentialStorageMode credentialStorageMode = webkit_authentication_request_can_save_credentials(request) ? AllowPersistentStorage : DisallowPersistentStorage;
     438    webkitWebViewBaseAddAuthenticationDialog(WEBKIT_WEB_VIEW_BASE(webView), webkitAuthenticationDialogNew(request, credentialStorageMode));
     439
     440    return TRUE;
    430441}
    431442
     
    606617    webViewClass->permission_request = webkitWebViewPermissionRequest;
    607618    webViewClass->run_file_chooser = webkitWebViewRunFileChooser;
     619    webViewClass->authenticate = webkitWebViewAuthenticate;
    608620
    609621    /**
     
    13671379        webkit_marshal_BOOLEAN__VOID,
    13681380        G_TYPE_BOOLEAN, 0);
     1381
     1382    /**
     1383     * WebKitWebView::authenticate:
     1384     * @web_view: the #WebKitWebView on which the signal is emitted
     1385     * @request: a #WebKitAuthenticationRequest
     1386     *
     1387     * This signal is emitted when the user is challenged with HTTP
     1388     * authentication. To let the  application access or supply
     1389     * the credentials as well as to allow the client application
     1390     * to either cancel the request or perform the authentication,
     1391     * the signal will pass an instance of the
     1392     * #WebKitAuthenticationRequest in the @request argument.
     1393     * To handle this signal asynchronously you should keep a ref
     1394     * of the request and return %TRUE. To disable HTTP authentication
     1395     * entirely, connect to this signal and simply return %TRUE.
     1396     *
     1397     * The default signal handler will run a default authentication
     1398     * dialog asynchronously for the user to interact with.
     1399     *
     1400     * Returns: %TRUE to stop other handlers from being invoked for the event.
     1401     *   %FALSE to propagate the event further.
     1402     *
     1403     * Since: 2.2
     1404     */
     1405    signals[AUTHENTICATE] =
     1406        g_signal_new("authenticate",
     1407            G_TYPE_FROM_CLASS(webViewClass),
     1408            G_SIGNAL_RUN_LAST,
     1409            G_STRUCT_OFFSET(WebKitWebViewClass, authenticate),
     1410            g_signal_accumulator_true_handled, 0 /* accumulator data */,
     1411            webkit_marshal_BOOLEAN__OBJECT,
     1412            G_TYPE_BOOLEAN, 1, /* number of parameters */
     1413            WEBKIT_TYPE_AUTHENTICATION_REQUEST);
    13691414}
    13701415
     
    17391784void webkitWebViewHandleAuthenticationChallenge(WebKitWebView* webView, AuthenticationChallengeProxy* authenticationChallenge)
    17401785{
    1741     CredentialStorageMode credentialStorageMode;
    1742     if (webkit_settings_get_enable_private_browsing(webkit_web_view_get_settings(webView)))
    1743         credentialStorageMode = DisallowPersistentStorage;
    1744     else
    1745         credentialStorageMode = AllowPersistentStorage;
    1746 
    1747     webkitWebViewBaseAddAuthenticationDialog(WEBKIT_WEB_VIEW_BASE(webView), webkitAuthenticationDialogNew(authenticationChallenge, credentialStorageMode));
     1786    gboolean privateBrowsingEnabled = webkit_settings_get_enable_private_browsing(webkit_web_view_get_settings(webView));
     1787    GRefPtr<WebKitAuthenticationRequest> request = adoptGRef(webkitAuthenticationRequestCreate(authenticationChallenge, privateBrowsingEnabled));
     1788    gboolean returnValue;
     1789    g_signal_emit(webView, signals[AUTHENTICATE], 0, request.get(), &returnValue);
    17481790}
    17491791
  • trunk/Source/WebKit2/UIProcess/API/gtk/WebKitWebView.h

    r150130 r153882  
    3030
    3131#include <JavaScriptCore/JSBase.h>
     32#include <webkit2/WebKitAuthenticationRequest.h>
    3233#include <webkit2/WebKitBackForwardList.h>
    3334#include <webkit2/WebKitDefines.h>
     
    243244    gboolean   (* web_process_crashed)       (WebKitWebView               *web_view);
    244245
     246    gboolean   (* authenticate)              (WebKitWebView               *web_view,
     247                                              WebKitAuthenticationRequest *request);
    245248    void (*_webkit_reserved0) (void);
    246249    void (*_webkit_reserved1) (void);
     
    250253    void (*_webkit_reserved5) (void);
    251254    void (*_webkit_reserved6) (void);
    252     void (*_webkit_reserved7) (void);
    253255};
    254256
  • trunk/Source/WebKit2/UIProcess/API/gtk/docs/webkit2gtk-docs.sgml

    r149117 r153882  
    1414    <xi:include href="xml/WebKitWebContext.xml"/>
    1515    <xi:include href="xml/WebKitWebView.xml"/>
     16    <xi:include href="xml/WebKitAuthenticationRequest.xml"/>
    1617    <xi:include href="xml/WebKitBackForwardList.xml"/>
    1718    <xi:include href="xml/WebKitBackForwardListItem.xml"/>
     
    5455    <title>Index</title>
    5556  </index>
     57 
     58  <index id="api-index-2-2" role="2.2">
     59    <title>Index of new symbols in 2.2</title>
     60    <xi:include href="xml/api-index-2.2.xml"><xi:fallback /></xi:include>
     61  </index> 
     62 
    5663  <xi:include href="xml/annotation-glossary.xml"><xi:fallback /></xi:include>
    5764</book>
  • trunk/Source/WebKit2/UIProcess/API/gtk/docs/webkit2gtk-sections.txt

    r150158 r153882  
    176176webkit_script_dialog_get_type
    177177WebKitWebViewPrivate
     178</SECTION>
     179
     180<SECTION>
     181<FILE>WebKitAuthenticationRequest</FILE>
     182WebKitAuthenticationRequest
     183WebKitAuthenticationScheme
     184webkit_authentication_request_authenticate
     185webkit_authentication_request_cancel
     186webkit_authentication_request_can_save_credentials
     187webkit_authentication_request_get_host
     188webkit_authentication_request_get_port
     189webkit_authentication_request_is_retry
     190webkit_authentication_request_get_proposed_credential
     191webkit_authentication_request_get_realm
     192webkit_authentication_request_get_scheme
     193webkit_authentication_request_is_for_proxy
     194
     195<SUBSECTION Standard>
     196WebKitAuthenticationRequestClass
     197WEBKIT_TYPE_AUTHENTICATION_REQUEST
     198WEBKIT_AUTHENTICATION_REQUEST
     199WEBKIT_AUTHENTICATION_REQUEST_CLASS
     200WEBKIT_AUTHENTICATION_REQUEST_GET_CLASS
     201WEBKIT_IS_AUTHENTICATION_REQUEST
     202WEBKIT_IS_AUTHENTICATION_REQUEST_CLASS
     203WEBKIT_TYPE_CREDENTIAL
     204
     205<SUBSECTION Private>
     206WebKitAuthenticationRequestPrivate
     207webkit_authentication_request_get_type
     208webkit_credential_get_type
     209
     210<SUBSECTION>
     211WebKitCredential
     212WebKitCredentialPersistence
     213webkit_credential_new
     214webkit_credential_copy
     215webkit_credential_free
     216webkit_credential_get_password
     217webkit_credential_get_persistence
     218webkit_credential_get_username
     219webkit_credential_has_password
    178220</SECTION>
    179221
  • trunk/Source/WebKit2/UIProcess/API/gtk/docs/webkit2gtk.types

    r149117 r153882  
    2525webkit_web_extension_get_type
    2626webkit_web_page_get_type
     27webkit_authentication_request_get_type
     28webkit_credential_get_type
  • trunk/Source/WebKit2/UIProcess/API/gtk/tests/TestWebKitWebView.cpp

    r151833 r153882  
    1919
    2020#include "config.h"
     21#include "LoadTrackingTest.h"
     22#include "WebKitTestServer.h"
    2123#include "WebViewTest.h"
    2224#include <JavaScriptCore/JSStringRef.h>
     
    2628#include <wtf/gobject/GRefPtr.h>
    2729#include <wtf/text/StringHash.h>
     30
     31static WebKitTestServer* kServer;
    2832
    2933static void testWebViewDefaultContext(WebViewTest* test, gconstpointer)
     
    12301234}
    12311235
     1236class AuthenticationTest: public LoadTrackingTest {
     1237public:
     1238    MAKE_GLIB_TEST_FIXTURE(AuthenticationTest);
     1239
     1240    AuthenticationTest()
     1241    {
     1242        g_signal_connect(m_webView, "authenticate", G_CALLBACK(runAuthenticationCallback), this);
     1243    }
     1244
     1245    ~AuthenticationTest()
     1246    {
     1247        g_signal_handlers_disconnect_matched(m_webView, G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, this);
     1248    }
     1249
     1250    static int authenticationRetries;
     1251
     1252    void loadURI(const char* uri)
     1253    {
     1254        // Reset the retry count of the fake server when a page is loaded.
     1255        authenticationRetries = 0;
     1256        LoadTrackingTest::loadURI(uri);
     1257    }
     1258
     1259    static gboolean runAuthenticationCallback(WebKitWebView*, WebKitAuthenticationRequest* request, AuthenticationTest* test)
     1260    {
     1261        test->runAuthentication(request);
     1262        return TRUE;
     1263    }
     1264
     1265    void runAuthentication(WebKitAuthenticationRequest* request)
     1266    {
     1267        assertObjectIsDeletedWhenTestFinishes(G_OBJECT(request));
     1268        m_authenticationRequest = request;
     1269        g_main_loop_quit(m_mainLoop);
     1270    }
     1271
     1272    WebKitAuthenticationRequest* waitForAuthenticationRequest()
     1273    {
     1274        g_main_loop_run(m_mainLoop);
     1275        return m_authenticationRequest.get();
     1276    }
     1277
     1278private:
     1279    GRefPtr<WebKitAuthenticationRequest> m_authenticationRequest;
     1280};
     1281
     1282int AuthenticationTest::authenticationRetries = 0;
     1283
     1284static const char authTestUsername[] = "username";
     1285static const char authTestPassword[] = "password";
     1286static const char authExpectedSuccessTitle[] = "WebKit2Gtk+ Authentication test";
     1287static const char authExpectedFailureTitle[] = "401 Authorization Required";
     1288static const char authExpectedAuthorization[] = "Basic dXNlcm5hbWU6cGFzc3dvcmQ="; // Base64 encoding of "username:password".
     1289static const char authSuccessHTMLString[] =
     1290    "<html>"
     1291    "<head><title>WebKit2Gtk+ Authentication test</title></head>"
     1292    "<body></body></html>";
     1293static const char authFailureHTMLString[] =
     1294    "<html>"
     1295    "<head><title>401 Authorization Required</title></head>"
     1296    "<body></body></html>";
     1297
     1298static void testWebViewAuthenticationRequest(AuthenticationTest* test, gconstpointer)
     1299{
     1300    // Test authentication request getters match soup authentication header.
     1301    test->loadURI(kServer->getURIForPath("/auth-test.html").data());
     1302    WebKitAuthenticationRequest* request = test->waitForAuthenticationRequest();
     1303    g_assert_cmpstr(webkit_authentication_request_get_host(request), ==, soup_uri_get_host(kServer->baseURI()));
     1304    g_assert_cmpuint(webkit_authentication_request_get_port(request), ==, soup_uri_get_port(kServer->baseURI()));
     1305    g_assert_cmpstr(webkit_authentication_request_get_realm(request), ==, "my realm");
     1306    g_assert(webkit_authentication_request_get_scheme(request) == WEBKIT_AUTHENTICATION_SCHEME_HTTP_BASIC);
     1307    g_assert(!webkit_authentication_request_is_for_proxy(request));
     1308    g_assert(!webkit_authentication_request_is_retry(request));
     1309}
     1310
     1311static void testWebViewAuthenticationCancel(AuthenticationTest* test, gconstpointer)
     1312{
     1313    // Test cancel.
     1314    test->loadURI(kServer->getURIForPath("/auth-test.html").data());
     1315    WebKitAuthenticationRequest* request = test->waitForAuthenticationRequest();
     1316    webkit_authentication_request_cancel(request);
     1317    // Server doesn't ask for new credentials.
     1318    test->waitUntilLoadFinished();
     1319
     1320    g_assert_cmpint(test->m_loadEvents.size(), ==, 3);
     1321    g_assert_cmpint(test->m_loadEvents[0], ==, LoadTrackingTest::ProvisionalLoadStarted);
     1322    g_assert_cmpint(test->m_loadEvents[1], ==, LoadTrackingTest::ProvisionalLoadFailed);
     1323    g_assert_cmpint(test->m_loadEvents[2], ==, LoadTrackingTest::LoadFinished);
     1324
     1325    g_assert_error(test->m_error.get(), WEBKIT_NETWORK_ERROR, WEBKIT_NETWORK_ERROR_CANCELLED);
     1326}
     1327
     1328static void testWebViewAuthenticationFailure(AuthenticationTest* test, gconstpointer)
     1329{
     1330    // Test authentication failures.
     1331    test->loadURI(kServer->getURIForPath("/auth-test.html").data());
     1332    WebKitAuthenticationRequest* request = test->waitForAuthenticationRequest();
     1333    g_assert(!webkit_authentication_request_is_retry(request));
     1334    WebKitCredential* credential = webkit_credential_new(authTestUsername, "wrongpassword", WEBKIT_CREDENTIAL_PERSISTENCE_NONE);
     1335    webkit_authentication_request_authenticate(request, credential);
     1336    webkit_credential_free(credential);
     1337    // Expect a second authentication request.
     1338    request = test->waitForAuthenticationRequest();
     1339    g_assert(webkit_authentication_request_is_retry(request));
     1340    // Test second failure.
     1341    credential = webkit_credential_new(authTestUsername, "wrongpassword2", WEBKIT_CREDENTIAL_PERSISTENCE_NONE);
     1342    webkit_authentication_request_authenticate(request, credential);
     1343    webkit_credential_free(credential);
     1344    // Expect authentication failed page.
     1345    test->waitUntilLoadFinished();
     1346
     1347    g_assert_cmpint(test->m_loadEvents.size(), ==, 3);
     1348    g_assert_cmpint(test->m_loadEvents[0], ==, LoadTrackingTest::ProvisionalLoadStarted);
     1349    g_assert_cmpint(test->m_loadEvents[1], ==, LoadTrackingTest::LoadCommitted);
     1350    g_assert_cmpint(test->m_loadEvents[2], ==, LoadTrackingTest::LoadFinished);
     1351    g_assert_cmpstr(webkit_web_view_get_title(test->m_webView), ==, authExpectedFailureTitle);
     1352}
     1353
     1354static void testWebViewAuthenticationNoCredential(AuthenticationTest* test, gconstpointer)
     1355{
     1356    // Test continue without credentials.
     1357    test->loadURI(kServer->getURIForPath("/auth-test.html").data());
     1358    WebKitAuthenticationRequest* request = test->waitForAuthenticationRequest();
     1359    webkit_authentication_request_authenticate(request, 0);
     1360    // Server doesn't ask for new credentials.
     1361    test->waitUntilLoadFinished();
     1362
     1363    g_assert_cmpint(test->m_loadEvents.size(), ==, 3);
     1364    g_assert_cmpint(test->m_loadEvents[0], ==, LoadTrackingTest::ProvisionalLoadStarted);
     1365    g_assert_cmpint(test->m_loadEvents[1], ==, LoadTrackingTest::LoadCommitted);
     1366    g_assert_cmpint(test->m_loadEvents[2], ==, LoadTrackingTest::LoadFinished);
     1367    g_assert_cmpstr(webkit_web_view_get_title(test->m_webView), ==, authExpectedFailureTitle);
     1368}
     1369
     1370static void testWebViewAuthenticationStorage(AuthenticationTest* test, gconstpointer)
     1371{
     1372    // Enable private browsing before authentication request to test that credentials can't be saved.
     1373    webkit_settings_set_enable_private_browsing(webkit_web_view_get_settings(test->m_webView), TRUE);
     1374    test->loadURI(kServer->getURIForPath("/auth-test.html").data());
     1375    WebKitAuthenticationRequest* request = test->waitForAuthenticationRequest();
     1376    g_assert(!webkit_authentication_request_get_proposed_credential(request));
     1377    g_assert(!webkit_authentication_request_can_save_credentials(request));
     1378
     1379    // If WebKit has been compiled with libsecret, and private browsing is disabled
     1380    // then check that credentials can be saved.
     1381#if ENABLE(CREDENTIAL_STORAGE)
     1382    webkit_settings_set_enable_private_browsing(webkit_web_view_get_settings(test->m_webView), FALSE);
     1383    test->loadURI(kServer->getURIForPath("/auth-test.html").data());
     1384    request = test->waitForAuthenticationRequest();
     1385    g_assert(!webkit_authentication_request_get_proposed_credential(request));
     1386    g_assert(webkit_authentication_request_can_save_credentials(request));
     1387#endif
     1388}
     1389
     1390static void testWebViewAuthenticationSuccess(AuthenticationTest* test, gconstpointer)
     1391{
     1392    // Test correct authentication.
     1393    test->loadURI(kServer->getURIForPath("/auth-test.html").data());
     1394    WebKitAuthenticationRequest* request = test->waitForAuthenticationRequest();
     1395    WebKitCredential* credential = webkit_credential_new(authTestUsername, authTestPassword, WEBKIT_CREDENTIAL_PERSISTENCE_FOR_SESSION);
     1396    webkit_authentication_request_authenticate(request, credential);
     1397    webkit_credential_free(credential);
     1398    test->waitUntilLoadFinished();
     1399
     1400    g_assert_cmpint(test->m_loadEvents.size(), ==, 3);
     1401    g_assert_cmpint(test->m_loadEvents[0], ==, LoadTrackingTest::ProvisionalLoadStarted);
     1402    g_assert_cmpint(test->m_loadEvents[1], ==, LoadTrackingTest::LoadCommitted);
     1403    g_assert_cmpint(test->m_loadEvents[2], ==, LoadTrackingTest::LoadFinished);
     1404    g_assert_cmpstr(webkit_web_view_get_title(test->m_webView), ==, authExpectedSuccessTitle);
     1405
     1406    // Test loading the same (authorized) page again.
     1407    test->loadURI(kServer->getURIForPath("/auth-test.html").data());
     1408    // There is no authentication challenge.
     1409    test->waitUntilLoadFinished();
     1410
     1411    g_assert_cmpint(test->m_loadEvents.size(), ==, 3);
     1412    g_assert_cmpint(test->m_loadEvents[0], ==, LoadTrackingTest::ProvisionalLoadStarted);
     1413    g_assert_cmpint(test->m_loadEvents[1], ==, LoadTrackingTest::LoadCommitted);
     1414    g_assert_cmpint(test->m_loadEvents[2], ==, LoadTrackingTest::LoadFinished);
     1415    g_assert_cmpstr(webkit_web_view_get_title(test->m_webView), ==, authExpectedSuccessTitle);
     1416}
     1417
     1418static void serverCallback(SoupServer*, SoupMessage* message, const char* path, GHashTable*, SoupClientContext*, void*)
     1419{
     1420    if (message->method != SOUP_METHOD_GET) {
     1421        soup_message_set_status(message, SOUP_STATUS_NOT_IMPLEMENTED);
     1422        return;
     1423    }
     1424
     1425    if (!strcmp(path, "/auth-test.html")) {
     1426        const char* authorization = soup_message_headers_get_one(message->request_headers, "Authorization");
     1427        // Require authentication.
     1428        if (!g_strcmp0(authorization, authExpectedAuthorization)) {
     1429            // Successful authentication.
     1430            soup_message_set_status(message, SOUP_STATUS_OK);
     1431            soup_message_body_append(message->response_body, SOUP_MEMORY_STATIC, authSuccessHTMLString, strlen(authSuccessHTMLString));
     1432            AuthenticationTest::authenticationRetries = 0;
     1433        } else if (++AuthenticationTest::authenticationRetries < 3) {
     1434            // No or invalid authorization header provided by the client, request authentication twice then fail.
     1435            soup_message_set_status(message, SOUP_STATUS_UNAUTHORIZED);
     1436            soup_message_headers_append(message->response_headers, "WWW-Authenticate", "Basic realm=\"my realm\"");
     1437            // Include a failure message in case the user attempts to proceed without authentication.
     1438            soup_message_body_append(message->response_body, SOUP_MEMORY_STATIC, authFailureHTMLString, strlen(authFailureHTMLString));
     1439        } else {
     1440            // Authentication not successful, display a "401 Authorization Required" page.
     1441            soup_message_set_status(message, SOUP_STATUS_OK);
     1442            soup_message_body_append(message->response_body, SOUP_MEMORY_STATIC, authFailureHTMLString, strlen(authFailureHTMLString));
     1443        }
     1444    } else
     1445        soup_message_set_status(message, SOUP_STATUS_NOT_FOUND);
     1446
     1447    soup_message_body_complete(message->response_body);
     1448}
     1449
    12321450void beforeAll()
    12331451{
     1452    kServer = new WebKitTestServer();
     1453    kServer->run(serverCallback);
     1454
    12341455    WebViewTest::add("WebKitWebView", "default-context", testWebViewDefaultContext);
    12351456    WebViewTest::add("WebKitWebView", "custom-charset", testWebViewCustomCharset);
     
    12521473    SnapshotWebViewTest::add("WebKitWebView", "snapshot", testWebViewSnapshot);
    12531474    WebViewTest::add("WebKitWebView", "page-visibility", testWebViewPageVisibility);
     1475    AuthenticationTest::add("WebKitWebView", "authentication-request", testWebViewAuthenticationRequest);
     1476    AuthenticationTest::add("WebKitWebView", "authentication-cancel", testWebViewAuthenticationCancel);
     1477    AuthenticationTest::add("WebKitWebView", "authentication-failure", testWebViewAuthenticationFailure);
     1478    AuthenticationTest::add("WebKitWebView", "authentication-no-credential", testWebViewAuthenticationNoCredential);
     1479    AuthenticationTest::add("WebKitWebView", "authentication-storage", testWebViewAuthenticationStorage);
     1480    // Testing authentication success must be done last because libsoup will never fire
     1481    // the authenticate signal again once authentication is successful.
     1482    AuthenticationTest::add("WebKitWebView", "authentication-success", testWebViewAuthenticationSuccess);
    12541483}
    12551484
    12561485void afterAll()
    12571486{
    1258 }
     1487    delete kServer;
     1488}
  • trunk/Source/WebKit2/UIProcess/API/gtk/webkit2.h

    r149117 r153882  
    2424#define __WEBKIT2_H_INSIDE__
    2525
     26#include <webkit2/WebKitAuthenticationRequest.h>
    2627#include <webkit2/WebKitBackForwardList.h>
    2728#include <webkit2/WebKitBackForwardListItem.h>
     
    3031#include <webkit2/WebKitContextMenuItem.h>
    3132#include <webkit2/WebKitCookieManager.h>
     33#include <webkit2/WebKitCredential.h>
    3234#include <webkit2/WebKitDefines.h>
    3335#include <webkit2/WebKitDownload.h>
Note: See TracChangeset for help on using the changeset viewer.