CredentialBackingStore.cpp [plain text]
#include "config.h"
#include "CredentialBackingStore.h"
#if ENABLE(CREDENTIAL_STORAGE)
#define SECRET_WITH_UNSTABLE 1
#define SECRET_API_SUBJECT_TO_CHANGE 1
#include "AuthenticationChallenge.h"
#include "GRefPtrGtk.h"
#include <glib/gi18n-lib.h>
#include <libsecret/secret.h>
#include <libsoup/soup.h>
#include <wtf/gobject/GUniquePtr.h>
#include <wtf/text/CString.h>
#endif
namespace WebCore {
CredentialBackingStore& credentialBackingStore()
{
DEPRECATED_DEFINE_STATIC_LOCAL(CredentialBackingStore, backingStore, ());
return backingStore;
}
#if ENABLE(CREDENTIAL_STORAGE)
static GRefPtr<GHashTable> createAttributeHashTableFromChallenge(const AuthenticationChallenge& challenge, const Credential& credential = Credential())
{
SoupURI* uri = soup_message_get_uri(challenge.soupMessage());
GRefPtr<GHashTable> attributes = adoptGRef(secret_attributes_build(
SECRET_SCHEMA_COMPAT_NETWORK,
"domain", soup_auth_get_realm(challenge.soupAuth()),
"server", uri->host,
"protocol", uri->scheme,
"authtype", soup_auth_get_scheme_name(challenge.soupAuth()),
"port", uri->port,
NULL));
if (credential.isEmpty())
return attributes;
g_hash_table_insert(attributes.get(), g_strdup("user"), g_strdup(credential.user().utf8().data()));
return attributes;
}
struct CredentialForChallengeAsyncReadyCallbackData {
CredentialBackingStore::CredentialForChallengeCallback callback;
void* data;
};
static void credentialForChallengeAsyncReadyCallback(SecretService* service, GAsyncResult* asyncResult, CredentialForChallengeAsyncReadyCallbackData* callbackData)
{
CredentialBackingStore::CredentialForChallengeCallback callback = callbackData->callback;
void* data = callbackData->data;
delete callbackData;
GUniqueOutPtr<GError> error;
GUniquePtr<GList> elements(secret_service_search_finish(service, asyncResult, &error.outPtr()));
if (error || !elements || !elements->data) {
callback(Credential(), data);
return;
}
GRefPtr<SecretItem> secretItem = adoptGRef(static_cast<SecretItem*>(elements->data));
GRefPtr<GHashTable> attributes = adoptGRef(secret_item_get_attributes(secretItem.get()));
String user = String::fromUTF8(static_cast<const char*>(g_hash_table_lookup(attributes.get(), "user")));
if (user.isEmpty()) {
callback(Credential(), data);
return;
}
size_t length;
GRefPtr<SecretValue> secretValue = adoptGRef(secret_item_get_secret(secretItem.get()));
const char* passwordData = secret_value_get(secretValue.get(), &length);
String password = String::fromUTF8(passwordData, length);
callback(Credential(user, password, CredentialPersistencePermanent), data);
}
#endif // ENABLE(CREDENTIAL_STORAGE)
void CredentialBackingStore::credentialForChallenge(const AuthenticationChallenge& challenge, CredentialForChallengeCallback callback, void* data)
{
#if ENABLE(CREDENTIAL_STORAGE)
SecretSearchFlags searchFlags = static_cast<SecretSearchFlags>(SECRET_SEARCH_UNLOCK | SECRET_SEARCH_LOAD_SECRETS);
CredentialForChallengeAsyncReadyCallbackData* callbackData = new CredentialForChallengeAsyncReadyCallbackData;
callbackData->callback = callback;
callbackData->data = data;
secret_service_search(
0, SECRET_SCHEMA_COMPAT_NETWORK,
createAttributeHashTableFromChallenge(challenge).get(),
searchFlags,
0, reinterpret_cast<GAsyncReadyCallback>(credentialForChallengeAsyncReadyCallback),
callbackData);
#else
UNUSED_PARAM(challenge);
callback(Credential(), data);
#endif // ENABLE(CREDENTIAL_STORAGE)
}
void CredentialBackingStore::storeCredentialsForChallenge(const AuthenticationChallenge& challenge, const Credential& credential)
{
#if ENABLE(CREDENTIAL_STORAGE)
CString utf8Password = credential.password().utf8();
GRefPtr<SecretValue> newSecretValue = adoptGRef(secret_value_new(utf8Password.data(), utf8Password.length(), "text/plain"));
secret_service_store(
0, SECRET_SCHEMA_COMPAT_NETWORK,
createAttributeHashTableFromChallenge(challenge, credential).get(),
SECRET_COLLECTION_DEFAULT,
_("WebKitGTK+ password"),
newSecretValue.get(),
0, 0, 0); #else
UNUSED_PARAM(challenge);
UNUSED_PARAM(credential);
#endif // ENABLE(CREDENTIAL_STORAGE)
}
}