File: sasl-auth-op.cpp

package info (click to toggle)
ktp-auth-handler 22.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 560 kB
  • sloc: cpp: 1,326; sh: 3; makefile: 2
file content (132 lines) | stat: -rw-r--r-- 5,492 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
/*
 * Copyright (C) 2011 Collabora Ltd. <http://www.collabora.co.uk/>
 *   @author Andre Moreira Magalhaes <andre.magalhaes@collabora.co.uk>
 * Copyright (C) 2011 David Edmundson <kde@davidedmundson.co.uk>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "sasl-auth-op.h"

#include "x-telepathy-password-auth-operation.h"
#include "x-telepathy-sso-google-operation.h"

#include <QtCore/QScopedPointer>

#include <TelepathyQt/PendingVariantMap>

#include <QDebug>
#include <KLocalizedString>

SaslAuthOp::SaslAuthOp(const Tp::AccountPtr &account,
        const Tp::ChannelPtr &channel)
    : Tp::PendingOperation(channel),
      m_account(account),
      m_channel(channel),
      m_saslIface(channel->interface<Tp::Client::ChannelInterfaceSASLAuthenticationInterface>())
{
    //Check if the account has any StorageIdentifier, in which case we will
    //prioritize those mechanism related with KDE Accounts integration
    QScopedPointer<Tp::Client::AccountInterfaceStorageInterface> accountStorageInterface(
        new Tp::Client::AccountInterfaceStorageInterface(m_account->busName(), m_account->objectPath()));

    Tp::PendingVariantMap *pendingMap = accountStorageInterface->requestAllProperties();
    connect(pendingMap, SIGNAL(finished(Tp::PendingOperation*)), SLOT(onGetAccountStorageFetched(Tp::PendingOperation*)));
}

SaslAuthOp::~SaslAuthOp()
{
}

void SaslAuthOp::gotProperties(Tp::PendingOperation *op)
{
    if (m_mechanisms.isEmpty()) {
        if (op->isError()) {
            qWarning() << "Unable to retrieve available SASL mechanisms";
            m_channel->requestClose();
            setFinishedWithError(op->errorName(), op->errorMessage());
            return;
        }

        Tp::PendingVariantMap *pvm = qobject_cast<Tp::PendingVariantMap*>(op);
        m_properties = qdbus_cast<QVariantMap>(pvm->result());
        m_mechanisms = qdbus_cast<QStringList>(m_properties.value(QLatin1String("AvailableMechanisms")));
        qDebug() << m_mechanisms;
    }

    uint status = qdbus_cast<uint>(m_properties.value(QLatin1String("SASLStatus")));
    QString error = qdbus_cast<QString>(m_properties.value(QLatin1String("SASLError")));
    QVariantMap errorDetails = qdbus_cast<QVariantMap>(m_properties.value(QLatin1String("SASLErrorDetails")));

    if (m_mechanisms.contains(QLatin1String("X-OAUTH2"))) {
        qDebug() << "Starting X-OAuth2 auth";
        m_mechanisms.removeAll(QStringLiteral("X-OAUTH2"));
        XTelepathySSOGoogleOperation *authop = new XTelepathySSOGoogleOperation(m_account, m_accountStorageId, m_saslIface);
        connect(authop,
                SIGNAL(finished(Tp::PendingOperation*)),
                SLOT(onAuthOperationFinished(Tp::PendingOperation*)));

        authop->onSASLStatusChanged(status, error, errorDetails);
    } else if (m_mechanisms.contains(QLatin1String("X-TELEPATHY-PASSWORD"))) {
        qDebug() << "Starting Password auth";
        m_mechanisms.removeAll(QStringLiteral("X-TELEPATHY-PASSWORD"));
        Q_EMIT ready(this);
        XTelepathyPasswordAuthOperation *authop = new XTelepathyPasswordAuthOperation(m_account, m_accountStorageId, m_saslIface, qdbus_cast<bool>(m_properties.value(QLatin1String("CanTryAgain"))));
        connect(authop,
                SIGNAL(finished(Tp::PendingOperation*)),
                SLOT(onAuthOperationFinished(Tp::PendingOperation*)));

        authop->onSASLStatusChanged(status, error, errorDetails);
    } else {
        qWarning() << "X-TELEPATHY-PASSWORD, X-OAUTH2 are the only supported SASL mechanism and are not available:" << m_mechanisms;
        m_channel->requestClose();
        setFinishedWithError(TP_QT_ERROR_NOT_IMPLEMENTED,
                QLatin1String("X-TELEPATHY-PASSWORD, X-OAUTH2 are the only supported SASL mechanism and are not available:"));
        return;
    }
}

void SaslAuthOp::onAuthOperationFinished(Tp::PendingOperation *op)
{
    if (op->isError()) {
        if (!m_mechanisms.isEmpty()) {
            // if we have other mechanisms left, try again with different one
            gotProperties(0);
        } else {
            setFinishedWithError(op->errorName(), op->errorMessage());
            m_channel->requestClose();
        }
    } else {
        setFinished();
        m_channel->requestClose();
    }
}

void SaslAuthOp::setReady()
{
    connect(m_saslIface->requestAllProperties(),
            SIGNAL(finished(Tp::PendingOperation*)),
            SLOT(gotProperties(Tp::PendingOperation*)));
}

void SaslAuthOp::onGetAccountStorageFetched(Tp::PendingOperation* op)
{
    Tp::PendingVariantMap *pendingMap = qobject_cast<Tp::PendingVariantMap*>(op);

    m_accountStorageId = pendingMap->result()["StorageIdentifier"].value<QDBusVariant>().variant().toInt();
    qDebug() << m_accountStorageId;

    setReady();
}