Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(516)

Unified Diff: third_party/WebKit/Source/modules/webauth/WebAuthentication.cpp

Issue 2702653002: Patch #1 of multiple. Add webauth .mojom and initial usage of makeCredential. (Closed)
Patch Set: Adding additional comments to mojom structs. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/modules/webauth/WebAuthentication.cpp
diff --git a/third_party/WebKit/Source/modules/webauth/WebAuthentication.cpp b/third_party/WebKit/Source/modules/webauth/WebAuthentication.cpp
index 1274dcf6c690975e947e853361836ae62d97788a..4eaa2618fb20aa0bb5c7c24ebcaea250fe18c78f 100644
--- a/third_party/WebKit/Source/modules/webauth/WebAuthentication.cpp
+++ b/third_party/WebKit/Source/modules/webauth/WebAuthentication.cpp
@@ -4,27 +4,170 @@
#include "modules/webauth/WebAuthentication.h"
+#include <stdint.h>
+
#include "bindings/core/v8/ScriptPromise.h"
+#include "bindings/core/v8/ScriptPromiseResolver.h"
+#include "core/dom/DOMException.h"
+#include "core/dom/Document.h"
+#include "core/dom/ExceptionCode.h"
+#include "core/frame/LocalFrame.h"
+#include "modules/webauth/RelyingPartyAccount.h"
+#include "modules/webauth/ScopedCredential.h"
+#include "modules/webauth/ScopedCredentialOptions.h"
+#include "modules/webauth/ScopedCredentialParameters.h"
+#include "public/platform/InterfaceProvider.h"
+
+namespace {
+const char kNoAuthenticatorError[] = "Authenticator unavailable.";
+} // anonymous namespace
+
+namespace mojo {
+
+using webauth::mojom::blink::RelyingPartyAccount;
+using webauth::mojom::blink::RelyingPartyAccountPtr;
+using webauth::mojom::blink::ScopedCredentialOptions;
+using webauth::mojom::blink::ScopedCredentialOptionsPtr;
+using webauth::mojom::blink::ScopedCredentialParameters;
+using webauth::mojom::blink::ScopedCredentialParametersPtr;
+using webauth::mojom::blink::ScopedCredentialDescriptor;
+using webauth::mojom::blink::ScopedCredentialType;
+using webauth::mojom::blink::Transport;
+
+// TODO(kpaulhamus): Make this a TypeConverter
+Vector<uint8_t> ConvertBufferSource(const blink::BufferSource& buffer) {
+ DCHECK(buffer.isNull());
+ Vector<uint8_t> vector;
+ if (buffer.isArrayBuffer()) {
+ vector.Append(static_cast<uint8_t*>(buffer.getAsArrayBuffer()->Data()),
+ buffer.getAsArrayBuffer()->ByteLength());
+ } else {
+ vector.Append(static_cast<uint8_t*>(
+ buffer.getAsArrayBufferView().View()->BaseAddress()),
+ buffer.getAsArrayBufferView().View()->byteLength());
+ }
+ return vector;
+}
+
+// TODO(kpaulhamus): Make this a TypeConverter
+ScopedCredentialType ConvertScopedCredentialType(const String& cred_type) {
+ if (cred_type == "ScopedCred")
+ return ScopedCredentialType::SCOPEDCRED;
+ NOTREACHED();
+ return ScopedCredentialType::SCOPEDCRED;
+}
+
+// TODO(kpaulhamus): Make this a TypeConverter
+Transport ConvertTransport(const String& transport) {
+ if (transport == "usb")
+ return Transport::USB;
+ if (transport == "nfc")
+ return Transport::NFC;
+ if (transport == "ble")
+ return Transport::BLE;
+ NOTREACHED();
+ return Transport::USB;
+}
+
+RelyingPartyAccountPtr ConvertRelyingPartyAccount(
+ const blink::RelyingPartyAccount& account_information,
+ blink::ScriptPromiseResolver* resolver) {
+ auto mojoAccount = RelyingPartyAccount::New();
+
+ mojoAccount->relying_party_display_name = account_information.rpDisplayName();
+ mojoAccount->display_name = account_information.displayName();
+ mojoAccount->id = account_information.id();
+ mojoAccount->name = account_information.name();
+ mojoAccount->image_url = account_information.imageURL();
+ return mojoAccount;
+}
+
+// TODO(kpaulhamus): Make this a TypeConverter
+ScopedCredentialOptionsPtr ConvertScopedCredentialOptions(
+ const blink::ScopedCredentialOptions options,
+ blink::ScriptPromiseResolver* resolver) {
+ auto mojoOptions = ScopedCredentialOptions::New();
+ mojoOptions->timeout_seconds = options.timeoutSeconds();
+ mojoOptions->relying_party_id = options.rpId();
+
+ // Adds the excludeList members (which are ScopedCredentialDescriptors)
+ for (const auto& descriptor : options.excludeList()) {
+ auto mojoDescriptor = ScopedCredentialDescriptor::New();
+ mojoDescriptor->type = ConvertScopedCredentialType(descriptor.type());
+ mojoDescriptor->id = ConvertBufferSource(descriptor.id());
+ for (const auto& transport : descriptor.transports())
+ mojoDescriptor->transports.push_back(ConvertTransport(transport));
+ mojoOptions->exclude_list.push_back(std::move(mojoDescriptor));
+ }
+ // TODO(kpaulhamus): add AuthenticationExtensions;
+ return mojoOptions;
+}
+
+// TODO(kpaulhamus): Make this a TypeConverter
+ScopedCredentialParametersPtr ConvertScopedCredentialParameter(
+ const blink::ScopedCredentialParameters parameter,
+ blink::ScriptPromiseResolver* resolver) {
+ auto mojoParameter = ScopedCredentialParameters::New();
+ mojoParameter->type = ConvertScopedCredentialType(parameter.type());
+ // TODO(kpaulhamus): add AlgorithmIdentifier
+ return mojoParameter;
+}
+} // namespace mojo
namespace blink {
-WebAuthentication::WebAuthentication(LocalFrame& frame) {}
+WebAuthentication::WebAuthentication(LocalFrame& frame)
+ : ContextLifecycleObserver(frame.GetDocument()) {
+ frame.GetInterfaceProvider()->GetInterface(
+ mojo::MakeRequest(&authenticator_));
+ authenticator_.set_connection_error_handler(ConvertToBaseCallback(
+ WTF::Bind(&WebAuthentication::OnAuthenticatorConnectionError,
+ WrapWeakPersistent(this))));
+}
-WebAuthentication::~WebAuthentication() {}
+WebAuthentication::~WebAuthentication() {
+ // |authenticator_| may still be valid but there should be no more
+ // outstanding requests because each holds a persistent handle to this object.
+ DCHECK(authenticatorRequests_.IsEmpty());
+}
void WebAuthentication::Dispose() {}
-ScriptPromise WebAuthentication::makeCredential(
+ScriptPromise WebAuthentication::MakeCredential(
ScriptState* script_state,
const RelyingPartyAccount& account_information,
const HeapVector<ScopedCredentialParameters> crypto_parameters,
const BufferSource& attestation_challenge,
ScopedCredentialOptions& options) {
- NOTREACHED();
- return ScriptPromise();
+ if (!authenticator_) {
+ return ScriptPromise::RejectWithDOMException(
+ script_state, DOMException::Create(kNotSupportedError));
+ }
+
+ ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state);
+ ScriptPromise promise = resolver->Promise();
+
+ // TODO(kpaulhamus) validate parameters according to spec
+ auto account =
+ mojo::ConvertRelyingPartyAccount(account_information, resolver);
+ Vector<uint8_t> buffer = mojo::ConvertBufferSource(attestation_challenge);
+ auto opts = mojo::ConvertScopedCredentialOptions(options, resolver);
+ Vector<webauth::mojom::blink::ScopedCredentialParametersPtr> parameters;
+ for (const auto& parameter : crypto_parameters) {
+ parameters.push_back(
+ mojo::ConvertScopedCredentialParameter(parameter, resolver));
+ }
+
+ authenticatorRequests_.insert(resolver);
+ authenticator_->MakeCredential(
+ std::move(account), std::move(parameters), buffer, std::move(opts),
+ ConvertToBaseCallback(Bind(&WebAuthentication::OnMakeCredential,
+ WrapPersistent(this),
+ WrapPersistent(resolver))));
+ return promise;
}
-ScriptPromise WebAuthentication::getAssertion(
+ScriptPromise WebAuthentication::GetAssertion(
ScriptState* script_state,
const BufferSource& assertion_challenge,
const AuthenticationAssertionOptions& options) {
@@ -32,4 +175,59 @@ ScriptPromise WebAuthentication::getAssertion(
return ScriptPromise();
}
+void WebAuthentication::ContextDestroyed(ExecutionContext*) {
+ authenticator_.reset();
+ authenticatorRequests_.clear();
+}
+
+void WebAuthentication::OnAuthenticatorConnectionError() {
+ authenticator_.reset();
+ for (ScriptPromiseResolver* resolver : authenticatorRequests_) {
+ resolver->Reject(
+ DOMException::Create(kNotFoundError, kNoAuthenticatorError));
+ }
+ authenticatorRequests_.clear();
+}
+
+void WebAuthentication::OnMakeCredential(
+ ScriptPromiseResolver* resolver,
+ Vector<webauth::mojom::blink::ScopedCredentialInfoPtr> credentials) {
+ if (!MarkRequestComplete(resolver))
+ return;
+
+ HeapVector<Member<ScopedCredentialInfo>> scopedCredentials;
+ for (auto& credential : credentials) {
+ if (credential->client_data.IsEmpty() ||
+ credential->attestation.IsEmpty()) {
+ resolver->Reject(
+ DOMException::Create(kNotFoundError, "No credentials returned."));
+ }
+ DOMArrayBuffer* clientDataBuffer = DOMArrayBuffer::Create(
+ static_cast<void*>(&credential->client_data.front()),
+ credential->client_data.size());
+
+ DOMArrayBuffer* attestationBuffer = DOMArrayBuffer::Create(
+ static_cast<void*>(&credential->attestation.front()),
+ credential->attestation.size());
+
+ scopedCredentials.push_back(
+ ScopedCredentialInfo::Create(clientDataBuffer, attestationBuffer));
+ }
+ resolver->Resolve();
+ authenticatorRequests_.erase(resolver);
+}
+
+bool WebAuthentication::MarkRequestComplete(ScriptPromiseResolver* resolver) {
+ auto requestEntry = authenticatorRequests_.find(resolver);
+ if (requestEntry == authenticatorRequests_.end())
+ return false;
+ authenticatorRequests_.erase(requestEntry);
+ return true;
+}
+
+DEFINE_TRACE(WebAuthentication) {
+ visitor->Trace(authenticatorRequests_);
+ ContextLifecycleObserver::Trace(visitor);
+}
+
} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698