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 QUOTIENT_API QUrl getUrlFromSourceInfo(const FileSourceInfo& fsi);
73 
74 QUOTIENT_API void setUrlInSourceInfo(FileSourceInfo& fsi, const QUrl& newUrl);
75 
76 // The way FileSourceInfo is stored in JSON requires an extra parameter so
77 // the original template is not applicable
78 template <>
79 void fillJson(QJsonObject&, const FileSourceInfo&) = delete;
80 
81 //! \brief Export FileSourceInfo to a JSON object
82 //!
83 //! Depending on what is stored inside FileSourceInfo, this function will insert
84 //! - a key-to-string pair where key is taken from jsonKeys[0] and the string
85 //! is the URL, if FileSourceInfo stores a QUrl;
86 //! - a key-to-object mapping where key is taken from jsonKeys[1] and the object
87 //! is the result of converting EncryptedFileMetadata to JSON,
88 //! if FileSourceInfo stores EncryptedFileMetadata
89 QUOTIENT_API void fillJson(QJsonObject& jo,
90  const std::array<QLatin1String, 2>& jsonKeys,
91  const FileSourceInfo& fsi);
92 
93 namespace FileMetadataMap {
94  QUOTIENT_API void add(const QString& roomId,
95  const QString& eventId,
96  const EncryptedFileMetadata& fileMetadata);
97  QUOTIENT_API void remove(const QString& roomId,
98  const QString& eventId);
99 
100  //! \brief Obtain file source information across connections, thread-safely
101  //! \return the previously saved EncryptedFileMetadata object, or an invalid
102  //! (default-constructed) object in case of unsuccessful lookup
103  QUOTIENT_API EncryptedFileMetadata lookup(const QString& roomId,
104  const QString& eventId);
105 }
106 
107 } // namespace Quotient