libQuotient
A Qt library for building matrix clients
filesourceinfo.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 Carl Schwan <carlschwan@kde.org>
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 
5 #pragma once
6 
7 #include <Quotient/converters.h>
8 
9 #include <array>
10 
11 namespace Quotient {
12 /**
13  * JSON Web Key object as specified in
14  * https://spec.matrix.org/unstable/client-server-api/#extensions-to-mroommessage-msgtypes
15  * The only currently relevant member is `k`, the rest needs to be set to the defaults specified in the spec.
16  */
17 struct JWK
18 {
19  Q_GADGET
20  Q_PROPERTY(QString kty MEMBER kty CONSTANT)
21  Q_PROPERTY(QStringList keyOps MEMBER keyOps CONSTANT)
22  Q_PROPERTY(QString alg MEMBER alg CONSTANT)
23  Q_PROPERTY(QString k MEMBER k CONSTANT)
24  Q_PROPERTY(bool ext MEMBER ext CONSTANT)
25 
26 public:
27  QString kty;
28  QStringList keyOps;
29  QString alg;
30  QString k;
31  bool ext;
32 };
33 
34 struct QUOTIENT_API EncryptedFileMetadata {
35  Q_GADGET
36  Q_PROPERTY(QUrl url MEMBER url CONSTANT)
37  Q_PROPERTY(JWK key MEMBER key CONSTANT)
38  Q_PROPERTY(QString iv MEMBER iv CONSTANT)
39  Q_PROPERTY(QHash<QString, QString> hashes MEMBER hashes CONSTANT)
40  Q_PROPERTY(QString v MEMBER v CONSTANT)
41  Q_PROPERTY(bool isValid READ isValid CONSTANT)
42 
43 public:
44  QUrl url;
45  JWK key;
46  QString iv;
47  QHash<QString, QString> hashes;
48  QString v;
49 
50  bool isValid() const { return url.isValid(); }
51 };
52 
53 QUOTIENT_API std::pair<EncryptedFileMetadata, QByteArray> encryptFile(
54  const QByteArray& plainText);
55 QUOTIENT_API QByteArray decryptFile(const QByteArray& ciphertext,
56  const EncryptedFileMetadata& metadata);
57 
58 template <>
59 struct QUOTIENT_API JsonObjectConverter<EncryptedFileMetadata> {
60  static void dumpTo(QJsonObject& jo, const EncryptedFileMetadata& pod);
61  static void fillFrom(const QJsonObject& jo, EncryptedFileMetadata& pod);
62 };
63 
64 template <>
65 struct QUOTIENT_API JsonObjectConverter<JWK> {
66  static void dumpTo(QJsonObject& jo, const JWK& pod);
67  static void fillFrom(const QJsonObject& jo, JWK& pod);
68 };
69 
70 using FileSourceInfo = std::variant<QUrl, EncryptedFileMetadata>;
71 
72 using FileSourceInfoKeys = std::array<QLatin1String, std::variant_size_v<FileSourceInfo>>;
73 
74 QUOTIENT_API QUrl getUrlFromSourceInfo(const FileSourceInfo& fsi);
75 
76 QUOTIENT_API void setUrlInSourceInfo(FileSourceInfo& fsi, const QUrl& newUrl);
77 
78 // The way FileSourceInfo is stored in JSON requires an extra parameter so
79 // the original template is not applicable
80 template <>
81 void fillJson(QJsonObject&, const FileSourceInfo&) = delete;
82 
83 //! \brief Export FileSourceInfo to a JSON object
84 //!
85 //! Depending on what is stored inside FileSourceInfo, this function will insert
86 //! - a key-to-string pair where key is taken from jsonKeys[0] and the string
87 //! is the URL, if FileSourceInfo stores a QUrl;
88 //! - a key-to-object mapping where key is taken from jsonKeys[1] and the object
89 //! is the result of converting EncryptedFileMetadata to JSON,
90 //! if FileSourceInfo stores EncryptedFileMetadata
91 QUOTIENT_API void fillJson(QJsonObject& jo, const FileSourceInfoKeys& jsonKeys,
92  const FileSourceInfo& fsi);
93 
94 QUOTIENT_API FileSourceInfo fileSourceInfoFromJson(const QJsonObject& jo,
95  const FileSourceInfoKeys& jsonKeys);
96 
97 namespace FileMetadataMap {
98  QUOTIENT_API void add(const QString& roomId,
99  const QString& eventId,
100  const EncryptedFileMetadata& fileMetadata);
101  QUOTIENT_API void remove(const QString& roomId,
102  const QString& eventId);
103 
104  //! \brief Obtain file source information across connections, thread-safely
105  //! \return the previously saved EncryptedFileMetadata object, or an invalid
106  //! (default-constructed) object in case of unsuccessful lookup
107  QUOTIENT_API EncryptedFileMetadata lookup(const QString& roomId,
108  const QString& eventId);
109 }
110 
111 } // namespace Quotient