simplechatengine.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of qtprotobuf project https://git.semlanik.org/semlanik/qtprotobuf
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "simplechatengine.h"
  26. #include <http2channel.h>
  27. #include <insecurecredentials.h>
  28. #include <sslcredentials.h>
  29. #include <QDebug>
  30. #include <QFile>
  31. #include <QSslConfiguration>
  32. #include <QCryptographicHash>
  33. #include <QDateTime>
  34. using namespace qtprotobuf::examples;
  35. class AuthCredentials : public qtprotobuf::CallCredentials
  36. {
  37. public:
  38. AuthCredentials(const QString &userName, const QString &password) :
  39. CallCredentials(CredentialMap{{QLatin1String("user-name"), QVariant::fromValue(userName)},
  40. {QLatin1String("user-password"), QVariant::fromValue(password)}}) {}
  41. };
  42. SimpleChatEngine::SimpleChatEngine(QObject *parent) : QObject(parent), m_client(new SimpleChatClient)
  43. {
  44. }
  45. SimpleChatEngine::~SimpleChatEngine()
  46. {
  47. delete m_client;
  48. }
  49. void SimpleChatEngine::login(const QString &name, const QString &password)
  50. {
  51. QSslConfiguration conf = QSslConfiguration::defaultConfiguration();
  52. QFile certFile("cert.pem");
  53. certFile.open(QIODevice::ReadOnly);
  54. QByteArray cert = certFile.readAll();
  55. conf.setCaCertificates({QSslCertificate(cert)});
  56. conf.setProtocol(QSsl::TlsV1_2);
  57. conf.setAllowedNextProtocols({QSslConfiguration::ALPNProtocolHTTP2});
  58. std::shared_ptr<qtprotobuf::AbstractChannel> channel(new qtprotobuf::Http2Channel("localhost", 65002, qtprotobuf::SslCredentials(conf) |
  59. AuthCredentials(name, QCryptographicHash::hash(password.toUtf8(), QCryptographicHash::Md5).toHex())));
  60. m_client->attachChannel(channel);
  61. m_client->subscribeMessageListUpdates(None());
  62. QObject::connect(m_client, &SimpleChatClient::messageListUpdated, this, [this](const qtprotobuf::examples::ChatMessages &messages){
  63. loggedIn();
  64. m_messages.reset(messages.messages());
  65. });
  66. }
  67. void SimpleChatEngine::sendMessage(const QString &content)
  68. {
  69. m_client->sendMessage(ChatMessage(QDateTime::currentMSecsSinceEpoch(), content.toUtf8(), ChatMessage::ContentType::Text));
  70. }