瀏覽代碼

LibWeb: Move dictionaries to separate file

Co-Authored-By: Andrew Kaster <akaster@serenityos.org>
stelar7 1 年之前
父節點
當前提交
3e0c0ce7ae

+ 2 - 0
Meta/gn/secondary/Userland/Libraries/LibWeb/Crypto/BUILD.gn

@@ -4,6 +4,8 @@ source_set("Crypto") {
   sources = [
     "Crypto.cpp",
     "Crypto.h",
+    "CryptoBindings.cpp",
+    "CryptoBindings.h",
     "CryptoKey.cpp",
     "CryptoKey.h",
     "SubtleCrypto.cpp",

+ 1 - 0
Userland/Libraries/LibWeb/CMakeLists.txt

@@ -24,6 +24,7 @@ set(SOURCES
     Bindings/PlatformObject.cpp
     Clipboard/Clipboard.cpp
     Crypto/Crypto.cpp
+    Crypto/CryptoBindings.cpp
     Crypto/CryptoKey.cpp
     Crypto/SubtleCrypto.cpp
     CSS/Angle.cpp

+ 53 - 0
Userland/Libraries/LibWeb/Crypto/CryptoBindings.cpp

@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2023, stelar7 <dudedbz@gmail.com>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibJS/Runtime/AbstractOperations.h>
+#include <LibJS/Runtime/GlobalObject.h>
+#include <LibWeb/Bindings/ExceptionOrUtils.h>
+#include <LibWeb/Crypto/CryptoBindings.h>
+
+namespace Web::Bindings {
+
+JS_DEFINE_ALLOCATOR(KeyAlgorithm);
+
+JS::NonnullGCPtr<KeyAlgorithm> KeyAlgorithm::create(JS::Realm& realm)
+{
+    return realm.heap().allocate<KeyAlgorithm>(realm, realm);
+}
+
+KeyAlgorithm::KeyAlgorithm(JS::Realm& realm)
+    : Object(ConstructWithPrototypeTag::Tag, realm.intrinsics().object_prototype())
+{
+}
+
+void KeyAlgorithm::initialize(JS::Realm& realm)
+{
+    define_native_accessor(realm, "name", name_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable);
+    Base::initialize(realm);
+}
+
+static JS::ThrowCompletionOr<KeyAlgorithm*> impl_from(JS::VM& vm)
+{
+    auto this_value = vm.this_value();
+    JS::Object* this_object = nullptr;
+    if (this_value.is_nullish())
+        this_object = &vm.current_realm()->global_object();
+    else
+        this_object = TRY(this_value.to_object(vm));
+
+    if (!is<KeyAlgorithm>(this_object))
+        return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "KeyAlgorithm");
+    return static_cast<KeyAlgorithm*>(this_object);
+}
+
+JS_DEFINE_NATIVE_FUNCTION(KeyAlgorithm::name_getter)
+{
+    auto* impl = TRY(impl_from(vm));
+    auto name = TRY(throw_dom_exception_if_needed(vm, [&] { return impl->name(); }));
+    return JS::PrimitiveString::create(vm, name);
+}
+
+}

+ 76 - 0
Userland/Libraries/LibWeb/Crypto/CryptoBindings.h

@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2023, stelar7 <dudedbz@gmail.com>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <LibWeb/WebIDL/Buffers.h>
+
+// FIXME: Generate these from IDL
+namespace Web::Bindings {
+
+// https://w3c.github.io/webcrypto/#JsonWebKey-dictionary
+struct RsaOtherPrimesInfo {
+    Optional<String> r;
+    Optional<String> d;
+    Optional<String> t;
+};
+
+// https://w3c.github.io/webcrypto/#JsonWebKey-dictionary
+struct JsonWebKey {
+    Optional<String> kty;
+    Optional<String> use;
+    Optional<Vector<String>> key_ops;
+    Optional<String> alg;
+    Optional<bool> ext;
+    Optional<String> crv;
+    Optional<String> x;
+    Optional<String> y;
+    Optional<String> d;
+    Optional<String> n;
+    Optional<String> e;
+    Optional<String> p;
+    Optional<String> q;
+    Optional<String> dp;
+    Optional<String> dq;
+    Optional<String> qi;
+    Optional<Vector<RsaOtherPrimesInfo>> oth;
+    Optional<String> k;
+};
+
+// https://w3c.github.io/webcrypto/#dfn-Algorithm
+struct Algorithm {
+    String name;
+};
+
+// https://w3c.github.io/webcrypto/#key-algorithm-dictionary
+class KeyAlgorithm : public JS::Object {
+    JS_OBJECT(KeyAlgorithm, Object);
+    JS_DECLARE_ALLOCATOR(KeyAlgorithm);
+
+public:
+    static JS::NonnullGCPtr<KeyAlgorithm> create(JS::Realm&);
+    virtual ~KeyAlgorithm() override = default;
+
+    String const& name() const { return m_name; }
+    void set_name(String name) { m_name = move(name); }
+
+private:
+    KeyAlgorithm(JS::Realm&);
+    virtual void initialize(JS::Realm&) override;
+
+    JS_DECLARE_NATIVE_FUNCTION(name_getter);
+
+    String m_name;
+};
+
+// https://w3c.github.io/webcrypto/#pbkdf2-params
+struct Pbkdf2Params {
+    JS::Handle<WebIDL::BufferSource> salt;
+    u32 iterations;
+    Variant<JS::Handle<JS::Object>, String> hash;
+};
+
+};

+ 1 - 1
Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp

@@ -190,7 +190,7 @@ SubtleCrypto::SupportedAlgorithmsMap& SubtleCrypto::supported_algorithms_interna
     return s_supported_algorithms;
 }
 
-// https://w3c.github.io/webcrypto/#algorithm-normalization-internal
+// https://w3c.github.io/webcrypto/#algorithm-normalization-internalS
 SubtleCrypto::SupportedAlgorithmsMap SubtleCrypto::supported_algorithms()
 {
     auto& internal_object = supported_algorithms_internal();

+ 1 - 9
Userland/Libraries/LibWeb/Crypto/SubtleCrypto.h

@@ -10,15 +10,7 @@
 #include <AK/String.h>
 #include <LibJS/Forward.h>
 #include <LibWeb/Bindings/PlatformObject.h>
-// FIXME: Generate these from IDL
-namespace Web::Bindings {
-
-// https://w3c.github.io/webcrypto/#dfn-Algorithm
-struct Algorithm {
-    String name;
-};
-
-};
+#include <LibWeb/Crypto/CryptoBindings.h>
 
 namespace Web::Crypto {
 

+ 36 - 0
Userland/Libraries/LibWeb/Crypto/SubtleCrypto.idl

@@ -4,6 +4,42 @@ dictionary Algorithm {
     required DOMString name;
 };
 
+enum KeyFormat { "raw", "spki", "pkcs8", "jwk" };
+
+dictionary RsaOtherPrimesInfo {
+    // The following fields are defined in Section 6.3.2.7 of JSON Web Algorithms
+    DOMString r;
+    DOMString d;
+    DOMString t;
+};
+
+dictionary JsonWebKey
+{
+    // The following fields are defined in Section 3.1 of JSON Web Key
+    DOMString kty;
+    DOMString use;
+    sequence<DOMString> key_ops;
+    DOMString alg;
+
+    // The following fields are defined in JSON Web Key Parameters Registration
+    boolean ext;
+
+    // The following fields are defined in Section 6 of JSON Web Algorithms
+    DOMString crv;
+    DOMString x;
+    DOMString y;
+    DOMString d;
+    DOMString n;
+    DOMString e;
+    DOMString p;
+    DOMString q;
+    DOMString dp;
+    DOMString dq;
+    DOMString qi;
+    sequence<RsaOtherPrimesInfo> oth;
+    DOMString k;
+};
+
 // https://w3c.github.io/webcrypto/#subtlecrypto-interface
 [SecureContext,Exposed=(Window,Worker)]
 interface SubtleCrypto {