File: authenticationwizard.cpp

package info (click to toggle)
ktp-text-ui 22.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,196 kB
  • sloc: cpp: 12,718; javascript: 6,752; sh: 21; makefile: 5
file content (448 lines) | stat: -rw-r--r-- 14,746 bytes parent folder | download | duplicates (3)
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*************************************************************************
 * Copyright <2007 - 2013>  <Michael Zanetti> <mzanetti@kde.org>         *
 * Copyright <2014>       <Marcin ZiemiƄski> <zieminn@gmail.com>         *
 *                                                                       *
 * This program is free software; you can redistribute it and/or         *
 * modify it under the terms of the GNU General Public License as        *
 * published by the Free Software Foundation; either version 2 of        *
 * the License or (at your option) version 3 or any later version        *
 * accepted by the membership of KDE e.V. (or its successor approved     *
 * by the membership of KDE e.V.), which shall act as a proxy            *
 * defined in Section 14 of version 3 of the license.                    *
 *                                                                       *
 * This program is distributed in the hope that it will be useful,       *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 * GNU General Public License for more details.                          *
 *                                                                       *
 * You should have received a copy of the GNU General Public License     *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 *************************************************************************/

#include "authenticationwizard.h"
#include "ktp-debug.h"

#include <KTp/OTR/channel-adapter.h>
#include <KLocalizedString>
#include <KNotification>
#include <KIconLoader>
#include <KWindowSystem>

#include <QComboBox>
#include <QLabel>
#include <QLineEdit>
#include <QRadioButton>
#include <QGroupBox>
#include <QProgressBar>
#include <QVBoxLayout>

namespace
{

    class WaitPage: public QWizardPage
    {
        public:

            WaitPage(const QString &text) : canContinue(false)
        {
            canContinue = false;
            setTitle(i18nc("@title","Authenticating contact..."));
            QVBoxLayout *layout = new QVBoxLayout();
            layout->addWidget(new QLabel(text));
            layout->addStretch();
            QProgressBar *progressBar = new QProgressBar();
            progressBar->setMinimum(0);
            progressBar->setMaximum(0);
            layout->addWidget(progressBar);
            layout->addStretch();
            setCommitPage(true);
            setLayout(layout);
        }

            void ready()
            {
                canContinue = true;
            }

        protected:
            virtual bool isComplete() const
            {
                return canContinue;
            }

        private:
            bool canContinue;
    };

    QList<AuthenticationWizard*> wizardList;
}

AuthenticationWizard::AuthenticationWizard(
        KTp::ChannelAdapter *chAdapter,
        const QString &contact,
        QWidget *parent,
        bool initiate,
        const QString &question)
    : QWizard(parent),
    chAdapter(chAdapter),
    contact(contact),
    question(question),
    initiate(initiate)
{

	wizardList.append(this);
	setAttribute(Qt::WA_DeleteOnClose);

	setPage(Page_SelectMethod, createIntroPage());
	setPage(Page_QuestionAnswer, createQAPage());
	setPage(Page_SharedSecret, createSSPage());
	setPage(Page_ManualVerification, createMVPage());
	setPage(Page_Wait1, new WaitPage(i18n("Waiting for <b>%1</b>...", contact)));
	setPage(Page_Wait2, new WaitPage(i18n("Checking if answers match...")));
	setPage(Page_Final, createFinalPage());

	if(!initiate) {
		if(question.isEmpty()) {
			setStartId(Page_SharedSecret);
		} else {
			setStartId(Page_QuestionAnswer);
		}
	}

	connect(this, SIGNAL(rejected()), this, SLOT(cancelVerification()));
	connect(rbQA, SIGNAL(clicked()), this, SLOT(updateInfoBox()));
	connect(rbSS, SIGNAL(clicked()), this, SLOT(updateInfoBox()));
	connect(rbMV, SIGNAL(clicked()), this, SLOT(updateInfoBox()));

	updateInfoBox();

	resize(rbMV->width() * 1.05, rbMV->width() * 0.5);
	show();
}


AuthenticationWizard::~AuthenticationWizard()
{
	wizardList.removeAll(this);
}

AuthenticationWizard *AuthenticationWizard::findWizard(KTp::ChannelAdapter *chAdapter)
{
	for(int i = 0; i < wizardList.size(); i++) {
		if(wizardList.at(i)->chAdapter == chAdapter) {
			return wizardList.at(i);
		}
	}
	return 0;
}

QWizardPage *AuthenticationWizard::createIntroPage()
{

	QWizardPage *page = new QWizardPage();
	page->setTitle(i18nc("@title", "Select authentication method"));

	rbQA = new QRadioButton(i18n("Question and Answer"));
	rbSS = new QRadioButton(i18n("Shared Secret"));
	rbMV = new QRadioButton(i18n("Manual fingerprint verification"));

	QGroupBox *frame = new QGroupBox();
	QVBoxLayout *frameLayout = new QVBoxLayout();
	frame->setLayout(frameLayout);
	infoLabel = new QLabel();
	infoLabel->setWordWrap(true);
	frameLayout->addWidget(infoLabel);

	QVBoxLayout *layout = new QVBoxLayout();
	layout->addWidget(rbQA);
	layout->addWidget(rbSS);
	layout->addWidget(rbMV);

	layout->addSpacing(30);
	layout->addWidget(frame);

	page->setLayout(layout);

	rbQA->setChecked(true);

	return page;
}

QWizardPage *AuthenticationWizard::createQAPage()
{
	QWizardPage *page = new QWizardPage();
	QGridLayout *layout = new QGridLayout();
    layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding), 0, 0);

	if(initiate) {
		page->setTitle(i18nc("@title", "Question and Answer"));

		lQuestion = new QLabel(i18nc("@info", "Enter a question that only <b>%1</b> is able to answer:",
                    contact));
		layout->addWidget(lQuestion);
		leQuestion = new QLineEdit();
		layout->addWidget(leQuestion);
		lAnswer = new QLabel(i18nc("@info", "Enter the answer to your question:"));
		layout->addWidget(lAnswer);
	} else {
		if(!question.isEmpty()) {
			page->setTitle(i18nc("@title", "Authentication with <b>%1</b>", contact));
			lQuestion = new QLabel(i18nc("@info", "<b>%1</b> would like to verify your authentication. "
                        "Please answer the following question in the field below:", contact));
            layout->setRowMinimumHeight(1, 30);
			lQuestion->setWordWrap(true);
			layout->addWidget(lQuestion);
			lAnswer = new QLabel(question);
            QFont font = lAnswer->font();
            font.setItalic(true);
            lAnswer->setFont(font);
			lAnswer->setWordWrap(true);
			layout->addWidget(lAnswer);
		}
	}
	leAnswer = new QLineEdit();
	layout->addWidget(leAnswer);
    layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding), 5, 0);

	page->setLayout(layout);
	page->setCommitPage(true);
	return page;
}

QWizardPage *AuthenticationWizard::createSSPage()
{
	QWizardPage *page = new QWizardPage();
	QGridLayout *layout = new QGridLayout();
    layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding), 0, 0);

	if(initiate) {
		page->setTitle(i18nc("@title", "Shared Secret"));

		layout->addWidget(new QLabel(i18nc("@info", "Enter a secret passphrase known only to you and <b>%1</b>:", contact)));
	} else {
        page->setTitle(i18nc("@title", "Authentication with <b>%1</b>", contact));
		layout->addWidget(new QLabel(i18nc("@info", "Enter the secret passphrase known only to you and <b>%1</b>:", contact)));
	}
	leSecret = new QLineEdit();
	layout->addWidget(leSecret);

    layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding), 4, 0);
	page->setLayout(layout);
	page->setCommitPage(true);
	return page;
}

QWizardPage *AuthenticationWizard::createMVPage()
{
	QWizardPage *page = new QWizardPage();
	page->setTitle(i18nc("@title", "Manual Verification"));

	QGridLayout *layout = new QGridLayout();
    layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding), 0, 0);

	QLabel *lMessage1 = new QLabel(i18nc("@info",
                "Contact <b>%1</b> via another secure channel and verify that the following fingerprint is correct:", contact));
	lMessage1->setWordWrap(true);
	layout->addWidget(lMessage1);
    QLabel *lFingerprint = new QLabel(QLatin1String("<b>") + chAdapter->remoteFingerprint() + QLatin1String("</b>"));
    lFingerprint->setAlignment(Qt::AlignCenter);
    lFingerprint->setTextInteractionFlags(Qt::TextSelectableByMouse);
	layout->addWidget(lFingerprint);

	cbManualAuth = new QComboBox();
	cbManualAuth->addItem(i18nc("@item:inlistbox ...verified that", "I have not"));
	cbManualAuth->addItem(i18nc("@item:inlistbox ...verified that", "I have"));
	cbManualAuth->setSizeAdjustPolicy(QComboBox::AdjustToContents);

	if(chAdapter->otrTrustLevel() == KTp::OTRTrustLevelPrivate) {
		cbManualAuth->setCurrentIndex(1);
	} else {
		cbManualAuth->setCurrentIndex(0);
	}

	QLabel *lMessage2 = new QLabel(i18nc("@info:label I have...",
                "verified that this is in fact the correct fingerprint for <b>%1</b>.", contact));
	lMessage2->setWordWrap(true);

	QHBoxLayout *verifyLayout = new QHBoxLayout();
	verifyLayout->addWidget(cbManualAuth, 0, Qt::AlignLeft);
    verifyLayout->addSpacing(5);
	verifyLayout->addWidget(lMessage2, 1);

	QFrame *frame = new QFrame();
	frame->setLayout(verifyLayout);
	layout->addWidget(frame);

    layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding), 6, 0);
    layout->setVerticalSpacing(15);
	page->setLayout(layout);

	return page;
}

QWizardPage *AuthenticationWizard::createFinalPage()
{
	QWizardPage *page = new QWizardPage();
	QGridLayout *layout = new QGridLayout();
    layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding), 0, 0);

	lFinal = new QLabel();
	lFinal->setWordWrap(true);
	layout->addWidget(lFinal);
    layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding), 2, 0);

	page->setLayout(layout);

	return page;
}

int AuthenticationWizard::nextId() const
{
	if(currentId() == Page_SelectMethod) {
		if(rbQA->isChecked())
			return Page_QuestionAnswer;
		if(rbSS->isChecked())
			return Page_SharedSecret;
		if(rbMV->isChecked())
			return Page_ManualVerification;
	}
	if(currentId() == Page_SharedSecret || currentId() == Page_QuestionAnswer) {
		if(initiate) {
			return Page_Wait1;
		} else {
			return Page_Wait2;
		}
	}
	if(currentId() == Page_Wait1) {
		return Page_Wait2;
	}
	if(currentId() == Page_Wait2) {
		return Page_Final;
	}
	return -1;
}

bool AuthenticationWizard::validateCurrentPage()
{
	qCDebug(KTP_TEXTUI_LIB) << "currentId:" << currentId();
	switch(currentId()) {
		case 1:
			if(initiate) {
                chAdapter->startPeerAuthenticationQA(leQuestion->text(), leAnswer->text());
			} else {
                chAdapter->respondPeerAuthentication(leAnswer->text());
			}
			break;
		case 2:
			if(initiate) {
                chAdapter->startPeerAuthenticationSS(leSecret->text());
			} else {
                chAdapter->respondPeerAuthentication(leSecret->text());
			}
			break;
		case 3:
			if(cbManualAuth->currentIndex() == 0 ) {
                chAdapter->trustFingerprint(chAdapter->remoteFingerprint(), false);
			} else {
                chAdapter->trustFingerprint(chAdapter->remoteFingerprint(), true);
			}
			break;
	}
	return true;
}

void AuthenticationWizard::cancelVerification()
{
	qCDebug(KTP_TEXTUI_LIB) << "cancelVerification...";
	if(!initiate){
        chAdapter->abortPeerAuthentication();
	}
}

void AuthenticationWizard::nextState()
{
    qCDebug(KTP_TEXTUI_LIB);
	if(currentId() == Page_Wait1) {
		static_cast<WaitPage*>(currentPage())->ready();
		next();
	}
}

void AuthenticationWizard::finished(bool success)
{
	qCDebug(KTP_TEXTUI_LIB) << "authWizard finished";
	if(currentId() == Page_Wait2){
		qCDebug(KTP_TEXTUI_LIB) << "Yes, in wait_page2";
		static_cast<WaitPage*>(currentPage())->ready();
		next();
		if(success) {
			qCDebug(KTP_TEXTUI_LIB) << "auth succeeded";
			currentPage()->setTitle(i18n("Authentication successful"));
			if(!question.isEmpty()|| rbQA->isChecked()) {
				if(initiate){
					qCDebug(KTP_TEXTUI_LIB) << "initiate";
					lFinal->setText(i18n("The authentication with <b>%1</b> has been completed successfully."
                                " The conversation is now secure.", contact));
				} else {
					qCDebug(KTP_TEXTUI_LIB) << "not initiate";
                    lFinal->setText(i18n("<b>%1</b> has successfully authenticated you."
                                " You may want to authenticate this contact as well by asking your own question.", contact));
				}
			} else {
				lFinal->setText(i18n("The authentication with <b>%1</b> has been completed successfully. "
                            "The conversation is now secure.", contact));
			}
		} else {
			currentPage()->setTitle(i18n("Authentication failed"));
			lFinal->setText(i18n("The authentication with <b>%1</b> has failed."
                        " To make sure you are not talking to an imposter, "
                        "try again using the manual fingerprint verification method."
                        " Note that the conversation is now insecure.", contact));
		}
	}

	setOption(QWizard::NoCancelButton, true);

}

void AuthenticationWizard::aborted()
{
	if(currentId() == Page_SharedSecret || currentId() == Page_QuestionAnswer) {
		next();
	}
	if(currentId() == Page_Wait1){
		next();
	}
	if(currentId() == Page_Wait2){
		next();
	}
	currentPage()->setTitle(i18n("Authentication aborted"));
	lFinal->setText(i18n("<b>%1</b> has aborted the authentication process."
                " To make sure you are not talking to an imposter, "
                "try again using the manual fingerprint verification method.", contact));

	setOption(QWizard::NoCancelButton, true);
}

void AuthenticationWizard::updateInfoBox(){
	if(rbQA->isChecked()) {
		infoLabel->setText(i18n("Ask <b>%1</b> a question, the answer to which is known only to you and them."
                    " If the answer does not match, you may be talking to an imposter.", contact));
	} else if(rbSS->isChecked()) {
		infoLabel->setText(i18n("Pick a secret known only to you and <b>%1</b>. If the secret does not match, "
                    "you may be talking to an imposter. Do not send the secret through the chat window, "
                    "or this authentication method could be compromised with ease.", contact));
	} else {
		infoLabel->setText(i18n("Verify <b>%1's</b> fingerprint manually. "
                    "For example via a phone call or signed (and verified) email.", contact));
	}
}

void AuthenticationWizard::notificationActivated( unsigned int id)
{
	qCDebug(KTP_TEXTUI_LIB) << "notificationActivated. ButtonId" << id;
	if(id == 1) {
        this->raise();
        KWindowSystem::forceActiveWindow(this->winId());
	}
}