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

Side by Side Diff: third_party/WebKit/Source/modules/webauth/WebAuthentication.cpp

Issue 2788823002: Add the Mojo implementation of authenticator.mojom's MakeCredential. (Closed)
Patch Set: Addressing mkwst comments Created 3 years, 6 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 unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "modules/webauth/WebAuthentication.h" 5 #include "modules/webauth/WebAuthentication.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include "bindings/core/v8/ScriptPromise.h" 9 #include "bindings/core/v8/ScriptPromise.h"
10 #include "bindings/core/v8/ScriptPromiseResolver.h" 10 #include "bindings/core/v8/ScriptPromiseResolver.h"
11 #include "core/dom/DOMException.h" 11 #include "core/dom/DOMException.h"
12 #include "core/dom/Document.h" 12 #include "core/dom/Document.h"
13 #include "core/dom/ExceptionCode.h" 13 #include "core/dom/ExceptionCode.h"
14 #include "core/frame/LocalFrame.h" 14 #include "core/frame/LocalFrame.h"
15 #include "modules/webauth/RelyingPartyAccount.h" 15 #include "modules/webauth/RelyingPartyAccount.h"
16 #include "modules/webauth/ScopedCredential.h" 16 #include "modules/webauth/ScopedCredential.h"
17 #include "modules/webauth/ScopedCredentialOptions.h" 17 #include "modules/webauth/ScopedCredentialOptions.h"
18 #include "modules/webauth/ScopedCredentialParameters.h" 18 #include "modules/webauth/ScopedCredentialParameters.h"
19 #include "public/platform/InterfaceProvider.h" 19 #include "public/platform/InterfaceProvider.h"
20 20
21 namespace { 21 namespace {
22 const char kNoAuthenticatorError[] = "Authenticator unavailable."; 22 const char kNoAuthenticatorError[] = "Authenticator unavailable.";
23 // Time to wait for an authenticator to successfully complete an operation.
24 static const int kAdjustedTimeoutLower = 60;
25 static const int kAdjustedTimeoutUpper = 120;
23 } // anonymous namespace 26 } // anonymous namespace
24 27
25 namespace mojo { 28 namespace mojo {
26
27 using webauth::mojom::blink::RelyingPartyAccount; 29 using webauth::mojom::blink::RelyingPartyAccount;
28 using webauth::mojom::blink::RelyingPartyAccountPtr; 30 using webauth::mojom::blink::RelyingPartyAccountPtr;
31 using webauth::mojom::blink::AuthenticatorStatus;
32 using webauth::mojom::blink::ScopedCredentialDescriptor;
29 using webauth::mojom::blink::ScopedCredentialOptions; 33 using webauth::mojom::blink::ScopedCredentialOptions;
30 using webauth::mojom::blink::ScopedCredentialOptionsPtr; 34 using webauth::mojom::blink::ScopedCredentialOptionsPtr;
31 using webauth::mojom::blink::ScopedCredentialParameters; 35 using webauth::mojom::blink::ScopedCredentialParameters;
32 using webauth::mojom::blink::ScopedCredentialParametersPtr; 36 using webauth::mojom::blink::ScopedCredentialParametersPtr;
33 using webauth::mojom::blink::ScopedCredentialDescriptor;
34 using webauth::mojom::blink::ScopedCredentialType; 37 using webauth::mojom::blink::ScopedCredentialType;
35 using webauth::mojom::blink::Transport; 38 using webauth::mojom::blink::Transport;
36 39
37 // TODO(kpaulhamus): Make this a TypeConverter 40 // TODO(kpaulhamus): Make this a TypeConverter
38 Vector<uint8_t> ConvertBufferSource(const blink::BufferSource& buffer) { 41 Vector<uint8_t> ConvertBufferSource(const blink::BufferSource& buffer) {
39 DCHECK(buffer.isNull()); 42 DCHECK(!buffer.isNull());
40 Vector<uint8_t> vector; 43 Vector<uint8_t> vector;
41 if (buffer.isArrayBuffer()) { 44 if (buffer.isArrayBuffer()) {
42 vector.Append(static_cast<uint8_t*>(buffer.getAsArrayBuffer()->Data()), 45 vector.Append(static_cast<uint8_t*>(buffer.getAsArrayBuffer()->Data()),
43 buffer.getAsArrayBuffer()->ByteLength()); 46 buffer.getAsArrayBuffer()->ByteLength());
44 } else { 47 } else {
45 vector.Append(static_cast<uint8_t*>( 48 vector.Append(static_cast<uint8_t*>(
46 buffer.getAsArrayBufferView().View()->BaseAddress()), 49 buffer.getAsArrayBufferView().View()->BaseAddress()),
47 buffer.getAsArrayBufferView().View()->byteLength()); 50 buffer.getAsArrayBufferView().View()->byteLength());
48 } 51 }
49 return vector; 52 return vector;
(...skipping 12 matching lines...) Expand all
62 if (transport == "usb") 65 if (transport == "usb")
63 return Transport::USB; 66 return Transport::USB;
64 if (transport == "nfc") 67 if (transport == "nfc")
65 return Transport::NFC; 68 return Transport::NFC;
66 if (transport == "ble") 69 if (transport == "ble")
67 return Transport::BLE; 70 return Transport::BLE;
68 NOTREACHED(); 71 NOTREACHED();
69 return Transport::USB; 72 return Transport::USB;
70 } 73 }
71 74
75 // TODO(kpaulhamus): Make this a TypeConverter
72 RelyingPartyAccountPtr ConvertRelyingPartyAccount( 76 RelyingPartyAccountPtr ConvertRelyingPartyAccount(
73 const blink::RelyingPartyAccount& account_information, 77 const blink::RelyingPartyAccount& account_information,
74 blink::ScriptPromiseResolver* resolver) { 78 blink::ScriptPromiseResolver* resolver) {
75 auto mojo_account = RelyingPartyAccount::New(); 79 auto mojo_account = RelyingPartyAccount::New();
76 80
77 mojo_account->relying_party_display_name = 81 mojo_account->relying_party_display_name =
78 account_information.rpDisplayName(); 82 account_information.rpDisplayName();
79 mojo_account->display_name = account_information.displayName(); 83 mojo_account->display_name = account_information.displayName();
80 mojo_account->id = account_information.id(); 84 mojo_account->id = account_information.id();
81 mojo_account->name = account_information.name(); 85 mojo_account->name = account_information.name();
82 mojo_account->image_url = account_information.imageURL(); 86 mojo_account->image_url = account_information.imageURL();
83 return mojo_account; 87 return mojo_account;
84 } 88 }
85 89
86 // TODO(kpaulhamus): Make this a TypeConverter 90 // TODO(kpaulhamus): Make this a TypeConverter
87 ScopedCredentialOptionsPtr ConvertScopedCredentialOptions( 91 ScopedCredentialOptionsPtr ConvertScopedCredentialOptions(
88 const blink::ScopedCredentialOptions options, 92 const blink::ScopedCredentialOptions options,
89 blink::ScriptPromiseResolver* resolver) { 93 blink::ScriptPromiseResolver* resolver) {
90 auto mojo_options = ScopedCredentialOptions::New(); 94 auto mojo_options = ScopedCredentialOptions::New();
91 mojo_options->timeout_seconds = options.timeoutSeconds(); 95 if (options.hasRpId()) {
92 mojo_options->relying_party_id = options.rpId(); 96 mojo_options->relying_party_id = options.rpId();
97 }
93 98
94 // Adds the excludeList members (which are ScopedCredentialDescriptors) 99 // Step 4 of https://w3c.github.io/webauthn/#createCredential
95 for (const auto& descriptor : options.excludeList()) { 100 int predicted_timeout = kAdjustedTimeoutLower;
96 auto mojo_descriptor = ScopedCredentialDescriptor::New(); 101 if (options.hasTimeoutSeconds()) {
97 mojo_descriptor->type = ConvertScopedCredentialType(descriptor.type()); 102 predicted_timeout = static_cast<int>(options.timeoutSeconds());
98 mojo_descriptor->id = ConvertBufferSource(descriptor.id()); 103 }
99 for (const auto& transport : descriptor.transports()) 104
100 mojo_descriptor->transports.push_back(ConvertTransport(transport)); 105 mojo_options->adjusted_timeout = static_cast<double>(
101 mojo_options->exclude_list.push_back(std::move(mojo_descriptor)); 106 std::max(kAdjustedTimeoutLower,
107 std::min(kAdjustedTimeoutUpper, predicted_timeout)));
108
109 if (options.hasExcludeList()) {
110 // Adds the excludeList members (which are ScopedCredentialDescriptors)
111 for (const auto& descriptor : options.excludeList()) {
112 auto mojo_descriptor = ScopedCredentialDescriptor::New();
113 mojo_descriptor->type = ConvertScopedCredentialType(descriptor.type());
114 mojo_descriptor->id = ConvertBufferSource(descriptor.id());
115 for (const auto& transport : descriptor.transports())
116 mojo_descriptor->transports.push_back(ConvertTransport(transport));
117 mojo_options->exclude_list.push_back(std::move(mojo_descriptor));
118 }
102 } 119 }
103 // TODO(kpaulhamus): add AuthenticationExtensions; 120 // TODO(kpaulhamus): add AuthenticationExtensions;
104 return mojo_options; 121 return mojo_options;
105 } 122 }
106 123
107 // TODO(kpaulhamus): Make this a TypeConverter 124 // TODO(kpaulhamus): Make this a TypeConverter
108 ScopedCredentialParametersPtr ConvertScopedCredentialParameter( 125 ScopedCredentialParametersPtr ConvertScopedCredentialParameter(
109 const blink::ScopedCredentialParameters parameter, 126 const blink::ScopedCredentialParameters parameter,
110 blink::ScriptPromiseResolver* resolver) { 127 blink::ScriptPromiseResolver* resolver) {
111 auto mojo_parameter = ScopedCredentialParameters::New(); 128 auto mojo_parameter = ScopedCredentialParameters::New();
112 mojo_parameter->type = ConvertScopedCredentialType(parameter.type()); 129 mojo_parameter->type = ConvertScopedCredentialType(parameter.type());
113 // TODO(kpaulhamus): add AlgorithmIdentifier 130 // TODO(kpaulhamus): add AlgorithmIdentifier
114 return mojo_parameter; 131 return mojo_parameter;
115 } 132 }
133
134 blink::DOMException* CreateExceptionFromStatus(AuthenticatorStatus status) {
135 switch (status) {
136 case AuthenticatorStatus::NOT_ALLOWED_ERROR:
137 return blink::DOMException::Create(blink::kNotAllowedError,
138 "Not allowed.");
139 case AuthenticatorStatus::NOT_SUPPORTED_ERROR:
140 return blink::DOMException::Create(
141 blink::kNotSupportedError,
142 "Parameters for this operation are not supported.");
143 case AuthenticatorStatus::SECURITY_ERROR:
144 return blink::DOMException::Create(blink::kSecurityError,
145 "The operation was not allowed.");
146 case AuthenticatorStatus::UNKNOWN_ERROR:
147 return blink::DOMException::Create(blink::kUnknownError,
148 "Request failed.");
149 case AuthenticatorStatus::CANCELLED:
150 return blink::DOMException::Create(blink::kNotAllowedError,
151 "User canceled the operation.");
152 case AuthenticatorStatus::SUCCESS:
153 return nullptr;
154 default:
155 NOTREACHED();
156 return nullptr;
157 }
158 }
116 } // namespace mojo 159 } // namespace mojo
117 160
118 namespace blink { 161 namespace blink {
119
120 WebAuthentication::WebAuthentication(LocalFrame& frame) 162 WebAuthentication::WebAuthentication(LocalFrame& frame)
121 : ContextLifecycleObserver(frame.GetDocument()) { 163 : ContextLifecycleObserver(frame.GetDocument()) {}
122 frame.GetInterfaceProvider()->GetInterface(
123 mojo::MakeRequest(&authenticator_));
124 authenticator_.set_connection_error_handler(ConvertToBaseCallback(
125 WTF::Bind(&WebAuthentication::OnAuthenticatorConnectionError,
126 WrapWeakPersistent(this))));
127 }
128 164
129 WebAuthentication::~WebAuthentication() { 165 WebAuthentication::~WebAuthentication() {
130 // |authenticator_| may still be valid but there should be no more 166 // |authenticator_| may still be valid but there should be no more
131 // outstanding requests because each holds a persistent handle to this object. 167 // outstanding requests because each holds a persistent handle to this object.
132 DCHECK(authenticator_requests_.IsEmpty()); 168 DCHECK(authenticator_requests_.IsEmpty());
133 } 169 }
134 170
135 void WebAuthentication::Dispose() {}
136
137 ScriptPromise WebAuthentication::makeCredential( 171 ScriptPromise WebAuthentication::makeCredential(
138 ScriptState* script_state, 172 ScriptState* script_state,
139 const RelyingPartyAccount& account_information, 173 const RelyingPartyAccount& account_information,
140 const HeapVector<ScopedCredentialParameters> crypto_parameters, 174 const HeapVector<ScopedCredentialParameters> crypto_parameters,
141 const BufferSource& attestation_challenge, 175 const BufferSource& attestation_challenge,
142 ScopedCredentialOptions& options) { 176 ScopedCredentialOptions& options) {
143 if (!authenticator_) { 177 ScriptPromise promise = RejectIfNotSupported(script_state);
144 return ScriptPromise::RejectWithDOMException( 178 if (!promise.IsEmpty())
145 script_state, DOMException::Create(kNotSupportedError)); 179 return promise;
146 }
147 180
148 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); 181 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state);
149 ScriptPromise promise = resolver->Promise();
150 182
151 // TODO(kpaulhamus) validate parameters according to spec
152 auto account =
153 mojo::ConvertRelyingPartyAccount(account_information, resolver);
154 Vector<uint8_t> buffer = mojo::ConvertBufferSource(attestation_challenge); 183 Vector<uint8_t> buffer = mojo::ConvertBufferSource(attestation_challenge);
155 auto opts = mojo::ConvertScopedCredentialOptions(options, resolver); 184 auto opts = mojo::ConvertScopedCredentialOptions(options, resolver);
156 Vector<webauth::mojom::blink::ScopedCredentialParametersPtr> parameters; 185 Vector<webauth::mojom::blink::ScopedCredentialParametersPtr> parameters;
157 for (const auto& parameter : crypto_parameters) { 186 for (const auto& parameter : crypto_parameters) {
158 parameters.push_back( 187 if (parameter.hasType()) {
159 mojo::ConvertScopedCredentialParameter(parameter, resolver)); 188 parameters.push_back(
189 mojo::ConvertScopedCredentialParameter(parameter, resolver));
190 }
160 } 191 }
161 192 auto account =
193 mojo::ConvertRelyingPartyAccount(account_information, resolver);
162 authenticator_requests_.insert(resolver); 194 authenticator_requests_.insert(resolver);
163 authenticator_->MakeCredential( 195 authenticator_->MakeCredential(
164 std::move(account), std::move(parameters), buffer, std::move(opts), 196 std::move(account), std::move(parameters), buffer, std::move(opts),
165 ConvertToBaseCallback(Bind(&WebAuthentication::OnMakeCredential, 197 ConvertToBaseCallback(WTF::Bind(&WebAuthentication::OnMakeCredential,
166 WrapPersistent(this), 198 WrapPersistent(this),
167 WrapPersistent(resolver)))); 199 WrapPersistent(resolver))));
168 return promise; 200 return resolver->Promise();
169 } 201 }
170 202
171 ScriptPromise WebAuthentication::getAssertion( 203 ScriptPromise WebAuthentication::getAssertion(
172 ScriptState* script_state, 204 ScriptState* script_state,
173 const BufferSource& assertion_challenge, 205 const BufferSource& assertion_challenge,
174 const AuthenticationAssertionOptions& options) { 206 const AuthenticationAssertionOptions& options) {
175 NOTREACHED(); 207 NOTREACHED();
176 return ScriptPromise(); 208 return ScriptPromise();
177 } 209 }
178 210
179 void WebAuthentication::ContextDestroyed(ExecutionContext*) { 211 void WebAuthentication::ContextDestroyed(ExecutionContext*) {
180 authenticator_.reset(); 212 Cleanup();
181 authenticator_requests_.clear(); 213 }
214
215 void WebAuthentication::OnMakeCredential(
216 ScriptPromiseResolver* resolver,
217 webauth::mojom::blink::AuthenticatorStatus status,
218 webauth::mojom::blink::ScopedCredentialInfoPtr credential) {
219 if (!MarkRequestComplete(resolver))
220 return;
221
222 DOMException* error = mojo::CreateExceptionFromStatus(status);
223 if (error) {
224 resolver->Reject(error);
225 Cleanup();
226 return;
227 }
228
229 if (credential->client_data.IsEmpty() || credential->attestation.IsEmpty()) {
230 resolver->Reject(
231 DOMException::Create(kNotFoundError, "No credential returned."));
232 return;
233 }
234
235 DOMArrayBuffer* clientDataBuffer = DOMArrayBuffer::Create(
236 static_cast<void*>(&credential->client_data.front()),
237 credential->client_data.size());
238
239 DOMArrayBuffer* attestationBuffer = DOMArrayBuffer::Create(
240 static_cast<void*>(&credential->attestation.front()),
241 credential->attestation.size());
242
243 ScopedCredentialInfo* scopedCredential =
244 ScopedCredentialInfo::Create(clientDataBuffer, attestationBuffer);
245 resolver->Resolve(scopedCredential);
246 }
247
248 ScriptPromise WebAuthentication::RejectIfNotSupported(
249 ScriptState* script_state) {
250 if (!authenticator_) {
251 if (!GetFrame()) {
252 return ScriptPromise::RejectWithDOMException(
253 script_state, DOMException::Create(kNotSupportedError));
254 }
255 GetFrame()->GetInterfaceProvider()->GetInterface(
256 mojo::MakeRequest(&authenticator_));
257
258 authenticator_.set_connection_error_handler(ConvertToBaseCallback(
259 WTF::Bind(&WebAuthentication::OnAuthenticatorConnectionError,
260 WrapWeakPersistent(this))));
261 }
262 return ScriptPromise();
182 } 263 }
183 264
184 void WebAuthentication::OnAuthenticatorConnectionError() { 265 void WebAuthentication::OnAuthenticatorConnectionError() {
185 authenticator_.reset();
186 for (ScriptPromiseResolver* resolver : authenticator_requests_) { 266 for (ScriptPromiseResolver* resolver : authenticator_requests_) {
187 resolver->Reject( 267 resolver->Reject(
188 DOMException::Create(kNotFoundError, kNoAuthenticatorError)); 268 DOMException::Create(kNotFoundError, kNoAuthenticatorError));
189 } 269 }
190 authenticator_requests_.clear(); 270 Cleanup();
191 }
192
193 void WebAuthentication::OnMakeCredential(
194 ScriptPromiseResolver* resolver,
195 Vector<webauth::mojom::blink::ScopedCredentialInfoPtr> credentials) {
196 if (!MarkRequestComplete(resolver))
197 return;
198
199 HeapVector<Member<ScopedCredentialInfo>> scoped_credentials;
200 for (auto& credential : credentials) {
201 if (credential->client_data.IsEmpty() ||
202 credential->attestation.IsEmpty()) {
203 resolver->Reject(
204 DOMException::Create(kNotFoundError, "No credentials returned."));
205 }
206 DOMArrayBuffer* client_data_buffer = DOMArrayBuffer::Create(
207 static_cast<void*>(&credential->client_data.front()),
208 credential->client_data.size());
209
210 DOMArrayBuffer* attestation_buffer = DOMArrayBuffer::Create(
211 static_cast<void*>(&credential->attestation.front()),
212 credential->attestation.size());
213
214 scoped_credentials.push_back(
215 ScopedCredentialInfo::Create(client_data_buffer, attestation_buffer));
216 }
217 resolver->Resolve();
218 } 271 }
219 272
220 bool WebAuthentication::MarkRequestComplete(ScriptPromiseResolver* resolver) { 273 bool WebAuthentication::MarkRequestComplete(ScriptPromiseResolver* resolver) {
221 auto request_entry = authenticator_requests_.find(resolver); 274 auto request_entry = authenticator_requests_.find(resolver);
222 if (request_entry == authenticator_requests_.end()) 275 if (request_entry == authenticator_requests_.end())
223 return false; 276 return false;
224 authenticator_requests_.erase(request_entry); 277 authenticator_requests_.erase(request_entry);
225 return true; 278 return true;
226 } 279 }
227 280
228 DEFINE_TRACE(WebAuthentication) { 281 DEFINE_TRACE(WebAuthentication) {
229 visitor->Trace(authenticator_requests_); 282 visitor->Trace(authenticator_requests_);
230 ContextLifecycleObserver::Trace(visitor); 283 ContextLifecycleObserver::Trace(visitor);
231 } 284 }
232 285
286 // Clears the promise resolver, timer, and closes the Mojo connection.
287 void WebAuthentication::Cleanup() {
288 authenticator_.reset();
289 authenticator_requests_.clear();
290 }
291
233 } // namespace blink 292 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698