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
|
/*
* Copyright (C) 2017 Google Inc. All rights reserved.
* Copyright (C) 2017 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "CredentialsContainer.h"
#if ENABLE(WEB_AUTHN)
#include "AbortSignal.h"
#include "CredentialCreationOptions.h"
#include "CredentialRequestOptions.h"
#include "Document.h"
#include "ExceptionOr.h"
#include "JSDOMPromiseDeferred.h"
#include "Page.h"
#include "SecurityOrigin.h"
#include "WebAuthenticationConstants.h"
namespace WebCore {
CredentialsContainer::CredentialsContainer(WeakPtr<Document, WeakPtrImplWithEventTargetData>&& document)
: m_document(WTFMove(document))
{
}
ScopeAndCrossOriginParent CredentialsContainer::scopeAndCrossOriginParent() const
{
if (!m_document)
return std::pair { WebAuthn::Scope::CrossOrigin, std::nullopt };
bool isSameSite = true;
auto& origin = m_document->securityOrigin();
auto& url = m_document->url();
std::optional<SecurityOriginData> crossOriginParent;
for (auto* document = m_document->parentDocument(); document; document = document->parentDocument()) {
if (!origin.isSameOriginDomain(document->securityOrigin()) && !areRegistrableDomainsEqual(url, document->url()))
isSameSite = false;
if (!crossOriginParent && !origin.isSameOriginAs(document->securityOrigin()))
crossOriginParent = origin.data();
}
if (!crossOriginParent)
return std::pair { WebAuthn::Scope::SameOrigin, std::nullopt };
if (isSameSite)
return std::pair { WebAuthn::Scope::SameSite, std::nullopt };
return std::pair { WebAuthn::Scope::CrossOrigin, crossOriginParent };
}
void CredentialsContainer::get(CredentialRequestOptions&& options, CredentialPromise&& promise)
{
// The following implements https://www.w3.org/TR/credential-management-1/#algorithm-request as of 4 August 2017
// with enhancement from 14 November 2017 Editor's Draft.
if (!m_document || !m_document->page()) {
promise.reject(Exception { NotSupportedError });
return;
}
if (options.signal && options.signal->aborted()) {
promise.reject(Exception { AbortError, "Aborted by AbortSignal."_s });
return;
}
// Step 1-2.
ASSERT(m_document->isSecureContext());
// Step 3 is enhanced with doesHaveSameOriginAsItsAncestors.
// Step 4-6. Shortcut as we only support PublicKeyCredential which can only
// be requested from [[discoverFromExternalSource]].
if (!options.publicKey) {
promise.reject(Exception { NotSupportedError, "Only PublicKeyCredential is supported."_s });
return;
}
// The request will be aborted in WebAuthenticatorCoordinatorProxy if conditional mediation is not available.
if (options.mediation != CredentialRequestOptions::MediationRequirement::Conditional && !m_document->hasFocus()) {
promise.reject(Exception { NotAllowedError, "The document is not focused."_s });
return;
}
m_document->page()->authenticatorCoordinator().discoverFromExternalSource(*m_document, WTFMove(options), scopeAndCrossOriginParent(), WTFMove(promise));
}
void CredentialsContainer::store(const BasicCredential&, CredentialPromise&& promise)
{
promise.reject(Exception { NotSupportedError, "Not implemented."_s });
}
void CredentialsContainer::isCreate(CredentialCreationOptions&& options, CredentialPromise&& promise)
{
// The following implements https://www.w3.org/TR/credential-management-1/#algorithm-create as of 4 August 2017
// with enhancement from 14 November 2017 Editor's Draft.
if (!m_document || !m_document->page()) {
promise.reject(Exception { NotSupportedError });
return;
}
if (options.signal && options.signal->aborted()) {
promise.reject(Exception { AbortError, "Aborted by AbortSignal."_s });
return;
}
// Step 1-2.
ASSERT(m_document->isSecureContext());
// Step 3-7. Shortcut as we only support one kind of credentials.
if (!options.publicKey) {
promise.reject(Exception { NotSupportedError, "Only PublicKeyCredential is supported."_s });
return;
}
// Extra.
if (!m_document->hasFocus()) {
promise.reject(Exception { NotAllowedError, "The document is not focused."_s });
return;
}
m_document->page()->authenticatorCoordinator().create(*m_document, options.publicKey.value(), scopeAndCrossOriginParent().first, WTFMove(options.signal), WTFMove(promise));
}
void CredentialsContainer::preventSilentAccess(DOMPromiseDeferred<void>&& promise) const
{
promise.resolve();
}
} // namespace WebCore
#endif // ENABLE(WEB_AUTHN)
|