File: MockGreeter.cpp

package info (click to toggle)
lomiri 0.5.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 24,088 kB
  • sloc: cpp: 39,498; python: 2,559; javascript: 1,426; ansic: 1,012; sh: 289; xml: 252; makefile: 69
file content (320 lines) | stat: -rw-r--r-- 9,220 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (C) 2014-2017 Canonical Ltd.
 *
 * 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; version 3.
 *
 * 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 "MockController.h"
#include "MockGreeter.h"
#include <QDBusInterface>
#include <QDBusPendingCall>
#include <QTimer>

namespace QLightDM
{

class GreeterPrivate
{
public:
    bool authenticated = false;
    QString authenticationUser;
    bool twoFactorDone = false;
    QTimer authCallbackTimer;
};

Greeter::Greeter(QObject *parent)
  : QObject(parent)
  , d_ptr(new GreeterPrivate)
{
    Q_D(Greeter);

    d->authCallbackTimer.setSingleShot(true);
    connect(&d->authCallbackTimer, &QTimer::timeout,
            this, &QLightDM::Greeter::handleAuthenticate);
}

Greeter::~Greeter()
{
    delete d_ptr;
}

QString Greeter::authenticationUser() const
{
    Q_D(const Greeter);

    return d->authenticationUser;
}

bool Greeter::hasGuestAccountHint() const
{
    return MockController::instance()->hasGuestAccountHint();
}

QString Greeter::getHint(const QString &name) const
{
    Q_UNUSED(name)
    return "";
}

QString Greeter::defaultSessionHint() const
{
    return QStringLiteral("lomiri");
}

bool Greeter::hideUsersHint() const
{
    return MockController::instance()->hideUsersHint();
}

bool Greeter::showManualLoginHint() const
{
    return MockController::instance()->showManualLoginHint();
}

bool Greeter::showRemoteLoginHint() const
{
    return false;
}

QString Greeter::selectUserHint() const
{
    return MockController::instance()->selectUserHint();
}

bool Greeter::selectGuestHint() const
{
    return MockController::instance()->selectGuestHint();
}

QString Greeter::autologinUserHint() const
{
    return "";
}

bool Greeter::autologinGuestHint() const
{
    return false;
}

int Greeter::autologinTimeoutHint() const
{
    return 0;
}

bool Greeter::inAuthentication() const
{
    return false;
}

QString Greeter::hostname() const
{
    return "hostname1";
}

bool Greeter::isAuthenticated() const
{
    Q_D(const Greeter);

    return d->authenticated;
}

bool Greeter::connectSync()
{
    return true;
}

void Greeter::authenticate(const QString &username)
{
    Q_D(Greeter);

    d->authenticated = false;
    d->authenticationUser = username;
    d->twoFactorDone = false;
    d->authCallbackTimer.start();
}

void Greeter::handleAuthenticate()
{
    Q_D(Greeter);

    if (d->authenticated) {
        // This can happen from the guest authentication flow
        Q_EMIT authenticationComplete();
        return;
    }

    if (d->authenticationUser.isEmpty()) {
        Q_EMIT showPrompt("Username: ", Greeter::PromptTypeQuestion);
        return;
    }

    // Send out any messages we need to
    if (d->authenticationUser == "info-prompt")
        Q_EMIT showMessage("Welcome to Lomiri Greeter", Greeter::MessageTypeInfo);
    else if (d->authenticationUser == "wide-info-prompt")
        Q_EMIT showMessage("Welcome to Lomiri Greeter, the greeteriest greeter that ever did appear in these fine lands", Greeter::MessageTypeInfo);
    else if (d->authenticationUser == "html-info-prompt")
        Q_EMIT showMessage("<b>&</b>", Greeter::MessageTypeInfo);
    else if (d->authenticationUser == "long-info-prompt")
        Q_EMIT showMessage("Welcome to Lomiri Greeter\n\nWe like to annoy you with super ridiculously long messages.\nLike this one\n\nThis is the last line of a multiple line message.", Greeter::MessageTypeInfo);
    else if (d->authenticationUser == "multi-info-prompt") {
        Q_EMIT showMessage("Welcome to Lomiri Greeter", Greeter::MessageTypeInfo);
        Q_EMIT showMessage("This is an error", Greeter::MessageTypeError);
        Q_EMIT showMessage("You should have seen three messages", Greeter::MessageTypeInfo);
    }

    // OK, now actually do the prompt
    if (d->authenticationUser == "no-password") {
        d->authenticated = true;
        Q_EMIT authenticationComplete();
    } else if (d->authenticationUser == "has-pin" || d->authenticationUser == "has-pin-clock") {
        Q_EMIT showPrompt("Password: ", Greeter::PromptTypeSecret);
    } else if (d->authenticationUser == "auth-error") {
        d->authenticated = false;
        Q_EMIT authenticationComplete();
    } else if (d->authenticationUser == "different-prompt") {
        Q_EMIT showPrompt("Secret word: ", Greeter::PromptTypeSecret);
    } else if (d->authenticationUser == "question-prompt") {
        Q_EMIT showPrompt("Favorite Color (blue): ", Greeter::PromptTypeQuestion);
    } else if (d->authenticationUser == "two-prompts") {
        Q_EMIT showPrompt("Favorite Color (blue):", Greeter::PromptTypeQuestion);
        Q_EMIT showPrompt("Password: ", Greeter::PromptTypeSecret);
    } else if (d->authenticationUser == "wacky-prompts") {
        Q_EMIT showMessage("First message", Greeter::MessageTypeInfo);
        Q_EMIT showPrompt("Favorite Color (blue)", Greeter::PromptTypeQuestion);
        Q_EMIT showMessage("Second message", Greeter::MessageTypeError);
        Q_EMIT showPrompt("Password: ", Greeter::PromptTypeSecret);
        Q_EMIT showMessage("Last message", Greeter::MessageTypeInfo);
    } else {
        Q_EMIT showPrompt("Password: ", Greeter::PromptTypeSecret);
    }
}

void Greeter::authenticateAsGuest()
{
    Q_D(Greeter);

    d->authenticated = true;
    d->authenticationUser = QString(); // this is what the real liblightdm does
    d->twoFactorDone = false;
    d->authCallbackTimer.start();
}

void Greeter::authenticateAutologin()
{}

void Greeter::authenticateRemote(const QString &session, const QString &username)
{
    Q_UNUSED(session)
    Q_UNUSED(username)
}

void Greeter::cancelAuthentication()
{
    Q_D(Greeter);
    d->authCallbackTimer.stop();
}

void Greeter::setLanguage (const QString &language)
{
    Q_UNUSED(language)
}

bool Greeter::startSessionSync(const QString &session)
{
    Q_UNUSED(session)

    // Send a request to hide the greeter.  This is normally done by logind,
    // but when testing, we don't want the bother of mocking that out. Instead,
    // just send the request directly ourselves.
    QDBusInterface iface("com.lomiri.LomiriGreeter",
                         "/com/lomiri/LomiriGreeter",
                         "com.lomiri.LomiriGreeter",
                         QDBusConnection::sessionBus());
    iface.asyncCall("HideGreeter");

    return true;
}

void Greeter::respond(const QString &response)
{
    Q_D(Greeter);

    if (d->authenticationUser.isEmpty()) {
        // A manual login, our first question was which username
        d->authenticationUser = response;
        handleAuthenticate();
        return;
    }

    if (d->authenticationUser == "no-response") {
        return;
    } else if (d->authenticationUser == "locked") {
        Q_EMIT showMessage("Account is locked", Greeter::MessageTypeError);
        sendAuthenticationComplete();
        return;
    } else if (d->authenticationUser == "two-factor") {
        if (!d->twoFactorDone) {
            if (response == "password") {
                d->twoFactorDone = true;
                Q_EMIT showPrompt("otp", Greeter::PromptTypeQuestion);
            } else {
                d->authenticated = false;
                sendAuthenticationComplete();
            }
        } else {
            d->authenticated = (response == "otp");
            sendAuthenticationComplete();
        }
        return;
    } else if (d->authenticationUser == "two-prompts" || d->authenticationUser == "wacky-prompts") {
        if (!d->twoFactorDone) {
            d->authenticated = (response == "blue");
            d->twoFactorDone = true;
        } else {
            d->authenticated = d->authenticated && (response == "password");
            sendAuthenticationComplete();
        }
        return;
    }

    if (d->authenticationUser == "has-pin" || d->authenticationUser == "has-pin-clock") {
        QString expectedPincode = "";
        for (int i = 1; i < response.size()+1; ++i) {
            expectedPincode = expectedPincode + QString::number(i);
        }
        d->authenticated = (response == expectedPincode);
    } else if (d->authenticationUser == "question-prompt") {
        d->authenticated = (response == "blue");
    } else {
        d->authenticated = (response == "password");
    }

    if (d->authenticationUser == "info-after-login" && d->authenticated) {
        Q_EMIT showMessage("Congratulations on logging in!", Greeter::MessageTypeInfo);
    }

    sendAuthenticationComplete();
}

void Greeter::sendAuthenticationComplete()
{
    if (qEnvironmentVariableIsEmpty("LOMIRI_TESTING")) {
        // simulate PAM's delay
        QTimer::singleShot(1000, this, &Greeter::authenticationComplete);
    } else {
        Q_EMIT authenticationComplete();
    }
}

}