account.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Copyright (C) by Daniel Molkentin <danimo@owncloud.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. */
  14. #ifndef SERVERCONNECTION_H
  15. #define SERVERCONNECTION_H
  16. #include <QByteArray>
  17. #include <QUrl>
  18. #include <QNetworkCookie>
  19. #include <QNetworkRequest>
  20. #include <QSslSocket>
  21. #include <QSslCertificate>
  22. #include <QSslConfiguration>
  23. #include <QSslCipher>
  24. #include <QSslError>
  25. #include <QSharedPointer>
  26. #ifndef TOKEN_AUTH_ONLY
  27. #include <QPixmap>
  28. #endif
  29. #include "common/utility.h"
  30. #include <memory>
  31. #include "capabilities.h"
  32. #include "clientsideencryption.h"
  33. class QSettings;
  34. class QNetworkReply;
  35. class QUrl;
  36. class QNetworkAccessManager;
  37. namespace QKeychain {
  38. class Job;
  39. class WritePasswordJob;
  40. class ReadPasswordJob;
  41. }
  42. namespace OCC {
  43. class AbstractCredentials;
  44. class Account;
  45. using AccountPtr = QSharedPointer<Account>;
  46. class AccessManager;
  47. class SimpleNetworkJob;
  48. /**
  49. * @brief Reimplement this to handle SSL errors from libsync
  50. * @ingroup libsync
  51. */
  52. class AbstractSslErrorHandler
  53. {
  54. public:
  55. virtual ~AbstractSslErrorHandler() = default;
  56. virtual bool handleErrors(QList<QSslError>, const QSslConfiguration &conf, QList<QSslCertificate> *, AccountPtr) = 0;
  57. };
  58. /**
  59. * @brief The Account class represents an account on an ownCloud Server
  60. * @ingroup libsync
  61. *
  62. * The Account has a name and url. It also has information about credentials,
  63. * SSL errors and certificates.
  64. */
  65. class OWNCLOUDSYNC_EXPORT Account : public QObject
  66. {
  67. Q_OBJECT
  68. Q_PROPERTY(QString id MEMBER _id)
  69. Q_PROPERTY(QString davUser MEMBER _davUser)
  70. Q_PROPERTY(QString displayName MEMBER _displayName)
  71. Q_PROPERTY(QUrl url MEMBER _url)
  72. public:
  73. static AccountPtr create();
  74. ~Account();
  75. AccountPtr sharedFromThis();
  76. /**
  77. * The user that can be used in dav url.
  78. *
  79. * This can very well be different frome the login user that's
  80. * stored in credentials()->user().
  81. */
  82. QString davUser() const;
  83. void setDavUser(const QString &newDavUser);
  84. QString davDisplayName() const;
  85. void setDavDisplayName(const QString &newDisplayName);
  86. #ifndef TOKEN_AUTH_ONLY
  87. QImage avatar() const;
  88. void setAvatar(const QImage &img);
  89. #endif
  90. /// The name of the account as shown in the toolbar
  91. QString displayName() const;
  92. /// The internal id of the account.
  93. QString id() const;
  94. /** Server url of the account */
  95. void setUrl(const QUrl &url);
  96. QUrl url() const { return _url; }
  97. /// Adjusts _userVisibleUrl once the host to use is discovered.
  98. void setUserVisibleHost(const QString &host);
  99. /**
  100. * @brief The possibly themed dav path for the account. It has
  101. * a trailing slash.
  102. * @returns the (themeable) dav path for the account.
  103. */
  104. QString davPath() const;
  105. void setDavPath(const QString &s) { _davPath = s; }
  106. void setNonShib(bool nonShib);
  107. /** Returns webdav entry URL, based on url() */
  108. QUrl davUrl() const;
  109. /** Returns the legacy permalink url for a file.
  110. *
  111. * This uses the old way of manually building the url. New code should
  112. * use the "privatelink" property accessible via PROPFIND.
  113. */
  114. QUrl deprecatedPrivateLinkUrl(const QByteArray &numericFileId) const;
  115. /** Holds the accounts credentials */
  116. AbstractCredentials *credentials() const;
  117. void setCredentials(AbstractCredentials *cred);
  118. /** Create a network request on the account's QNAM.
  119. *
  120. * Network requests in AbstractNetworkJobs are created through
  121. * this function. Other places should prefer to use jobs or
  122. * sendRequest().
  123. */
  124. QNetworkReply *sendRawRequest(const QByteArray &verb,
  125. const QUrl &url,
  126. QNetworkRequest req = QNetworkRequest(),
  127. QIODevice *data = nullptr);
  128. /** Create and start network job for a simple one-off request.
  129. *
  130. * More complicated requests typically create their own job types.
  131. */
  132. SimpleNetworkJob *sendRequest(const QByteArray &verb,
  133. const QUrl &url,
  134. QNetworkRequest req = QNetworkRequest(),
  135. QIODevice *data = nullptr);
  136. /** The ssl configuration during the first connection */
  137. QSslConfiguration getOrCreateSslConfig();
  138. QSslConfiguration sslConfiguration() const { return _sslConfiguration; }
  139. void setSslConfiguration(const QSslConfiguration &config);
  140. // Because of bugs in Qt, we use this to store info needed for the SSL Button
  141. QSslCipher _sessionCipher;
  142. QByteArray _sessionTicket;
  143. QList<QSslCertificate> _peerCertificateChain;
  144. /** The certificates of the account */
  145. QList<QSslCertificate> approvedCerts() const { return _approvedCerts; }
  146. void setApprovedCerts(const QList<QSslCertificate> certs);
  147. void addApprovedCerts(const QList<QSslCertificate> certs);
  148. // Usually when a user explicitly rejects a certificate we don't
  149. // ask again. After this call, a dialog will again be shown when
  150. // the next unknown certificate is encountered.
  151. void resetRejectedCertificates();
  152. // pluggable handler
  153. void setSslErrorHandler(AbstractSslErrorHandler *handler);
  154. // To be called by credentials only, for storing username and the like
  155. QVariant credentialSetting(const QString &key) const;
  156. void setCredentialSetting(const QString &key, const QVariant &value);
  157. /** Assign a client certificate */
  158. void setCertificate(const QByteArray certficate = QByteArray(), const QString privateKey = QString());
  159. /** Access the server capabilities */
  160. const Capabilities &capabilities() const;
  161. void setCapabilities(const QVariantMap &caps);
  162. /** Access the server version
  163. *
  164. * For servers >= 10.0.0, this can be the empty string until capabilities
  165. * have been received.
  166. */
  167. QString serverVersion() const;
  168. /** Server version for easy comparison.
  169. *
  170. * Example: serverVersionInt() >= makeServerVersion(11, 2, 3)
  171. *
  172. * Will be 0 if the version is not available yet.
  173. */
  174. int serverVersionInt() const;
  175. static int makeServerVersion(int majorVersion, int minorVersion, int patchVersion);
  176. void setServerVersion(const QString &version);
  177. /** Whether the server is too old.
  178. *
  179. * Not supporting server versions is a gradual process. There's a hard
  180. * compatibility limit (see ConnectionValidator) that forbids connecting
  181. * to extremely old servers. And there's a weak "untested, not
  182. * recommended, potentially dangerous" limit, that users might want
  183. * to go beyond.
  184. *
  185. * This function returns true if the server is beyond the weak limit.
  186. */
  187. bool serverVersionUnsupported() const;
  188. /** True when the server connection is using HTTP2 */
  189. bool isHttp2Supported() { return _http2Supported; }
  190. void setHttp2Supported(bool value) { _http2Supported = value; }
  191. void clearCookieJar();
  192. void lendCookieJarTo(QNetworkAccessManager *guest);
  193. QString cookieJarPath();
  194. void resetNetworkAccessManager();
  195. QNetworkAccessManager *networkAccessManager();
  196. QSharedPointer<QNetworkAccessManager> sharedNetworkAccessManager();
  197. /// Called by network jobs on credential errors, emits invalidCredentials()
  198. void handleInvalidCredentials();
  199. ClientSideEncryption* e2e();
  200. /// Used in RemoteWipe
  201. void retrieveAppPassword();
  202. void writeAppPasswordOnce(QString appPassword);
  203. void deleteAppPassword();
  204. /// Direct Editing
  205. // Check for the directEditing capability
  206. void fetchDirectEditors(const QUrl &directEditingURL, const QString &directEditingETag);
  207. public slots:
  208. /// Used when forgetting credentials
  209. void clearQNAMCache();
  210. void slotHandleSslErrors(QNetworkReply *, QList<QSslError>);
  211. signals:
  212. /// Emitted whenever there's network activity
  213. void propagatorNetworkActivity();
  214. /// Triggered by handleInvalidCredentials()
  215. void invalidCredentials();
  216. void credentialsFetched(AbstractCredentials *credentials);
  217. void credentialsAsked(AbstractCredentials *credentials);
  218. /// Forwards from QNetworkAccessManager::proxyAuthenticationRequired().
  219. void proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *);
  220. // e.g. when the approved SSL certificates changed
  221. void wantsAccountSaved(Account *acc);
  222. void serverVersionChanged(Account *account, const QString &newVersion, const QString &oldVersion);
  223. void accountChangedAvatar();
  224. void accountChangedDisplayName();
  225. /// Used in RemoteWipe
  226. void appPasswordRetrieved(QString);
  227. protected Q_SLOTS:
  228. void slotCredentialsFetched();
  229. void slotCredentialsAsked();
  230. void slotDirectEditingRecieved(const QJsonDocument &json);
  231. private:
  232. Account(QObject *parent = nullptr);
  233. void setSharedThis(AccountPtr sharedThis);
  234. QWeakPointer<Account> _sharedThis;
  235. QString _id;
  236. QString _davUser;
  237. QString _displayName;
  238. #ifndef TOKEN_AUTH_ONLY
  239. QImage _avatarImg;
  240. #endif
  241. QMap<QString, QVariant> _settingsMap;
  242. QUrl _url;
  243. /** If url to use for any user-visible urls.
  244. *
  245. * If the server configures overwritehost this can be different from
  246. * the connection url in _url. We retrieve the visible host through
  247. * the ocs/v1.php/config endpoint in ConnectionValidator.
  248. */
  249. QUrl _userVisibleUrl;
  250. QList<QSslCertificate> _approvedCerts;
  251. QSslConfiguration _sslConfiguration;
  252. Capabilities _capabilities;
  253. QString _serverVersion;
  254. QScopedPointer<AbstractSslErrorHandler> _sslErrorHandler;
  255. QSharedPointer<QNetworkAccessManager> _am;
  256. QScopedPointer<AbstractCredentials> _credentials;
  257. bool _http2Supported = false;
  258. /// Certificates that were explicitly rejected by the user
  259. QList<QSslCertificate> _rejectedCertificates;
  260. static QString _configFileName;
  261. QString _davPath; // defaults to value from theme, might be overwritten in brandings
  262. ClientSideEncryption _e2e;
  263. /// Used in RemoteWipe
  264. bool _wroteAppPassword = false;
  265. friend class AccountManager;
  266. // Direct Editing
  267. QString _lastDirectEditingETag;
  268. /* IMPORTANT - remove later - FIXME MS@2019-12-07 -->
  269. * TODO: For "Log out" & "Remove account": Remove client CA certs and KEY!
  270. *
  271. * Disabled as long as selecting another cert is not supported by the UI.
  272. *
  273. * Being able to specify a new certificate is important anyway: expiry etc.
  274. *
  275. * We introduce this dirty hack here, to allow deleting them upon Remote Wipe.
  276. */
  277. public:
  278. void setRemoteWipeRequested_HACK() { _isRemoteWipeRequested_HACK = true; }
  279. bool isRemoteWipeRequested_HACK() { return _isRemoteWipeRequested_HACK; }
  280. private:
  281. bool _isRemoteWipeRequested_HACK = false;
  282. // <-- FIXME MS@2019-12-07
  283. };
  284. }
  285. Q_DECLARE_METATYPE(OCC::AccountPtr)
  286. #endif //SERVERCONNECTION_H