| 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 "bindings/core/v8/ScriptPromise.h" | 7 #include "bindings/core/v8/ScriptPromise.h" |
| 8 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 9 #include "core/dom/DOMException.h" |
| 10 #include "core/dom/Document.h" |
| 11 #include "core/dom/ExceptionCode.h" |
| 12 #include "core/frame/LocalFrame.h" |
| 13 #include "core/frame/UseCounter.h" |
| 14 #include "modules/webauth/RelyingPartyAccount.h" |
| 15 #include "modules/webauth/ScopedCredential.h" |
| 16 #include "modules/webauth/ScopedCredentialOptions.h" |
| 17 #include "modules/webauth/ScopedCredentialParameters.h" |
| 18 #include "public/platform/InterfaceProvider.h" |
| 19 |
| 20 namespace { |
| 21 const char kNoAuthenticatorError[] = "Authenticator unavailable."; |
| 22 } // anonymous namespace |
| 23 |
| 24 namespace mojo { |
| 25 |
| 26 using webauth::mojom::blink::RelyingPartyAccount; |
| 27 using webauth::mojom::blink::RelyingPartyAccountPtr; |
| 28 using webauth::mojom::blink::ScopedCredentialOptions; |
| 29 using webauth::mojom::blink::ScopedCredentialOptionsPtr; |
| 30 using webauth::mojom::blink::ScopedCredentialParameters; |
| 31 using webauth::mojom::blink::ScopedCredentialParametersPtr; |
| 32 using webauth::mojom::blink::ScopedCredentialDescriptor; |
| 33 using webauth::mojom::blink::ScopedCredentialType; |
| 34 using webauth::mojom::blink::Transport; |
| 35 |
| 36 Vector<uint8_t> convertBufferSource(const blink::BufferSource& buffer) { |
| 37 DCHECK(buffer.isNull()); |
| 38 Vector<uint8_t> vector; |
| 39 if (buffer.isArrayBuffer()) { |
| 40 vector.append(static_cast<uint8_t*>(buffer.getAsArrayBuffer()->data()), |
| 41 buffer.getAsArrayBuffer()->byteLength()); |
| 42 } else { |
| 43 vector.append( |
| 44 static_cast<uint8_t*>(buffer.getAsArrayBufferView()->baseAddress()), |
| 45 buffer.getAsArrayBufferView()->byteLength()); |
| 46 } |
| 47 return vector; |
| 48 } |
| 49 |
| 50 ScopedCredentialType convertScopedCredentialType(const WTF::String& credType) { |
| 51 if (credType == "ScopedCred") |
| 52 return ScopedCredentialType::SCOPEDCRED; |
| 53 NOTREACHED(); |
| 54 return ScopedCredentialType::SCOPEDCRED; |
| 55 } |
| 56 |
| 57 Transport convertTransport(const WTF::String& transport) { |
| 58 if (transport == "usb") |
| 59 return Transport::USB; |
| 60 if (transport == "nfc") |
| 61 return Transport::NFC; |
| 62 if (transport == "ble") |
| 63 return Transport::BLE; |
| 64 NOTREACHED(); |
| 65 return Transport::USB; |
| 66 } |
| 67 |
| 68 RelyingPartyAccountPtr convertRelyingPartyAccount( |
| 69 const blink::RelyingPartyAccount& accountInformation, |
| 70 blink::ScriptPromiseResolver* resolver) { |
| 71 auto mojoAccount = RelyingPartyAccount::New(); |
| 72 |
| 73 mojoAccount->rp_display_name = accountInformation.rpDisplayName(); |
| 74 mojoAccount->display_name = accountInformation.displayName(); |
| 75 mojoAccount->id = accountInformation.id(); |
| 76 mojoAccount->name = accountInformation.name(); |
| 77 mojoAccount->image_url = accountInformation.imageURL(); |
| 78 return mojoAccount; |
| 79 } |
| 80 |
| 81 ScopedCredentialOptionsPtr convertScopedCredentialOptions( |
| 82 const blink::ScopedCredentialOptions options, |
| 83 blink::ScriptPromiseResolver* resolver) { |
| 84 auto mojoOptions = ScopedCredentialOptions::New(); |
| 85 mojoOptions->timeout_seconds = options.timeoutSeconds(); |
| 86 mojoOptions->rp_id = options.rpId(); |
| 87 |
| 88 // Adds the excludeList members (which are ScopedCredentialDescriptors) |
| 89 for (const auto& descriptor : options.excludeList()) { |
| 90 auto mojoDescriptor = ScopedCredentialDescriptor::New(); |
| 91 mojoDescriptor->type = convertScopedCredentialType(descriptor.type()); |
| 92 mojoDescriptor->id = convertBufferSource(descriptor.id()); |
| 93 for (const auto& transport : descriptor.transports()) |
| 94 mojoDescriptor->transports.push_back(convertTransport(transport)); |
| 95 mojoOptions->exclude_list.push_back(std::move(mojoDescriptor)); |
| 96 } |
| 97 // TODO (kpaulhamus) add AuthenticationExtensions; |
| 98 return mojoOptions; |
| 99 } |
| 100 |
| 101 ScopedCredentialParametersPtr convertScopedCredentialParameter( |
| 102 const blink::ScopedCredentialParameters parameter, |
| 103 blink::ScriptPromiseResolver* resolver) { |
| 104 auto mojoParameter = ScopedCredentialParameters::New(); |
| 105 mojoParameter->type = convertScopedCredentialType(parameter.type()); |
| 106 // TODO (kpaulhamus) add AlgorithmIdentifier |
| 107 return mojoParameter; |
| 108 } |
| 109 } // namespace mojo |
| 8 | 110 |
| 9 namespace blink { | 111 namespace blink { |
| 10 | 112 |
| 11 WebAuthentication::WebAuthentication(LocalFrame& frame) {} | 113 WebAuthentication::WebAuthentication(LocalFrame& frame) |
| 114 : ContextLifecycleObserver(frame.document()) { |
| 115 frame.interfaceProvider()->getInterface(mojo::MakeRequest(&m_authenticator)); |
| 116 m_authenticator.set_connection_error_handler(convertToBaseCallback( |
| 117 WTF::bind(&WebAuthentication::onAuthenticatorConnectionError, |
| 118 wrapWeakPersistent(this)))); |
| 119 } |
| 12 | 120 |
| 13 WebAuthentication::~WebAuthentication() {} | 121 WebAuthentication::~WebAuthentication() { |
| 122 // |m_authenticator| may still be valid but there should be no more |
| 123 // outstanding requests because each holds a persistent handle to this object. |
| 124 DCHECK(m_authenticatorRequests.isEmpty()); |
| 125 } |
| 14 | 126 |
| 15 void WebAuthentication::dispose() {} | 127 void WebAuthentication::dispose() {} |
| 16 | 128 |
| 17 ScriptPromise WebAuthentication::makeCredential( | 129 ScriptPromise WebAuthentication::makeCredential( |
| 18 ScriptState* scriptState, | 130 ScriptState* scriptState, |
| 19 const RelyingPartyAccount& accountInformation, | 131 const RelyingPartyAccount& accountInformation, |
| 20 const HeapVector<ScopedCredentialParameters> cryptoParameters, | 132 const HeapVector<ScopedCredentialParameters> cryptoParameters, |
| 21 const BufferSource& attestationChallenge, | 133 const BufferSource& attestationChallenge, |
| 22 ScopedCredentialOptions& options) { | 134 ScopedCredentialOptions& options) { |
| 23 NOTREACHED(); | 135 ExecutionContext* executionContext = scriptState->getExecutionContext(); |
| 24 return ScriptPromise(); | 136 UseCounter::count(executionContext, UseCounter::AuthenticationMakeCredential); |
| 137 |
| 138 if (!m_authenticator) { |
| 139 return ScriptPromise::rejectWithDOMException( |
| 140 scriptState, DOMException::create(NotSupportedError)); |
| 141 } |
| 142 |
| 143 String errorMessage; |
| 144 if (!executionContext->isSecureContext(errorMessage)) { |
| 145 return ScriptPromise::rejectWithDOMException( |
| 146 scriptState, DOMException::create(SecurityError, errorMessage)); |
| 147 } |
| 148 |
| 149 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 150 ScriptPromise promise = resolver->promise(); |
| 151 |
| 152 // TODO(kpaulhamus) validate parameters according to spec |
| 153 auto account = mojo::convertRelyingPartyAccount(accountInformation, resolver); |
| 154 Vector<uint8_t> buffer = mojo::convertBufferSource(attestationChallenge); |
| 155 auto opts = mojo::convertScopedCredentialOptions(options, resolver); |
| 156 Vector<webauth::mojom::blink::ScopedCredentialParametersPtr> parameters; |
| 157 for (const auto& parameter : cryptoParameters) { |
| 158 parameters.push_back( |
| 159 mojo::convertScopedCredentialParameter(parameter, resolver)); |
| 160 } |
| 161 |
| 162 m_authenticatorRequests.insert(resolver); |
| 163 m_authenticator->makeCredential( |
| 164 std::move(account), std::move(parameters), buffer, std::move(opts), |
| 165 convertToBaseCallback(WTF::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* scriptState, | 172 ScriptState* scriptState, |
| 29 const BufferSource& assertionChallenge, | 173 const BufferSource& assertionChallenge, |
| 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 m_authenticator.reset(); |
| 181 m_authenticatorRequests.clear(); |
| 182 } |
| 183 |
| 184 void WebAuthentication::onAuthenticatorConnectionError() { |
| 185 m_authenticator.reset(); |
| 186 for (ScriptPromiseResolver* resolver : m_authenticatorRequests) { |
| 187 resolver->reject( |
| 188 DOMException::create(NotFoundError, kNoAuthenticatorError)); |
| 189 } |
| 190 m_authenticatorRequests.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>> scopedCredentials; |
| 200 for (auto& credential : credentials) { |
| 201 if (credential->client_data.isEmpty() || |
| 202 credential->attestation.isEmpty()) { |
| 203 resolver->reject( |
| 204 DOMException::create(NotFoundError, "No credentials returned.")); |
| 205 } |
| 206 DOMArrayBuffer* clientDataBuffer = DOMArrayBuffer::create( |
| 207 static_cast<void*>(&credential->client_data.front()), |
| 208 credential->client_data.size()); |
| 209 |
| 210 DOMArrayBuffer* attestationBuffer = DOMArrayBuffer::create( |
| 211 static_cast<void*>(&credential->attestation.front()), |
| 212 credential->attestation.size()); |
| 213 |
| 214 scopedCredentials.push_back( |
| 215 ScopedCredentialInfo::create(clientDataBuffer, attestationBuffer)); |
| 216 } |
| 217 resolver->resolve(scopedCredentials); |
| 218 m_authenticatorRequests.erase(resolver); |
| 219 } |
| 220 |
| 221 bool WebAuthentication::markRequestComplete(ScriptPromiseResolver* resolver) { |
| 222 auto requestEntry = m_authenticatorRequests.find(resolver); |
| 223 if (requestEntry == m_authenticatorRequests.end()) |
| 224 return false; |
| 225 m_authenticatorRequests.erase(requestEntry); |
| 226 return true; |
| 227 } |
| 228 |
| 229 DEFINE_TRACE(WebAuthentication) { |
| 230 visitor->trace(m_authenticatorRequests); |
| 231 ContextLifecycleObserver::trace(visitor); |
| 232 } |
| 233 |
| 35 } // namespace blink | 234 } // namespace blink |
| OLD | NEW |