<commit_before>/*************************************************************************************
 *  Copyright (C) 2013 by <NAME> <<EMAIL>>              *
 *                                                                                   *
 *  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 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                *
 *  Library General Public License for more details.                                 *
 *                                                                                   *
 *  You should have received a copy of the GNU Library General Public License        *
 *  along with this library; see the file COPYING.LIB.  If not, write to             *
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,             *
 *  Boston, MA 02110-1301, USA.                                                      *
 *************************************************************************************/

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

#include <KAccounts/getcredentialsjob.h>
#include <QDebug>

#include <KSharedConfig>
#include <KConfigGroup>

XTelepathySSOGoogleOperation::XTelepathySSOGoogleOperation(const Tp::AccountPtr &account, int accountStorageId, Tp::Client::ChannelInterfaceSASLAuthenticationInterface *saslIface)
    : PendingOperation(account)
    , m_saslIface(saslIface)
    , m_accountStorageId(accountStorageId)
{
    connect(m_saslIface, SIGNAL(SASLStatusChanged(uint,QString,QVariantMap)), SLOT(onSASLStatusChanged(uint,QString,QVariantMap)));
}

void XTelepathySSOGoogleOperation::onSASLStatusChanged(uint status, const QString &reason, const QVariantMap &details)
{
    switch (status){
    case Tp::SASLStatusNotStarted:
    {
        qDebug() << "Status Not started";
        GetCredentialsJob *job = new GetCredentialsJob(m_accountStorageId, QStringLiteral("oauth2"), QStringLiteral("web_server"), this);
        connect(job, SIGNAL(finished(KJob*)), SLOT(gotCredentials(KJob*)));
        job->start();
        break;
    }
    case Tp::SASLStatusServerSucceeded:
        qDebug() << "Status Server Succeeded";
        m_saslIface->AcceptSASL();
        break;

    case Tp::SASLStatusSucceeded:
        qDebug() << "Authentication succeeded";
        setFinished();
        break;
    }
}

void XTelepathySSOGoogleOperation::gotCredentials(KJob *kjob)
{
    GetCredentialsJob *job = qobject_cast< GetCredentialsJob* >(kjob);
    QVariantMap credentialsData = job->credentialsData();

    QByteArray data;
    data.append("\0", 1);
    data.append(credentialsData["AccountUsername"].toByteArray());
    data.append("\0", 1);
    data.append(credentialsData["AccessToken"].toByteArray());

    qDebug() << "Received Google credentials, starting auth mechanism...";

    m_saslIface->StartMechanismWithData(QLatin1String("X-OAUTH2"), data);
}<commit_msg>Handle Tp::SASLStatusServerFailed in Google auth operation<commit_after>/*************************************************************************************
 *  Copyright (C) 2013 by <NAME> <<EMAIL>>              *
 *                                                                                   *
 *  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 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                *
 *  Library General Public License for more details.                                 *
 *                                                                                   *
 *  You should have received a copy of the GNU Library General Public License        *
 *  along with this library; see the file COPYING.LIB.  If not, write to             *
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,             *
 *  Boston, MA 02110-1301, USA.                                                      *
 *************************************************************************************/

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

#include <KAccounts/getcredentialsjob.h>
#include <QDebug>

#include <KSharedConfig>
#include <KConfigGroup>
#include <KLocalizedString>

XTelepathySSOGoogleOperation::XTelepathySSOGoogleOperation(const Tp::AccountPtr &account, int accountStorageId, Tp::Client::ChannelInterfaceSASLAuthenticationInterface *saslIface)
    : PendingOperation(account)
    , m_saslIface(saslIface)
    , m_accountStorageId(accountStorageId)
{
    connect(m_saslIface, SIGNAL(SASLStatusChanged(uint,QString,QVariantMap)), SLOT(onSASLStatusChanged(uint,QString,QVariantMap)));
}

void XTelepathySSOGoogleOperation::onSASLStatusChanged(uint status, const QString &reason, const QVariantMap &details)
{
    switch (status){
    case Tp::SASLStatusNotStarted:
    {
        qDebug() << "Status Not started";
        GetCredentialsJob *job = new GetCredentialsJob(m_accountStorageId, QStringLiteral("oauth2"), QStringLiteral("web_server"), this);
        connect(job, SIGNAL(finished(KJob*)), SLOT(gotCredentials(KJob*)));
        job->start();
        break;
    }
    case Tp::SASLStatusServerSucceeded:
        qDebug() << "Status Server Succeeded";
        m_saslIface->AcceptSASL();
        break;

    case Tp::SASLStatusSucceeded:
        qDebug() << "Authentication succeeded";
        setFinished();
        break;
    case Tp::SASLStatusServerFailed:
        qDebug() << "Auth failed";
        QString errorMessage = details[QLatin1String("server-message")].toString();
        if (errorMessage.isEmpty()) {
            errorMessage = details[QLatin1String("debug-message")].toString();
        }
        setFinishedWithError(reason, errorMessage.isEmpty() ? i18n("Authentication error") : errorMessage);
    }
}

void XTelepathySSOGoogleOperation::gotCredentials(KJob *kjob)
{
    GetCredentialsJob *job = qobject_cast< GetCredentialsJob* >(kjob);
    QVariantMap credentialsData = job->credentialsData();

    QByteArray data;
    data.append("\0", 1);
    data.append(credentialsData["AccountUsername"].toByteArray());
    data.append("\0", 1);
    data.append(credentialsData["AccessToken"].toByteArray());

    qDebug() << "Received Google credentials, starting auth mechanism...";

    m_saslIface->StartMechanismWithData(QLatin1String("X-OAUTH2"), data);
}
{'fraction_non_alphanumeric': 0.13813282525857376, 'fraction_numerical': 0.006804572672836146, 'mean_word_length': 2.446998123827392, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4135754', 'n_tokens_mistral': 1904, 'n_tokens_neox': 1844, 'n_words': 468}