~telepathy-kde/telepathy-kde/ktp-auth-handler

120 by Àlex Fiestas
Merge branch 'sso-google'
1
/*************************************************************************************
2
 *  Copyright (C) 2013 by Alejandro Fiestas Olivares <afiestas@kde.org>              *
3
 *                                                                                   *
4
 *  This library is free software; you can redistribute it and/or                    *
5
 *  modify it under the terms of the GNU Lesser General Public                       *
6
 *  License as published by the Free Software Foundation; either                     *
7
 *  version 2 of the License, or (at your option) any later version.                 *
8
 *                                                                                   *
9
 *  This library is distributed in the hope that it will be useful,                  *
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                *
12
 *  Library General Public License for more details.                                 *
13
 *                                                                                   *
14
 *  You should have received a copy of the GNU Library General Public License        *
15
 *  along with this library; see the file COPYING.LIB.  If not, write to             *
16
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,             *
17
 *  Boston, MA 02110-1301, USA.                                                      *
18
 *************************************************************************************/
19
20
#include "getcredentialsjob.h"
21
#include "x-telepathy-sso-google-operation.h"
22
23
#include <KDebug>
24
25
XTelepathySSOGoogleOperation::XTelepathySSOGoogleOperation(const Tp::AccountPtr& account, int accountStorageId, Tp::Client::ChannelInterfaceSASLAuthenticationInterface* saslIface)
26
    : PendingOperation(account)
27
    , m_saslIface(saslIface)
28
    , m_accountStorageId(accountStorageId)
29
{
30
    Q_ASSERT(m_accountStorageId);
31
    connect(m_saslIface, SIGNAL(SASLStatusChanged(uint,QString,QVariantMap)), SLOT(onSASLStatusChanged(uint,QString,QVariantMap)));
32
}
33
34
void XTelepathySSOGoogleOperation::onSASLStatusChanged(uint status, const QString& reason, const QVariantMap& details)
35
{
36
    switch (status){
37
    case Tp::SASLStatusNotStarted:
38
    {
39
        kDebug() << "Status Not started";
40
        GetCredentialsJob *job = new GetCredentialsJob(m_accountStorageId, this);
41
        connect(job, SIGNAL(finished(KJob*)), SLOT(gotCredentials(KJob*)));
42
        job->start();
43
        break;
44
    }
45
    case Tp::SASLStatusServerSucceeded:
46
        kDebug() << "Status Server Succeeded";
47
        m_saslIface->AcceptSASL();
48
        break;
49
    }
50
}
51
52
void XTelepathySSOGoogleOperation::gotCredentials(KJob* kjob)
53
{
54
    GetCredentialsJob *job = qobject_cast< GetCredentialsJob* >(kjob);
55
    QVariantMap credentialsData = job->credentialsData();
56
57
    QByteArray data;
58
    data.append("\0", 1);
59
    data.append(credentialsData["AccountUsername"].toByteArray());
60
    data.append("\0", 1);
61
    data.append(credentialsData["AccessToken"].toByteArray());
62
63
    m_saslIface->StartMechanismWithData(QLatin1String("X-OAUTH2"), data);
64
}