mtproto_bound_key_creator.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "mtproto/details/mtproto_bound_key_creator.h"
  8. #include "mtproto/details/mtproto_serialized_request.h"
  9. namespace MTP::details {
  10. BoundKeyCreator::BoundKeyCreator(DcKeyRequest request, Delegate delegate)
  11. : _request(request)
  12. , _delegate(std::move(delegate)) {
  13. }
  14. void BoundKeyCreator::start(
  15. DcId dcId,
  16. int16 protocolDcId,
  17. not_null<AbstractConnection*> connection,
  18. not_null<DcOptions*> dcOptions) {
  19. Expects(!_creator.has_value());
  20. auto delegate = DcKeyCreator::Delegate();
  21. delegate.done = _delegate.unboundReady;
  22. delegate.sentSome = _delegate.sentSome;
  23. delegate.receivedSome = _delegate.receivedSome;
  24. _creator.emplace(
  25. dcId,
  26. protocolDcId,
  27. connection,
  28. dcOptions,
  29. std::move(delegate),
  30. _request);
  31. }
  32. void BoundKeyCreator::stop() {
  33. _creator = std::nullopt;
  34. }
  35. void BoundKeyCreator::bind(AuthKeyPtr &&persistentKey) {
  36. stop();
  37. _binder.emplace(std::move(persistentKey));
  38. }
  39. void BoundKeyCreator::restartBinder() {
  40. if (_binder) {
  41. _binder.emplace(_binder->persistentKey());
  42. }
  43. }
  44. bool BoundKeyCreator::readyToBind() const {
  45. return _binder.has_value();
  46. }
  47. SerializedRequest BoundKeyCreator::prepareBindRequest(
  48. const AuthKeyPtr &temporaryKey,
  49. uint64 sessionId) {
  50. Expects(_binder.has_value());
  51. return _binder->prepareRequest(temporaryKey, sessionId);
  52. }
  53. DcKeyBindState BoundKeyCreator::handleBindResponse(
  54. const mtpBuffer &response) {
  55. Expects(_binder.has_value());
  56. return _binder->handleResponse(response);
  57. }
  58. AuthKeyPtr BoundKeyCreator::bindPersistentKey() const {
  59. Expects(_binder.has_value());
  60. return _binder->persistentKey();
  61. }
  62. bool IsDestroyedTemporaryKeyError(const mtpBuffer &buffer) {
  63. auto from = buffer.data();
  64. auto error = MTPRpcError();
  65. if (!error.read(from, from + buffer.size())) {
  66. return false;
  67. }
  68. return error.match([&](const MTPDrpc_error &data) {
  69. return (data.verror_code().v == 401)
  70. && (data.verror_message().v == "AUTH_KEY_PERM_EMPTY");
  71. });
  72. }
  73. } // namespace MTP::details