-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtelegramauthenticate.cpp
300 lines (256 loc) · 7.96 KB
/
telegramauthenticate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#define DEFINE_DIS \
QPointer<TelegramAuthenticate> dis = this;
#include "telegramauthenticate.h"
#include "telegramengine.h"
#include <telegram.h>
#include <QPointer>
#include <QTimer>
#include <QCryptographicHash>
class TelegramAuthenticatePrivate
{
public:
QPointer<TelegramEngine> engine;
int state;
int callTimeout;
int remainingTime;
QString signup_firstName;
QString signup_lastName;
QTimer *remainTimer;
//for the password/2FA authentication. Storing AccountPassword here did not work, the bytes got corrupted
QString currentSalt;
};
TelegramAuthenticate::TelegramAuthenticate(QObject *parent) :
TqObject(parent)
{
p = new TelegramAuthenticatePrivate;
p->state = AuthUnknown;
p->callTimeout = 0;
p->remainingTime = 0;
p->remainTimer = 0;
}
TelegramEngine *TelegramAuthenticate::engine() const
{
return p->engine;
}
void TelegramAuthenticate::setEngine(TelegramEngine *engine)
{
if(p->engine == engine)
return;
if(p->engine)
{
disconnect(p->engine.data(), &TelegramEngine::telegramChanged, this, &TelegramAuthenticate::refresh);
disconnect(p->engine.data(), &TelegramEngine::stateChanged, this, &TelegramAuthenticate::refresh);
}
p->engine = engine;
if(p->engine)
{
connect(p->engine.data(), &TelegramEngine::telegramChanged, this, &TelegramAuthenticate::refresh);
connect(p->engine.data(), &TelegramEngine::stateChanged, this, &TelegramAuthenticate::refresh);
}
refresh();
Q_EMIT engineChanged();
}
int TelegramAuthenticate::state() const
{
return p->state;
}
int TelegramAuthenticate::callTimeout() const
{
return p->callTimeout;
}
int TelegramAuthenticate::remainingTime() const
{
return p->remainingTime;
}
QStringList TelegramAuthenticate::requiredProperties()
{
return QStringList() << FUNCTION_NAME(engine);
}
void TelegramAuthenticate::signUp(const QString &firstName, const QString &lastName)
{
if(p->state != AuthSignUpNeeded)
{
qDebug() << "Authenticate Error: You can only call signUp method, when state is AuthSignUpNeeded.";
return;
}
p->signup_firstName = firstName.trimmed();
p->signup_lastName = lastName.trimmed();
requestCode();
}
void TelegramAuthenticate::signIn(const QString &code)
{
if(p->state != AuthCodeRequested && p->state != AuthCodeRequestingError && p->state != AuthLoggingInError)
{
qDebug() << "Authenticate Error: You can only call signIn method, when state is AuthCodeRequested.";
return;
}
if(code.trimmed().isEmpty())
{
qDebug() << "Authenticate Error: you can sign-in using empty code.";
return;
}
DEFINE_DIS;
switchState(AuthLoggingIn);
QPointer<Telegram> tg = p->engine->telegram();
TelegramCore::Callback<AuthAuthorization> callback = [this, dis, tg](TG_AUTH_SIGN_IN_CALLBACK) {
Q_UNUSED(msgId)
Q_UNUSED(result)
if(!dis || !tg) return;
if(error.errorText == "SESSION_PASSWORD_NEEDED") {
tg->accountGetPassword([this, dis, tg](TG_ACCOUNT_GET_PASSWORD_CALLBACK){
Q_UNUSED(msgId)
if(!error.null) {
setError(error.errorText, error.errorCode);
switchState(AuthLoggingInError);
return;
}
//As a workaround for the binary corruption of the AccountPassword we store it here as a string, thereby guaranteeing deep copy
p->currentSalt = QString(result.currentSalt().toHex());
switchState(AuthPasswordRequested);
});
}
else
if(!error.null) {
setError(error.errorText, error.errorCode);
switchState(AuthLoggingInError);
}
};
if(!p->signup_firstName.isEmpty() && !p->signup_lastName.isEmpty())
tg->authSignUp(code.trimmed(), p->signup_firstName, p->signup_lastName, callback);
else
tg->authSignIn(code.trimmed(), callback);
}
void TelegramAuthenticate::checkPassword(const QString &password)
{
if(p->state != AuthPasswordRequested)
{
qDebug() << "Authenticate Error: You can only call checkPassword method, when state is AuthPasswordRequested.";
return;
}
if(password.isEmpty())
{
qDebug() << "Authenticate Error: you can't leave password argument empty.";
return;
}
//Reconstructing a byte array with the current salt. A better solution could be made of course
const QByteArray salt = QByteArray::fromHex(p->currentSalt.toUtf8());
QByteArray passData = salt + password.toUtf8() + salt;
switchState(AuthLoggingIn);
DEFINE_DIS;
Telegram *tg = p->engine->telegram();
//Moved hash calculation here. Either the whole hash is done here or in the lower lib. Splitting makes things hard to analyze & debug
tg->authCheckPassword(QCryptographicHash::hash(passData, QCryptographicHash::Sha256), [this, dis](TG_AUTH_CHECK_PASSWORD_CALLBACK){
Q_UNUSED(msgId)
Q_UNUSED(result)
if(!error.null) {
setError(error.errorText, error.errorCode);
switchState(AuthLoggingInError);
return;
}
});
}
void TelegramAuthenticate::refresh()
{
if(!p->engine || !p->engine->telegram())
{
clean();
return;
}
Telegram *tg = p->engine->telegram();
DEFINE_DIS;
if(tg)
connect(tg, &Telegram::authLoggedIn, [this, dis](){
if(!dis) return;
switchState(AuthLoggedIn);
Q_EMIT authLoggedIn();
});
if(!tg)
switchState(AuthUnknown);
else
if(tg->isLoggedIn() || p->engine->state() == TelegramEngine::AuthLoggedIn)
switchState(AuthLoggedIn);
else
if(p->engine->state() != TelegramEngine::AuthNeeded)
switchState(AuthUnknown);
else
{
switchState(AuthCheckingPhone);
tg->authCheckPhone([this, dis](TG_AUTH_CHECK_PHONE_CALLBACK){
Q_UNUSED(msgId)
if(!dis) return;
if(!error.null) {
setError(error.errorText, error.errorCode);
switchState(AuthCheckingPhoneError);
}
else
if(result.phoneRegistered())
requestCode();
else
switchState(AuthSignUpNeeded);
});
}
}
void TelegramAuthenticate::clean()
{
p->signup_firstName.clear();
p->signup_lastName.clear();
p->callTimeout = 0;
p->remainingTime = 0;
switchState(AuthUnknown);
Q_EMIT callTimeoutChanged();
Q_EMIT remainingTimeChanged();
}
void TelegramAuthenticate::startRemainingTimer(int time)
{
if(p->remainingTime == time)
return;
if(p->remainTimer)
delete p->remainTimer;
p->remainTimer = 0;
p->remainingTime = time;
if(!time)
{
Q_EMIT remainingTimeChanged();
return;
}
p->remainTimer = new QTimer(this);
p->remainTimer->setSingleShot(false);
p->remainTimer->setInterval(1000);
p->remainTimer->start();
connect(p->remainTimer, &QTimer::timeout, [this](){
p->remainingTime--;
Q_EMIT remainingTimeChanged();
if(p->remainingTime == 0)
p->remainTimer->deleteLater();
});
}
void TelegramAuthenticate::requestCode()
{
Telegram *tg = p->engine->telegram();
DEFINE_DIS;
tg->authSendCode([this, dis](TG_AUTH_SEND_CODE_CALLBACK){
Q_UNUSED(msgId)
if(!dis) return;
if(!error.null) {
setError(error.errorText, error.errorCode);
switchState(AuthCodeRequestingError);
} else {
p->callTimeout = result.timeout();
startRemainingTimer(p->callTimeout);
Q_EMIT callTimeoutChanged();
switchState(AuthCodeRequested);
}
});
switchState(AuthCodeRequesting);
}
void TelegramAuthenticate::switchState(int state)
{
if(p->state == state)
return;
p->state = state;
Q_EMIT stateChanged();
}
TelegramAuthenticate::~TelegramAuthenticate()
{
delete p;
}