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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
/*
* Copyright (C) 2013 Samsung Electronics Inc. All rights reserved.
*
* 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 "WebKitCredential.h"
#include "WebCredential.h"
#include "WebKitCredentialPrivate.h"
#include <wtf/text/CString.h>
using namespace WebKit;
/**
* WebKitCredential:
*
* Groups information used for user authentication.
*
* Since: 2.2
*/
struct _WebKitCredential {
_WebKitCredential(const WebCore::Credential& coreCredential)
: credential(coreCredential)
{
}
WebCore::Credential credential;
CString username;
CString password;
};
G_DEFINE_BOXED_TYPE(WebKitCredential, webkit_credential, webkit_credential_copy, webkit_credential_free)
static inline WebKitCredentialPersistence toWebKitCredentialPersistence(WebCore::CredentialPersistence corePersistence)
{
switch (corePersistence) {
case WebCore::CredentialPersistenceNone:
return WEBKIT_CREDENTIAL_PERSISTENCE_NONE;
case WebCore::CredentialPersistenceForSession:
return WEBKIT_CREDENTIAL_PERSISTENCE_FOR_SESSION;
case WebCore::CredentialPersistencePermanent:
return WEBKIT_CREDENTIAL_PERSISTENCE_PERMANENT;
default:
ASSERT_NOT_REACHED();
return WEBKIT_CREDENTIAL_PERSISTENCE_NONE;
}
}
static inline WebCore::CredentialPersistence toWebCoreCredentialPersistence(WebKitCredentialPersistence kitPersistence)
{
switch (kitPersistence) {
case WEBKIT_CREDENTIAL_PERSISTENCE_NONE:
return WebCore::CredentialPersistenceNone;
case WEBKIT_CREDENTIAL_PERSISTENCE_FOR_SESSION:
return WebCore::CredentialPersistenceForSession;
case WEBKIT_CREDENTIAL_PERSISTENCE_PERMANENT:
return WebCore::CredentialPersistencePermanent;
default:
ASSERT_NOT_REACHED();
return WebCore::CredentialPersistenceNone;
}
}
WebKitCredential* webkitCredentialCreate(const WebCore::Credential& coreCredential)
{
WebKitCredential* credential = static_cast<WebKitCredential*>(fastMalloc(sizeof(WebKitCredential)));
new (credential) WebKitCredential(coreCredential);
return credential;
}
const WebCore::Credential& webkitCredentialGetCredential(WebKitCredential* credential)
{
ASSERT(credential);
return credential->credential;
}
/**
* webkit_credential_new:
* @username: The username for the new credential
* @password: The password for the new credential
* @persistence: The #WebKitCredentialPersistence of the new credential
*
* Create a new credential from the provided username, password and persistence mode.
*
* Returns: (transfer full): A #WebKitCredential.
*
* Since: 2.2
*/
WebKitCredential* webkit_credential_new(const gchar* username, const gchar* password, WebKitCredentialPersistence persistence)
{
g_return_val_if_fail(username, 0);
g_return_val_if_fail(password, 0);
return webkitCredentialCreate(WebCore::Credential(String::fromUTF8(username), String::fromUTF8(password), toWebCoreCredentialPersistence(persistence)));
}
/**
* webkit_credential_new_for_certificate_pin:
* @pin: The PIN for the new credential
* @persistence: The #WebKitCredentialPersistence of the new credential
*
* Create a new credential from the provided PIN and persistence mode.
*
* Note that %WEBKIT_CREDENTIAL_PERSISTENCE_PERMANENT is not supported for certificate pin credentials.
*
* Returns: (transfer full): A #WebKitCredential.
*
* Since: 2.34
*/
WebKitCredential* webkit_credential_new_for_certificate_pin(const gchar* pin, WebKitCredentialPersistence persistence)
{
g_return_val_if_fail(pin, 0);
if (persistence == WEBKIT_CREDENTIAL_PERSISTENCE_PERMANENT) {
g_warning("Permanent persistence is not supported for certificate pin credentials. Session persistence will be used instead.");
persistence = WEBKIT_CREDENTIAL_PERSISTENCE_FOR_SESSION;
}
return webkitCredentialCreate(WebCore::Credential(emptyString(), String::fromUTF8(pin), toWebCoreCredentialPersistence(persistence)));
}
/**
* webkit_credential_new_for_certificate:
* @certificate: (nullable): The #GTlsCertificate, or %NULL
* @persistence: The #WebKitCredentialPersistence of the new credential
*
* Create a new credential from the @certificate and persistence mode.
*
* Note that %WEBKIT_CREDENTIAL_PERSISTENCE_PERMANENT is not supported for certificate credentials.
*
* Returns: (transfer full): A #WebKitCredential.
*
* Since: 2.34
*/
WebKitCredential* webkit_credential_new_for_certificate(GTlsCertificate* certificate, WebKitCredentialPersistence persistence)
{
g_return_val_if_fail(!certificate || G_IS_TLS_CERTIFICATE(certificate), nullptr);
if (persistence == WEBKIT_CREDENTIAL_PERSISTENCE_PERMANENT) {
g_warning("Permanent persistence is not supported for certificate credentials. Session persistence will be used instead.");
persistence = WEBKIT_CREDENTIAL_PERSISTENCE_FOR_SESSION;
}
return webkitCredentialCreate(WebCore::Credential(certificate, toWebCoreCredentialPersistence(persistence)));
}
/**
* webkit_credential_copy:
* @credential: a #WebKitCredential
*
* Make a copy of the #WebKitCredential.
*
* Returns: (transfer full): A copy of passed in #WebKitCredential
*
* Since: 2.2
*/
WebKitCredential* webkit_credential_copy(WebKitCredential* credential)
{
g_return_val_if_fail(credential, 0);
return webkitCredentialCreate(credential->credential);
}
/**
* webkit_credential_free:
* @credential: A #WebKitCredential
*
* Free the #WebKitCredential.
*
* Since: 2.2
*/
void webkit_credential_free(WebKitCredential* credential)
{
g_return_if_fail(credential);
credential->~WebKitCredential();
fastFree(credential);
}
/**
* webkit_credential_get_username:
* @credential: a #WebKitCredential
*
* Get the username currently held by this #WebKitCredential.
*
* Returns: The username stored in the #WebKitCredential.
*
* Since: 2.2
*/
const gchar* webkit_credential_get_username(WebKitCredential* credential)
{
g_return_val_if_fail(credential, 0);
if (credential->username.isNull())
credential->username = credential->credential.user().utf8();
return credential->username.data();
}
/**
* webkit_credential_get_password:
* @credential: a #WebKitCredential
*
* Get the password currently held by this #WebKitCredential.
*
* Returns: The password stored in the #WebKitCredential.
*
* Since: 2.2
*/
const gchar* webkit_credential_get_password(WebKitCredential* credential)
{
g_return_val_if_fail(credential, 0);
if (credential->password.isNull())
credential->password = credential->credential.password().utf8();
return credential->password.data();
}
/**
* webkit_credential_has_password:
* @credential: a #WebKitCredential
*
* Determine whether this credential has a password stored.
*
* Returns: %TRUE if the credential has a password or %FALSE otherwise.
*
* Since: 2.2
*/
gboolean webkit_credential_has_password(WebKitCredential* credential)
{
g_return_val_if_fail(credential, FALSE);
return credential->credential.hasPassword();
}
/**
* webkit_credential_get_certificate:
* @credential: a #WebKitCredential
*
* Get the certificate currently held by this #WebKitCredential.
*
* Returns: (transfer none): a #GTlsCertificate, or %NULL
*
* Since: 2.34
*/
GTlsCertificate* webkit_credential_get_certificate(WebKitCredential* credential)
{
g_return_val_if_fail(credential, NULL);
return credential->credential.certificate();
}
/**
* webkit_credential_get_persistence:
* @credential: a #WebKitCredential
*
* Get the persistence mode currently held by this #WebKitCredential.
*
* Returns: The #WebKitCredentialPersistence stored in the #WebKitCredential.
*
* Since: 2.2
*/
WebKitCredentialPersistence webkit_credential_get_persistence(WebKitCredential* credential)
{
g_return_val_if_fail(credential, WEBKIT_CREDENTIAL_PERSISTENCE_NONE);
return toWebKitCredentialPersistence(credential->credential.persistence());
}
|