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

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: Migreate webauth in components/ and chrome/ to content/ Created 3 years, 5 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;
jochen (gone - plz use gerrit) 2017/06/28 07:38:30 nit. static isn't needed since the definition is a
kpaulhamus 2017/06/28 08:48:16 Done.
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 // if rpID is missing, it will later be set to the origin of the page
97 // in the secure browser process.
98 mojo_options->relying_party_id = options.rpId();
99 }
93 100
94 // Adds the excludeList members (which are ScopedCredentialDescriptors) 101 // Step 4 of https://w3c.github.io/webauthn/#createCredential
95 for (const auto& descriptor : options.excludeList()) { 102 int predicted_timeout = kAdjustedTimeoutLower;
96 auto mojo_descriptor = ScopedCredentialDescriptor::New(); 103 if (options.hasTimeoutSeconds()) {
97 mojo_descriptor->type = ConvertScopedCredentialType(descriptor.type()); 104 predicted_timeout = static_cast<int>(options.timeoutSeconds());
98 mojo_descriptor->id = ConvertBufferSource(descriptor.id()); 105 }
99 for (const auto& transport : descriptor.transports()) 106
100 mojo_descriptor->transports.push_back(ConvertTransport(transport)); 107 mojo_options->adjusted_timeout = static_cast<double>(
101 mojo_options->exclude_list.push_back(std::move(mojo_descriptor)); 108 std::max(kAdjustedTimeoutLower,
109 std::min(kAdjustedTimeoutUpper, predicted_timeout)));
110
111 if (options.hasExcludeList()) {
112 // Adds the excludeList members (which are ScopedCredentialDescriptors)
113 for (const auto& descriptor : options.excludeList()) {
114 auto mojo_descriptor = ScopedCredentialDescriptor::New();
115 mojo_descriptor->type = ConvertScopedCredentialType(descriptor.type());
116 mojo_descriptor->id = ConvertBufferSource(descriptor.id());
117 for (const auto& transport : descriptor.transports())
118 mojo_descriptor->transports.push_back(ConvertTransport(transport));
119 mojo_options->exclude_list.push_back(std::move(mojo_descriptor));
120 }
102 } 121 }
103 // TODO(kpaulhamus): add AuthenticationExtensions; 122 // TODO(kpaulhamus): add AuthenticationExtensions;
104 return mojo_options; 123 return mojo_options;
105 } 124 }
106 125
107 // TODO(kpaulhamus): Make this a TypeConverter 126 // TODO(kpaulhamus): Make this a TypeConverter
108 ScopedCredentialParametersPtr ConvertScopedCredentialParameter( 127 ScopedCredentialParametersPtr ConvertScopedCredentialParameter(
109 const blink::ScopedCredentialParameters parameter, 128 const blink::ScopedCredentialParameters parameter,
110 blink::ScriptPromiseResolver* resolver) { 129 blink::ScriptPromiseResolver* resolver) {
111 auto mojo_parameter = ScopedCredentialParameters::New(); 130 auto mojo_parameter = ScopedCredentialParameters::New();
112 mojo_parameter->type = ConvertScopedCredentialType(parameter.type()); 131 mojo_parameter->type = ConvertScopedCredentialType(parameter.type());
113 // TODO(kpaulhamus): add AlgorithmIdentifier 132 // TODO(kpaulhamus): add AlgorithmIdentifier
114 return mojo_parameter; 133 return mojo_parameter;
115 } 134 }
135
136 blink::DOMException* CreateExceptionFromStatus(AuthenticatorStatus status) {
137 switch (status) {
138 case AuthenticatorStatus::NOT_IMPLEMENTED:
139 return blink::DOMException::Create(blink::kNotSupportedError,
140 "Not implemented.");
141 case AuthenticatorStatus::NOT_ALLOWED_ERROR:
142 return blink::DOMException::Create(blink::kNotAllowedError,
143 "Not allowed.");
144 case AuthenticatorStatus::NOT_SUPPORTED_ERROR:
145 return blink::DOMException::Create(
146 blink::kNotSupportedError,
147 "Parameters for this operation are not supported.");
148 case AuthenticatorStatus::SECURITY_ERROR:
149 return blink::DOMException::Create(blink::kSecurityError,
150 "The operation was not allowed.");
151 case AuthenticatorStatus::UNKNOWN_ERROR:
152 return blink::DOMException::Create(blink::kUnknownError,
153 "Request failed.");
154 case AuthenticatorStatus::CANCELLED:
155 return blink::DOMException::Create(blink::kNotAllowedError,
156 "User canceled the operation.");
157 case AuthenticatorStatus::SUCCESS:
158 return nullptr;
159 default:
160 NOTREACHED();
161 return nullptr;
162 }
163 }
116 } // namespace mojo 164 } // namespace mojo
117 165
118 namespace blink { 166 namespace blink {
119
120 WebAuthentication::WebAuthentication(LocalFrame& frame) 167 WebAuthentication::WebAuthentication(LocalFrame& frame)
121 : ContextLifecycleObserver(frame.GetDocument()) { 168 : 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 169
129 WebAuthentication::~WebAuthentication() { 170 WebAuthentication::~WebAuthentication() {
130 // |authenticator_| may still be valid but there should be no more 171 // |authenticator_| may still be valid but there should be no more
131 // outstanding requests because each holds a persistent handle to this object. 172 // outstanding requests because each holds a persistent handle to this object.
132 DCHECK(authenticator_requests_.IsEmpty()); 173 DCHECK(authenticator_requests_.IsEmpty());
133 } 174 }
134 175
135 void WebAuthentication::Dispose() {}
136
137 ScriptPromise WebAuthentication::makeCredential( 176 ScriptPromise WebAuthentication::makeCredential(
138 ScriptState* script_state, 177 ScriptState* script_state,
139 const RelyingPartyAccount& account_information, 178 const RelyingPartyAccount& account_information,
140 const HeapVector<ScopedCredentialParameters> crypto_parameters, 179 const HeapVector<ScopedCredentialParameters> crypto_parameters,
141 const BufferSource& attestation_challenge, 180 const BufferSource& attestation_challenge,
142 ScopedCredentialOptions& options) { 181 ScopedCredentialOptions& options) {
143 if (!authenticator_) { 182 ScriptPromise promise = RejectIfNotSupported(script_state);
144 return ScriptPromise::RejectWithDOMException( 183 if (!promise.IsEmpty())
145 script_state, DOMException::Create(kNotSupportedError)); 184 return promise;
146 }
147 185
148 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); 186 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state);
149 ScriptPromise promise = resolver->Promise();
150 187
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); 188 Vector<uint8_t> buffer = mojo::ConvertBufferSource(attestation_challenge);
155 auto opts = mojo::ConvertScopedCredentialOptions(options, resolver); 189 auto opts = mojo::ConvertScopedCredentialOptions(options, resolver);
156 Vector<webauth::mojom::blink::ScopedCredentialParametersPtr> parameters; 190 Vector<webauth::mojom::blink::ScopedCredentialParametersPtr> parameters;
157 for (const auto& parameter : crypto_parameters) { 191 for (const auto& parameter : crypto_parameters) {
158 parameters.push_back( 192 if (parameter.hasType()) {
159 mojo::ConvertScopedCredentialParameter(parameter, resolver)); 193 parameters.push_back(
194 mojo::ConvertScopedCredentialParameter(parameter, resolver));
195 }
160 } 196 }
161 197 auto account =
198 mojo::ConvertRelyingPartyAccount(account_information, resolver);
162 authenticator_requests_.insert(resolver); 199 authenticator_requests_.insert(resolver);
163 authenticator_->MakeCredential( 200 authenticator_->MakeCredential(
164 std::move(account), std::move(parameters), buffer, std::move(opts), 201 std::move(account), std::move(parameters), buffer, std::move(opts),
165 ConvertToBaseCallback(Bind(&WebAuthentication::OnMakeCredential, 202 ConvertToBaseCallback(WTF::Bind(&WebAuthentication::OnMakeCredential,
166 WrapPersistent(this), 203 WrapPersistent(this),
167 WrapPersistent(resolver)))); 204 WrapPersistent(resolver))));
168 return promise; 205 return resolver->Promise();
169 } 206 }
170 207
171 ScriptPromise WebAuthentication::getAssertion( 208 ScriptPromise WebAuthentication::getAssertion(
172 ScriptState* script_state, 209 ScriptState* script_state,
173 const BufferSource& assertion_challenge, 210 const BufferSource& assertion_challenge,
174 const AuthenticationAssertionOptions& options) { 211 const AuthenticationAssertionOptions& options) {
175 NOTREACHED(); 212 NOTREACHED();
176 return ScriptPromise(); 213 return ScriptPromise();
177 } 214 }
178 215
179 void WebAuthentication::ContextDestroyed(ExecutionContext*) { 216 void WebAuthentication::ContextDestroyed(ExecutionContext*) {
180 authenticator_.reset(); 217 Cleanup();
181 authenticator_requests_.clear(); 218 }
219
220 void WebAuthentication::OnMakeCredential(
221 ScriptPromiseResolver* resolver,
222 webauth::mojom::blink::AuthenticatorStatus status,
223 webauth::mojom::blink::ScopedCredentialInfoPtr credential) {
224 if (!MarkRequestComplete(resolver))
225 return;
226
227 DOMException* error = mojo::CreateExceptionFromStatus(status);
228 if (error) {
229 DCHECK(!credential);
230 resolver->Reject(error);
231 Cleanup();
232 return;
233 }
234
235 if (credential->client_data.IsEmpty() || credential->attestation.IsEmpty()) {
236 resolver->Reject(
237 DOMException::Create(kNotFoundError, "No credential returned."));
238 return;
239 }
240
241 DOMArrayBuffer* clientDataBuffer = DOMArrayBuffer::Create(
242 static_cast<void*>(&credential->client_data.front()),
243 credential->client_data.size());
244
245 DOMArrayBuffer* attestationBuffer = DOMArrayBuffer::Create(
246 static_cast<void*>(&credential->attestation.front()),
247 credential->attestation.size());
248
249 ScopedCredentialInfo* scopedCredential =
250 ScopedCredentialInfo::Create(clientDataBuffer, attestationBuffer);
251 resolver->Resolve(scopedCredential);
252 }
253
254 ScriptPromise WebAuthentication::RejectIfNotSupported(
255 ScriptState* script_state) {
256 if (!authenticator_) {
257 if (!GetFrame()) {
258 return ScriptPromise::RejectWithDOMException(
259 script_state, DOMException::Create(kNotSupportedError));
260 }
261 GetFrame()->GetInterfaceProvider()->GetInterface(
262 mojo::MakeRequest(&authenticator_));
263
264 authenticator_.set_connection_error_handler(ConvertToBaseCallback(
265 WTF::Bind(&WebAuthentication::OnAuthenticatorConnectionError,
266 WrapWeakPersistent(this))));
267 }
268 return ScriptPromise();
182 } 269 }
183 270
184 void WebAuthentication::OnAuthenticatorConnectionError() { 271 void WebAuthentication::OnAuthenticatorConnectionError() {
185 authenticator_.reset();
186 for (ScriptPromiseResolver* resolver : authenticator_requests_) { 272 for (ScriptPromiseResolver* resolver : authenticator_requests_) {
187 resolver->Reject( 273 resolver->Reject(
188 DOMException::Create(kNotFoundError, kNoAuthenticatorError)); 274 DOMException::Create(kNotFoundError, kNoAuthenticatorError));
189 } 275 }
190 authenticator_requests_.clear(); 276 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 } 277 }
219 278
220 bool WebAuthentication::MarkRequestComplete(ScriptPromiseResolver* resolver) { 279 bool WebAuthentication::MarkRequestComplete(ScriptPromiseResolver* resolver) {
221 auto request_entry = authenticator_requests_.find(resolver); 280 auto request_entry = authenticator_requests_.find(resolver);
222 if (request_entry == authenticator_requests_.end()) 281 if (request_entry == authenticator_requests_.end())
223 return false; 282 return false;
224 authenticator_requests_.erase(request_entry); 283 authenticator_requests_.erase(request_entry);
225 return true; 284 return true;
226 } 285 }
227 286
228 DEFINE_TRACE(WebAuthentication) { 287 DEFINE_TRACE(WebAuthentication) {
229 visitor->Trace(authenticator_requests_); 288 visitor->Trace(authenticator_requests_);
230 ContextLifecycleObserver::Trace(visitor); 289 ContextLifecycleObserver::Trace(visitor);
231 } 290 }
232 291
292 // Clears the promise resolver, timer, and closes the Mojo connection.
293 void WebAuthentication::Cleanup() {
294 authenticator_.reset();
295 authenticator_requests_.clear();
296 }
297
233 } // namespace blink 298 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698