LibWeb: Add the CryptoKey interface

This commit is contained in:
stelar7 2023-12-15 20:25:35 +01:00 committed by Andrew Kaster
parent 69ffdd5738
commit a8ddf6c2a4
Notes: sideshowbarker 2024-07-17 11:30:05 +09:00
7 changed files with 104 additions and 0 deletions

View file

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

View file

@ -26,6 +26,7 @@ standard_idl_files = [
"//Userland/Libraries/LibWeb/Animations/DocumentTimeline.idl",
"//Userland/Libraries/LibWeb/Clipboard/Clipboard.idl",
"//Userland/Libraries/LibWeb/Crypto/Crypto.idl",
"//Userland/Libraries/LibWeb/Crypto/CryptoKey.idl",
"//Userland/Libraries/LibWeb/Crypto/SubtleCrypto.idl",
"//Userland/Libraries/LibWeb/CSS/CSSConditionRule.idl",
"//Userland/Libraries/LibWeb/CSS/CSSFontFaceRule.idl",

View file

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

View file

@ -0,0 +1,40 @@
/*
* Copyright (c) 2023, stelar7 <dudedbz@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Crypto/CryptoKey.h>
namespace Web::Crypto {
JS_DEFINE_ALLOCATOR(CryptoKey);
JS::NonnullGCPtr<CryptoKey> CryptoKey::create(JS::Realm& realm)
{
return realm.heap().allocate<CryptoKey>(realm, realm);
}
CryptoKey::CryptoKey(JS::Realm& realm)
: PlatformObject(realm)
, m_algorithm(Object::create(realm, nullptr))
, m_usages(Object::create(realm, nullptr))
{
}
CryptoKey::~CryptoKey() = default;
void CryptoKey::initialize(JS::Realm& realm)
{
Base::initialize(realm);
set_prototype(&Bindings::ensure_web_prototype<Bindings::CryptoKeyPrototype>(realm, "CryptoKey"_fly_string));
}
void CryptoKey::visit_edges(Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_algorithm);
visitor.visit(m_usages);
}
}

View file

@ -0,0 +1,47 @@
/*
* Copyright (c) 2023, stelar7 <dudedbz@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Forward.h>
#include <LibJS/Heap/GCPtr.h>
#include <LibWeb/Bindings/CryptoKeyPrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/PlatformObject.h>
namespace Web::Crypto {
class CryptoKey final : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(CryptoKey, Bindings::PlatformObject);
JS_DECLARE_ALLOCATOR(CryptoKey);
public:
[[nodiscard]] static JS::NonnullGCPtr<CryptoKey> create(JS::Realm&);
virtual ~CryptoKey() override;
bool extractable() const { return m_extractable; }
Bindings::KeyType type() const { return m_type; }
Object const* algorithm() const { return m_algorithm.ptr(); }
Object const* usages() const { return m_usages.ptr(); }
void set_extractable(bool extractable) { m_extractable = extractable; }
void set_type(Bindings::KeyType type) { m_type = type; }
void set_algorithm(JS::NonnullGCPtr<Object> algorithm) { m_algorithm = move(algorithm); }
void set_usages(JS::NonnullGCPtr<Object> usages) { m_usages = move(usages); }
private:
explicit CryptoKey(JS::Realm&);
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Visitor&) override;
Bindings::KeyType m_type;
bool m_extractable { false };
JS::NonnullGCPtr<Object> m_algorithm;
JS::NonnullGCPtr<Object> m_usages;
};
}

View file

@ -0,0 +1,12 @@
enum KeyType { "public", "private", "secret" };
enum KeyUsage { "encrypt", "decrypt", "sign", "verify", "deriveKey", "deriveBits", "wrapKey", "unwrapKey" };
// https://w3c.github.io/webcrypto/#cryptokey-interface
[SecureContext,Exposed=(Window,Worker),Serializable]
interface CryptoKey {
readonly attribute KeyType type;
readonly attribute boolean extractable;
readonly attribute object algorithm;
readonly attribute object usages;
};

View file

@ -9,6 +9,7 @@ libweb_js_bindings(Animations/DocumentTimeline)
libweb_js_bindings(Animations/KeyframeEffect)
libweb_js_bindings(Clipboard/Clipboard)
libweb_js_bindings(Crypto/Crypto)
libweb_js_bindings(Crypto/CryptoKey)
libweb_js_bindings(Crypto/SubtleCrypto)
libweb_js_bindings(CSS/CSSConditionRule)
libweb_js_bindings(CSS/CSSFontFaceRule)