WebKitAuthenticationRequest.cpp [plain text]
#include "config.h"
#include "WebKitAuthenticationRequest.h"
#include "AuthenticationDecisionListener.h"
#include "WebCredential.h"
#include "WebKitAuthenticationRequestPrivate.h"
#include "WebKitCredentialPrivate.h"
#include "WebProtectionSpace.h"
#include <glib/gi18n-lib.h>
#include <wtf/glib/WTFGType.h>
#include <wtf/text/CString.h>
using namespace WebKit;
using namespace WebCore;
enum {
CANCELLED,
LAST_SIGNAL
};
struct _WebKitAuthenticationRequestPrivate {
RefPtr<AuthenticationChallengeProxy> authenticationChallenge;
bool privateBrowsingEnabled;
bool handledRequest;
CString host;
CString realm;
};
static guint signals[LAST_SIGNAL] = { 0, };
WEBKIT_DEFINE_TYPE(WebKitAuthenticationRequest, webkit_authentication_request, G_TYPE_OBJECT)
static inline WebKitAuthenticationScheme toWebKitAuthenticationScheme(WebCore::ProtectionSpaceAuthenticationScheme coreScheme)
{
switch (coreScheme) {
case WebCore::ProtectionSpaceAuthenticationSchemeDefault:
return WEBKIT_AUTHENTICATION_SCHEME_DEFAULT;
case WebCore::ProtectionSpaceAuthenticationSchemeHTTPBasic:
return WEBKIT_AUTHENTICATION_SCHEME_HTTP_BASIC;
case WebCore::ProtectionSpaceAuthenticationSchemeHTTPDigest:
return WEBKIT_AUTHENTICATION_SCHEME_HTTP_DIGEST;
case WebCore::ProtectionSpaceAuthenticationSchemeHTMLForm:
return WEBKIT_AUTHENTICATION_SCHEME_HTML_FORM;
case WebCore::ProtectionSpaceAuthenticationSchemeNTLM:
return WEBKIT_AUTHENTICATION_SCHEME_NTLM;
case WebCore::ProtectionSpaceAuthenticationSchemeNegotiate:
return WEBKIT_AUTHENTICATION_SCHEME_NEGOTIATE;
case WebCore::ProtectionSpaceAuthenticationSchemeClientCertificateRequested:
return WEBKIT_AUTHENTICATION_SCHEME_CLIENT_CERTIFICATE_REQUESTED;
case WebCore::ProtectionSpaceAuthenticationSchemeServerTrustEvaluationRequested:
return WEBKIT_AUTHENTICATION_SCHEME_SERVER_TRUST_EVALUATION_REQUESTED;
case WebCore::ProtectionSpaceAuthenticationSchemeUnknown:
return WEBKIT_AUTHENTICATION_SCHEME_UNKNOWN;
default:
ASSERT_NOT_REACHED();
return WEBKIT_AUTHENTICATION_SCHEME_DEFAULT;
}
}
static void webkitAuthenticationRequestDispose(GObject* object)
{
WebKitAuthenticationRequest* request = WEBKIT_AUTHENTICATION_REQUEST(object);
if (!request->priv->handledRequest)
webkit_authentication_request_cancel(request);
G_OBJECT_CLASS(webkit_authentication_request_parent_class)->dispose(object);
}
static void webkit_authentication_request_class_init(WebKitAuthenticationRequestClass* requestClass)
{
GObjectClass* objectClass = G_OBJECT_CLASS(requestClass);
objectClass->dispose = webkitAuthenticationRequestDispose;
signals[CANCELLED] =
g_signal_new("cancelled",
G_TYPE_FROM_CLASS(objectClass),
G_SIGNAL_RUN_LAST,
0, 0, 0,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
}
WebKitAuthenticationRequest* webkitAuthenticationRequestCreate(AuthenticationChallengeProxy* authenticationChallenge, bool privateBrowsingEnabled)
{
WebKitAuthenticationRequest* request = WEBKIT_AUTHENTICATION_REQUEST(g_object_new(WEBKIT_TYPE_AUTHENTICATION_REQUEST, NULL));
request->priv->authenticationChallenge = authenticationChallenge;
request->priv->privateBrowsingEnabled = privateBrowsingEnabled;
return request;
}
AuthenticationChallengeProxy* webkitAuthenticationRequestGetAuthenticationChallenge(WebKitAuthenticationRequest* request)
{
return request->priv->authenticationChallenge.get();
}
gboolean webkit_authentication_request_can_save_credentials(WebKitAuthenticationRequest* request)
{
g_return_val_if_fail(WEBKIT_IS_AUTHENTICATION_REQUEST(request), FALSE);
#if USE(LIBSECRET)
return !request->priv->privateBrowsingEnabled;
#else
return FALSE;
#endif
}
WebKitCredential* webkit_authentication_request_get_proposed_credential(WebKitAuthenticationRequest* request)
{
g_return_val_if_fail(WEBKIT_IS_AUTHENTICATION_REQUEST(request), 0);
const WebCore::Credential& credential = request->priv->authenticationChallenge->proposedCredential()->credential();
if (credential.isEmpty())
return 0;
return webkitCredentialCreate(credential);
}
const gchar* webkit_authentication_request_get_host(WebKitAuthenticationRequest* request)
{
g_return_val_if_fail(WEBKIT_IS_AUTHENTICATION_REQUEST(request), 0);
if (request->priv->host.isNull())
request->priv->host = request->priv->authenticationChallenge->protectionSpace()->host().utf8();
return request->priv->host.data();
}
guint webkit_authentication_request_get_port(WebKitAuthenticationRequest* request)
{
g_return_val_if_fail(WEBKIT_IS_AUTHENTICATION_REQUEST(request), 0);
return request->priv->authenticationChallenge->protectionSpace()->port();
}
const gchar* webkit_authentication_request_get_realm(WebKitAuthenticationRequest* request)
{
g_return_val_if_fail(WEBKIT_IS_AUTHENTICATION_REQUEST(request), 0);
if (request->priv->realm.isNull())
request->priv->realm = request->priv->authenticationChallenge->protectionSpace()->realm().utf8();
return request->priv->realm.data();
}
WebKitAuthenticationScheme webkit_authentication_request_get_scheme(WebKitAuthenticationRequest* request)
{
g_return_val_if_fail(WEBKIT_IS_AUTHENTICATION_REQUEST(request), WEBKIT_AUTHENTICATION_SCHEME_UNKNOWN);
return toWebKitAuthenticationScheme(request->priv->authenticationChallenge->protectionSpace()->authenticationScheme());
}
gboolean webkit_authentication_request_is_for_proxy(WebKitAuthenticationRequest* request)
{
g_return_val_if_fail(WEBKIT_IS_AUTHENTICATION_REQUEST(request), FALSE);
return request->priv->authenticationChallenge->protectionSpace()->isProxy();
}
gboolean webkit_authentication_request_is_retry(WebKitAuthenticationRequest* request)
{
g_return_val_if_fail(WEBKIT_IS_AUTHENTICATION_REQUEST(request), 0);
return request->priv->authenticationChallenge->previousFailureCount() ? TRUE : FALSE;
}
void webkit_authentication_request_authenticate(WebKitAuthenticationRequest* request, WebKitCredential* credential)
{
g_return_if_fail(WEBKIT_IS_AUTHENTICATION_REQUEST(request));
if (credential)
request->priv->authenticationChallenge->listener()->useCredential(WebCredential::create(webkitCredentialGetCredential(credential)).ptr());
else
request->priv->authenticationChallenge->listener()->useCredential(nullptr);
request->priv->handledRequest = true;
}
void webkit_authentication_request_cancel(WebKitAuthenticationRequest* request)
{
g_return_if_fail(WEBKIT_IS_AUTHENTICATION_REQUEST(request));
request->priv->authenticationChallenge->listener()->cancel();
g_signal_emit(request, signals[CANCELLED], 0);
}