Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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> | |
| 8 | |
| 7 #include "bindings/core/v8/ScriptPromise.h" | 9 #include "bindings/core/v8/ScriptPromise.h" |
| 10 #include "bindings/core/v8/ScriptPromiseResolver.h" | |
| 11 #include "core/dom/DOMException.h" | |
| 12 #include "core/dom/Document.h" | |
| 13 #include "core/dom/ExceptionCode.h" | |
| 14 #include "core/frame/LocalFrame.h" | |
| 15 #include "modules/webauth/RelyingPartyAccount.h" | |
| 16 #include "modules/webauth/ScopedCredential.h" | |
| 17 #include "modules/webauth/ScopedCredentialOptions.h" | |
| 18 #include "modules/webauth/ScopedCredentialParameters.h" | |
| 19 #include "public/platform/InterfaceProvider.h" | |
| 20 | |
| 21 namespace { | |
| 22 const char kNoAuthenticatorError[] = "Authenticator unavailable."; | |
| 23 } // anonymous namespace | |
| 24 | |
| 25 namespace mojo { | |
| 26 | |
| 27 using webauth::mojom::blink::RelyingPartyAccount; | |
| 28 using webauth::mojom::blink::RelyingPartyAccountPtr; | |
| 29 using webauth::mojom::blink::ScopedCredentialOptions; | |
| 30 using webauth::mojom::blink::ScopedCredentialOptionsPtr; | |
| 31 using webauth::mojom::blink::ScopedCredentialParameters; | |
| 32 using webauth::mojom::blink::ScopedCredentialParametersPtr; | |
| 33 using webauth::mojom::blink::ScopedCredentialDescriptor; | |
| 34 using webauth::mojom::blink::ScopedCredentialType; | |
| 35 using webauth::mojom::blink::Transport; | |
| 36 | |
| 37 // TODO(kpaulhamus): Make this a TypeConverter | |
| 38 Vector<uint8_t> ConvertBufferSource(const blink::BufferSource& buffer) { | |
| 39 DCHECK(buffer.isNull()); | |
| 40 Vector<uint8_t> vector; | |
| 41 if (buffer.isArrayBuffer()) { | |
| 42 vector.Append(static_cast<uint8_t*>(buffer.getAsArrayBuffer()->Data()), | |
| 43 buffer.getAsArrayBuffer()->ByteLength()); | |
| 44 } else { | |
| 45 vector.Append(static_cast<uint8_t*>( | |
| 46 buffer.getAsArrayBufferView().View()->BaseAddress()), | |
| 47 buffer.getAsArrayBufferView().View()->byteLength()); | |
| 48 } | |
| 49 return vector; | |
| 50 } | |
| 51 | |
| 52 // TODO(kpaulhamus): Make this a TypeConverter | |
| 53 ScopedCredentialType ConvertScopedCredentialType(const String& cred_type) { | |
| 54 if (cred_type == "ScopedCred") | |
| 55 return ScopedCredentialType::SCOPEDCRED; | |
| 56 NOTREACHED(); | |
| 57 return ScopedCredentialType::SCOPEDCRED; | |
| 58 } | |
| 59 | |
| 60 // TODO(kpaulhamus): Make this a TypeConverter | |
| 61 Transport ConvertTransport(const String& transport) { | |
| 62 if (transport == "usb") | |
| 63 return Transport::USB; | |
| 64 if (transport == "nfc") | |
| 65 return Transport::NFC; | |
| 66 if (transport == "ble") | |
| 67 return Transport::BLE; | |
| 68 NOTREACHED(); | |
| 69 return Transport::USB; | |
| 70 } | |
| 71 | |
| 72 RelyingPartyAccountPtr ConvertRelyingPartyAccount( | |
| 73 const blink::RelyingPartyAccount& account_information, | |
| 74 blink::ScriptPromiseResolver* resolver) { | |
| 75 auto mojo_account = RelyingPartyAccount::New(); | |
| 76 | |
| 77 mojo_account->relying_party_display_name = | |
| 78 account_information.rpDisplayName(); | |
| 79 mojo_account->display_name = account_information.displayName(); | |
| 80 mojo_account->id = account_information.id(); | |
| 81 mojo_account->name = account_information.name(); | |
| 82 mojo_account->image_url = account_information.imageURL(); | |
| 83 return mojo_account; | |
| 84 } | |
| 85 | |
| 86 // TODO(kpaulhamus): Make this a TypeConverter | |
| 87 ScopedCredentialOptionsPtr ConvertScopedCredentialOptions( | |
| 88 const blink::ScopedCredentialOptions options, | |
| 89 blink::ScriptPromiseResolver* resolver) { | |
| 90 auto mojo_options = ScopedCredentialOptions::New(); | |
| 91 mojo_options->timeout_seconds = options.timeoutSeconds(); | |
| 92 mojo_options->relying_party_id = options.rpId(); | |
| 93 | |
| 94 // Adds the excludeList members (which are ScopedCredentialDescriptors) | |
| 95 for (const auto& descriptor : options.excludeList()) { | |
| 96 auto mojo_descriptor = ScopedCredentialDescriptor::New(); | |
| 97 mojo_descriptor->type = ConvertScopedCredentialType(descriptor.type()); | |
| 98 mojo_descriptor->id = ConvertBufferSource(descriptor.id()); | |
| 99 for (const auto& transport : descriptor.transports()) | |
| 100 mojo_descriptor->transports.push_back(ConvertTransport(transport)); | |
| 101 mojo_options->exclude_list.push_back(std::move(mojo_descriptor)); | |
| 102 } | |
| 103 // TODO(kpaulhamus): add AuthenticationExtensions; | |
| 104 return mojo_options; | |
| 105 } | |
| 106 | |
| 107 // TODO(kpaulhamus): Make this a TypeConverter | |
| 108 ScopedCredentialParametersPtr ConvertScopedCredentialParameter( | |
| 109 const blink::ScopedCredentialParameters parameter, | |
| 110 blink::ScriptPromiseResolver* resolver) { | |
| 111 auto mojo_parameter = ScopedCredentialParameters::New(); | |
| 112 mojo_parameter->type = ConvertScopedCredentialType(parameter.type()); | |
| 113 // TODO(kpaulhamus): add AlgorithmIdentifier | |
| 114 return mojo_parameter; | |
| 115 } | |
| 116 } // namespace mojo | |
| 8 | 117 |
| 9 namespace blink { | 118 namespace blink { |
| 10 | 119 |
| 11 WebAuthentication::WebAuthentication(LocalFrame& frame) {} | 120 WebAuthentication::WebAuthentication(LocalFrame& frame) |
| 121 : 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 } | |
| 12 | 128 |
| 13 WebAuthentication::~WebAuthentication() {} | 129 WebAuthentication::~WebAuthentication() { |
| 130 // |authenticator_| may still be valid but there should be no more | |
| 131 // outstanding requests because each holds a persistent handle to this object. | |
| 132 DCHECK(authenticator_requests_.IsEmpty()); | |
| 133 } | |
| 14 | 134 |
| 15 void WebAuthentication::Dispose() {} | 135 void WebAuthentication::Dispose() {} |
| 16 | 136 |
| 17 ScriptPromise WebAuthentication::makeCredential( | 137 ScriptPromise WebAuthentication::makeCredential( |
| 18 ScriptState* script_state, | 138 ScriptState* script_state, |
| 19 const RelyingPartyAccount& account_information, | 139 const RelyingPartyAccount& account_information, |
| 20 const HeapVector<ScopedCredentialParameters> crypto_parameters, | 140 const HeapVector<ScopedCredentialParameters> crypto_parameters, |
| 21 const BufferSource& attestation_challenge, | 141 const BufferSource& attestation_challenge, |
| 22 ScopedCredentialOptions& options) { | 142 ScopedCredentialOptions& options) { |
| 23 NOTREACHED(); | 143 if (!authenticator_) { |
| 24 return ScriptPromise(); | 144 return ScriptPromise::RejectWithDOMException( |
| 145 script_state, DOMException::Create(kNotSupportedError)); | |
| 146 } | |
| 147 | |
| 148 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | |
| 149 ScriptPromise promise = resolver->Promise(); | |
| 150 | |
| 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); | |
| 155 auto opts = mojo::ConvertScopedCredentialOptions(options, resolver); | |
| 156 Vector<webauth::mojom::blink::ScopedCredentialParametersPtr> parameters; | |
| 157 for (const auto& parameter : crypto_parameters) { | |
| 158 parameters.push_back( | |
| 159 mojo::ConvertScopedCredentialParameter(parameter, resolver)); | |
| 160 } | |
| 161 | |
| 162 authenticator_requests_.insert(resolver); | |
| 163 authenticator_->MakeCredential( | |
| 164 std::move(account), std::move(parameters), buffer, std::move(opts), | |
| 165 ConvertToBaseCallback(Bind(&WebAuthentication::OnMakeCredential, | |
| 166 WrapPersistent(this), | |
| 167 WrapPersistent(resolver)))); | |
| 168 return promise; | |
| 25 } | 169 } |
| 26 | 170 |
| 27 ScriptPromise WebAuthentication::getAssertion( | 171 ScriptPromise WebAuthentication::getAssertion( |
| 28 ScriptState* script_state, | 172 ScriptState* script_state, |
| 29 const BufferSource& assertion_challenge, | 173 const BufferSource& assertion_challenge, |
| 30 const AuthenticationAssertionOptions& options) { | 174 const AuthenticationAssertionOptions& options) { |
| 31 NOTREACHED(); | 175 NOTREACHED(); |
| 32 return ScriptPromise(); | 176 return ScriptPromise(); |
| 33 } | 177 } |
| 34 | 178 |
| 179 void WebAuthentication::ContextDestroyed(ExecutionContext*) { | |
| 180 authenticator_.reset(); | |
|
ikilpatrick
2017/05/09 20:48:58
nothing is needed here except for maybe a TODO
bu
kpaulhamus
2017/05/10 17:58:29
Talked with jyasskin about this and it sounds like
| |
| 181 authenticator_requests_.clear(); | |
| 182 } | |
| 183 | |
| 184 void WebAuthentication::OnAuthenticatorConnectionError() { | |
| 185 authenticator_.reset(); | |
| 186 for (ScriptPromiseResolver* resolver : authenticator_requests_) { | |
| 187 resolver->Reject( | |
| 188 DOMException::Create(kNotFoundError, kNoAuthenticatorError)); | |
| 189 } | |
| 190 authenticator_requests_.clear(); | |
| 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 authenticator_requests_.erase(resolver); | |
|
ikilpatrick
2017/05/09 20:48:58
this isn't needed? (done in MarkRequestComplete?)
kpaulhamus
2017/05/10 17:58:29
Done.
| |
| 219 } | |
| 220 | |
| 221 bool WebAuthentication::MarkRequestComplete(ScriptPromiseResolver* resolver) { | |
| 222 auto request_entry = authenticator_requests_.find(resolver); | |
| 223 if (request_entry == authenticator_requests_.end()) | |
| 224 return false; | |
| 225 authenticator_requests_.erase(request_entry); | |
| 226 return true; | |
| 227 } | |
| 228 | |
| 229 DEFINE_TRACE(WebAuthentication) { | |
| 230 visitor->Trace(authenticator_requests_); | |
| 231 ContextLifecycleObserver::Trace(visitor); | |
| 232 } | |
| 233 | |
| 35 } // namespace blink | 234 } // namespace blink |
| OLD | NEW |