| 1 | /* |
| 2 | * This file is part of lomiri-online-accounts-service |
| 3 | * |
| 4 | * Copyright (C) 2012 Canonical Ltd. |
| 5 | * |
| 6 | * Contact: Alberto Mardegan <[email protected]> |
| 7 | * |
| 8 | * This program is free software: you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License version 3, as published |
| 10 | * by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranties of |
| 14 | * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "reauthenticator.h" |
| 22 | |
| 23 | #include <online-accounts-common/debug.h> |
| 24 | |
| 25 | #include <SignOn/AuthSession> |
| 26 | #include <SignOn/Identity> |
| 27 | |
| 28 | using namespace SignOnUi; |
| 29 | using namespace SignOn; |
| 30 | |
| 31 | namespace SignOnUi { |
| 32 | |
| 33 | class ReauthenticatorPrivate: public QObject |
| 34 | { |
| 35 | Q_OBJECT |
| 36 | Q_DECLARE_PUBLIC(Reauthenticator) |
| 37 | |
| 38 | public: |
| 39 | ReauthenticatorPrivate(const QList<AuthData> &clientData, |
| 40 | const QVariantMap &extraParameters, |
| 41 | Reauthenticator *reauthenticator); |
| 42 | ~ReauthenticatorPrivate(); |
| 43 | |
| 44 | void start(); |
| 45 | |
| 46 | private: |
| 47 | void checkFinished(); |
| 48 | |
| 49 | private Q_SLOTS: |
| 50 | void onError(const SignOn::Error &error); |
| 51 | void onResponse(const SignOn::SessionData &response); |
| 52 | |
| 53 | private: |
| 54 | mutable Reauthenticator *q_ptr; |
| 55 | QList<AuthData> m_clientData; |
| 56 | QVariantMap m_extraParameters; |
| 57 | int m_errorCount; |
| 58 | int m_responseCount; |
| 59 | }; |
| 60 | |
| 61 | } // namespace |
| 62 | |
| 63 | ReauthenticatorPrivate::ReauthenticatorPrivate( |
| 64 | const QList<AuthData> &clientData, |
| 65 | const QVariantMap &extraParameters, |
| 66 | Reauthenticator *request): |
| 67 | QObject(request), |
| 68 | q_ptr(request), |
| 69 | m_clientData(clientData), |
| 70 | m_extraParameters(extraParameters), |
| 71 | m_errorCount(0), |
| 72 | m_responseCount(0) |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | ReauthenticatorPrivate::~ReauthenticatorPrivate() |
| 77 | { |
| 78 | } |
| 79 | |
| 80 | void ReauthenticatorPrivate::start() |
| 81 | { |
| 82 | Q_FOREACH(const AuthData &authData, m_clientData) { |
| 83 | Identity *identity = |
| 84 | Identity::existingIdentity(authData.identity, this); |
| 85 | if (identity == 0) { m_errorCount++; continue; } |
| 86 | |
| 87 | AuthSession *authSession = identity->createSession(authData.method); |
| 88 | if (authSession == 0) { m_errorCount++; continue; } |
| 89 | |
| 90 | QObject::connect(authSession, |
| 91 | SIGNAL(error(const SignOn::Error &)), |
| 92 | this, |
| 93 | SLOT(onError(const SignOn::Error &))); |
| 94 | QObject::connect(authSession, |
| 95 | SIGNAL(response(const SignOn::SessionData &)), |
| 96 | this, |
| 97 | SLOT(onResponse(const SignOn::SessionData &))); |
| 98 | |
| 99 | /* Prepare the session data, adding the extra parameters. */ |
| 100 | QVariantMap sessionData = authData.sessionData; |
| 101 | QVariantMap::const_iterator i; |
| 102 | for (i = m_extraParameters.constBegin(); |
| 103 | i != m_extraParameters.constEnd(); |
| 104 | i++) { |
| 105 | sessionData[i.key()] = i.value(); |
| 106 | } |
| 107 | |
| 108 | /* Start the session right now; signon-ui is queueing them anyway. */ |
| 109 | authSession->process(sessionData, authData.mechanism); |
| 110 | } |
| 111 | |
| 112 | checkFinished(); |
| 113 | } |
| 114 | |
| 115 | void ReauthenticatorPrivate::checkFinished() |
| 116 | { |
| 117 | Q_Q(Reauthenticator); |
| 118 | |
| 119 | if (m_errorCount + m_responseCount < m_clientData.count()) return; |
| 120 | Q_EMIT q->finished(m_errorCount == 0); |
| 121 | } |
| 122 | |
| 123 | void ReauthenticatorPrivate::onError(const SignOn::Error &error) |
| 124 | { |
| 125 | DEBUG() << "Got error:" << error.message(); |
| 126 | |
| 127 | m_errorCount++; |
| 128 | checkFinished(); |
| 129 | } |
| 130 | |
| 131 | void ReauthenticatorPrivate::onResponse( |
| 132 | const SignOn::SessionData &response) |
| 133 | { |
| 134 | DEBUG() << "Got response:" << response.toMap(); |
| 135 | |
| 136 | m_responseCount++; |
| 137 | checkFinished(); |
| 138 | } |
| 139 | |
| 140 | Reauthenticator::Reauthenticator(const QList<AuthData> &clientData, |
| 141 | const QVariantMap &extraParameters, |
| 142 | QObject *parent): |
| 143 | QObject(parent), |
| 144 | d_ptr(new ReauthenticatorPrivate(clientData, extraParameters, this)) |
| 145 | { |
| 146 | } |
| 147 | |
| 148 | Reauthenticator::~Reauthenticator() |
| 149 | { |
| 150 | } |
| 151 | |
| 152 | void Reauthenticator::start() |
| 153 | { |
| 154 | Q_D(Reauthenticator); |
| 155 | d->start(); |
| 156 | } |
| 157 | |
| 158 | #include "reauthenticator.moc" |