File: ewk_auth_soup.cpp

package info (click to toggle)
qtwebkit-opensource-src 5.7.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 291,692 kB
  • ctags: 268,122
  • sloc: cpp: 1,360,420; python: 70,286; ansic: 42,986; perl: 35,476; ruby: 12,236; objc: 9,465; xml: 8,396; asm: 3,873; yacc: 2,397; sh: 1,647; makefile: 650; lex: 644; java: 110
file content (137 lines) | stat: -rw-r--r-- 4,537 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
    Copyright (C) 2009 Igalia S.L.
    Copyright (C) 2011 Samsung Electronics

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "config.h"
#include "ewk_auth_soup_private.h"

#include "ewk_auth.h"
#include <glib-object.h>
#include <glib.h>
#include <libsoup/soup.h>

/**
 * #Ewk_Soup_Auth_Dialog is a #SoupSessionFeature that you can attach to your
 * #SoupSession to provide a simple authentication dialog while
 * handling HTTP basic auth. It is built as a simple C-only module
 * to ease reuse.
 */

typedef struct _Ewk_Auth_Data {
    SoupMessage* message;
    SoupAuth* auth;
    SoupSession* session;
} Ewk_Auth_Data;

static Ewk_Auth_Show_Dialog_Callback ewk_auth_show_dialog_callback = 0;

static void ewk_auth_soup_dialog_session_feature_init(SoupSessionFeatureInterface*, gpointer);
static void attach(SoupSessionFeature*, SoupSession*);
static void detach(SoupSessionFeature*, SoupSession*);
static void free_auth_data(Ewk_Auth_Data*);

G_DEFINE_TYPE_WITH_CODE(Ewk_Soup_Auth_Dialog, ewk_auth_soup_dialog, G_TYPE_OBJECT,
                        G_IMPLEMENT_INTERFACE(SOUP_TYPE_SESSION_FEATURE, ewk_auth_soup_dialog_session_feature_init))

static void ewk_auth_soup_dialog_class_init(Ewk_Soup_Auth_DialogClass*)
{
}

static void ewk_auth_soup_dialog_init(Ewk_Soup_Auth_Dialog*)
{
}

static void ewk_auth_soup_dialog_session_feature_init(SoupSessionFeatureInterface* featureInterface, gpointer /*interfaceData*/)
{
    featureInterface->attach = attach;
    featureInterface->detach = detach;
}

/**
 * @internal
 *  Sets callback to be called when authentication is required.
 */
void ewk_auth_soup_show_dialog_callback_set(Ewk_Auth_Show_Dialog_Callback callback)
{
    ewk_auth_show_dialog_callback = callback;
}

/**
 * @internal
 *  Method for setting credentials
 *
 *  @param username username
 *  @param password password
 *  @param data soup authentication data
 */
void ewk_auth_soup_credentials_set(const char* username, const char* password, void* data)
{
    if (!data)
        return;

    Ewk_Auth_Data* authenticationData = static_cast<Ewk_Auth_Data*>(data);
    soup_auth_authenticate(authenticationData->auth, username, password);
    soup_session_unpause_message(authenticationData->session, authenticationData->message);
    free_auth_data(authenticationData);
}

static void session_authenticate(SoupSession* session, SoupMessage* message, SoupAuth* auth, gboolean /*retrying*/, gpointer /*user_data*/)
{
    SoupURI* uri;
    Ewk_Auth_Data* authenticationData;
    const char* realm;

    if (!ewk_auth_show_dialog_callback)
        return;

    soup_session_pause_message(session, message);
    // We need to make sure the message sticks around when pausing it.
    g_object_ref(message);
    g_object_ref(session);
    g_object_ref(auth);

    authenticationData = new Ewk_Auth_Data;
    authenticationData->message = message;
    authenticationData->auth = auth;
    authenticationData->session = session;

    uri = soup_message_get_uri(authenticationData->message);
    realm = soup_auth_get_realm(authenticationData->auth);

    // Call application method responsible for showing authentication dialog and sending back authorization data.
    ewk_auth_show_dialog_callback(realm, soup_uri_to_string(uri, TRUE), authenticationData);
}

static void free_auth_data(Ewk_Auth_Data* authData)
{
    g_object_unref(authData->message);
    g_object_unref(authData->session);
    g_object_unref(authData->auth);
    delete authData;
}

static void attach(SoupSessionFeature* manager, SoupSession* session)
{
    g_signal_connect(session, "authenticate", G_CALLBACK(session_authenticate), manager);
}

static void detach(SoupSessionFeature* manager, SoupSession* session)
{
    g_signal_handlers_disconnect_by_func(session, (void*)session_authenticate, manager);
}