File indexing completed on 2023-10-03 04:18:00
0001 /************************************************************************************* 0002 * Copyright (C) 2013 by Alejandro Fiestas Olivares <afiestas@kde.org> * 0003 * * 0004 * This library is free software; you can redistribute it and/or * 0005 * modify it under the terms of the GNU Lesser General Public * 0006 * License as published by the Free Software Foundation; either * 0007 * version 2 of the License, or (at your option) any later version. * 0008 * * 0009 * This library is distributed in the hope that it will be useful, * 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 0012 * Library General Public License for more details. * 0013 * * 0014 * You should have received a copy of the GNU Library General Public License * 0015 * along with this library; see the file COPYING.LIB. If not, write to * 0016 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * 0017 * Boston, MA 02110-1301, USA. * 0018 *************************************************************************************/ 0019 0020 #include "x-telepathy-sso-google-operation.h" 0021 0022 #include <KAccounts/getcredentialsjob.h> 0023 #include <QDebug> 0024 0025 #include <KSharedConfig> 0026 #include <KConfigGroup> 0027 #include <KLocalizedString> 0028 0029 XTelepathySSOGoogleOperation::XTelepathySSOGoogleOperation(const Tp::AccountPtr &account, int accountStorageId, Tp::Client::ChannelInterfaceSASLAuthenticationInterface *saslIface) 0030 : PendingOperation(account) 0031 , m_saslIface(saslIface) 0032 , m_accountStorageId(accountStorageId) 0033 { 0034 connect(m_saslIface, SIGNAL(SASLStatusChanged(uint,QString,QVariantMap)), SLOT(onSASLStatusChanged(uint,QString,QVariantMap))); 0035 } 0036 0037 void XTelepathySSOGoogleOperation::onSASLStatusChanged(uint status, const QString &reason, const QVariantMap &details) 0038 { 0039 switch (status){ 0040 case Tp::SASLStatusNotStarted: 0041 { 0042 qDebug() << "Status Not started"; 0043 GetCredentialsJob *job = new GetCredentialsJob(m_accountStorageId, QStringLiteral("oauth2"), QStringLiteral("web_server"), this); 0044 connect(job, SIGNAL(finished(KJob*)), SLOT(gotCredentials(KJob*))); 0045 job->start(); 0046 break; 0047 } 0048 case Tp::SASLStatusServerSucceeded: 0049 qDebug() << "Status Server Succeeded"; 0050 m_saslIface->AcceptSASL(); 0051 break; 0052 0053 case Tp::SASLStatusSucceeded: 0054 qDebug() << "Authentication succeeded"; 0055 setFinished(); 0056 break; 0057 case Tp::SASLStatusServerFailed: 0058 qDebug() << "Auth failed"; 0059 QString errorMessage = details[QLatin1String("server-message")].toString(); 0060 if (errorMessage.isEmpty()) { 0061 errorMessage = details[QLatin1String("debug-message")].toString(); 0062 } 0063 setFinishedWithError(reason, errorMessage.isEmpty() ? i18n("Authentication error") : errorMessage); 0064 } 0065 } 0066 0067 void XTelepathySSOGoogleOperation::gotCredentials(KJob *kjob) 0068 { 0069 GetCredentialsJob *job = qobject_cast< GetCredentialsJob* >(kjob); 0070 QVariantMap credentialsData = job->credentialsData(); 0071 0072 QByteArray data; 0073 data.append("\0", 1); 0074 data.append(credentialsData["AccountUsername"].toByteArray()); 0075 data.append("\0", 1); 0076 data.append(credentialsData["AccessToken"].toByteArray()); 0077 0078 qDebug() << "Received Google credentials, starting auth mechanism..."; 0079 0080 m_saslIface->StartMechanismWithData(QLatin1String("X-OAUTH2"), data); 0081 }