|
4 | 4 | * SPDX-License-Identifier: BSD-2-Clause |
5 | 5 | */ |
6 | 6 |
|
| 7 | +#include <LibWeb/Bindings/ExceptionOrUtils.h> |
| 8 | +#include <LibWeb/CredentialManagement/CredentialsContainer.h> |
7 | 9 | #include <LibWeb/CredentialManagement/PasswordCredential.h> |
8 | 10 | #include <LibWeb/CredentialManagement/PasswordCredentialOperations.h> |
| 11 | +#include <LibWeb/HTML/AutocompleteElement.h> |
| 12 | +#include <LibWeb/XHR/FormData.h> |
9 | 13 |
|
10 | 14 | namespace Web::CredentialManagement { |
11 | 15 |
|
@@ -51,4 +55,31 @@ void PasswordCredential::initialize(JS::Realm& realm) |
51 | 55 | Base::initialize(realm); |
52 | 56 | } |
53 | 57 |
|
| 58 | +// https://w3c.github.io/webappsec-credential-management/#create-passwordcredential |
| 59 | +JS::ThrowCompletionOr<Variant<Empty, GC::Ref<Credential>, GC::Ref<CreateCredentialAlgorithm>>> PasswordCredentialInterface::create(JS::Realm& realm, URL::Origin const& origin, CredentialCreationOptions const& options, bool) const |
| 60 | +{ |
| 61 | + // 1. Assert: options["password"] exists, and sameOriginWithAncestors is unused. |
| 62 | + VERIFY(options.password.has_value()); |
| 63 | + |
| 64 | + auto maybe_result = options.password->visit( |
| 65 | + // 2. If options["password"] is an HTMLFormElement, return the result of executing Create a PasswordCredential |
| 66 | + // from an HTMLFormElement given options["password"] and origin. Rethrow any exceptions. |
| 67 | + [&](GC::Root<HTML::HTMLFormElement> const& form) { |
| 68 | + return create_password_credential(realm, *form, origin); |
| 69 | + }, |
| 70 | + // 3. If options["password"] is a PasswordCredentialData, return the result of executing |
| 71 | + // Create a PasswordCredential from PasswordCredentialData given options["password"]. Rethrow any exceptions. |
| 72 | + [&](PasswordCredentialData const& data) { |
| 73 | + return create_password_credential(realm, data, origin); |
| 74 | + }, |
| 75 | + // 4. Throw a TypeError exception. |
| 76 | + [&](auto) { |
| 77 | + return realm.vm().throw_completion<JS::TypeError>("options.password must be an HTMLFormElement or a PasswordCredentialData"sv); |
| 78 | + }); |
| 79 | + if (maybe_result.is_error()) |
| 80 | + return Bindings::exception_to_throw_completion(realm.vm(), maybe_result.release_error()); |
| 81 | + |
| 82 | + return maybe_result.release_value(); |
| 83 | +} |
| 84 | + |
54 | 85 | } |
0 commit comments