| 1 | /* |
| 2 | * This file is part of OnlineAccountsDaemon |
| 3 | * |
| 4 | * Copyright (C) 2015 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 Lesser General Public License version 3, as |
| 10 | * published 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 Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "access_request.h" |
| 22 | |
| 23 | #include <Accounts/AuthData> |
| 24 | #include <LomiriOnlineAccountsClient/Setup> |
| 25 | #include <QDBusArgument> |
| 26 | #include <QDebug> |
| 27 | #include "account_info.h" |
| 28 | #include "authenticator.h" |
| 29 | |
| 30 | using namespace OnlineAccountsDaemon; |
| 31 | |
| 32 | namespace OnlineAccountsDaemon { |
| 33 | |
| 34 | class AccessRequestPrivate: public QObject |
| 35 | { |
| 36 | Q_OBJECT |
| 37 | Q_DECLARE_PUBLIC(AccessRequest) |
| 38 | |
| 39 | public: |
| 40 | AccessRequestPrivate(AccessRequest *q); |
| 41 | |
| 42 | void requestAccess(const QString &applicationId, |
| 43 | const QString &serviceId, |
| 44 | const QVariantMap ¶meters, |
| 45 | pid_t clientPid); |
| 46 | |
| 47 | private Q_SLOTS: |
| 48 | void onSetupFinished(QVariantMap reply); |
| 49 | void onAuthenticationFinished(); |
| 50 | |
| 51 | private: |
| 52 | OnlineAccountsClient::Setup m_setup; |
| 53 | Authenticator m_authenticator; |
| 54 | QVariantMap m_parameters; |
| 55 | AccountInfo m_accountInfo; |
| 56 | AccessRequest *q_ptr; |
| 57 | }; |
| 58 | |
| 59 | } // namespace |
| 60 | |
| 61 | AccessRequestPrivate::AccessRequestPrivate(AccessRequest *q): |
| 62 | QObject(q), |
| 63 | q_ptr(q) |
| 64 | { |
| 65 | QObject::connect(&m_setup, SIGNAL(finished(QVariantMap)), |
| 66 | this, SLOT(onSetupFinished(QVariantMap))); |
| 67 | QObject::connect(&m_authenticator, SIGNAL(finished()), |
| 68 | this, SLOT(onAuthenticationFinished())); |
| 69 | } |
| 70 | |
| 71 | void AccessRequestPrivate::requestAccess(const QString &applicationId, |
| 72 | const QString &serviceId, |
| 73 | const QVariantMap ¶meters, |
| 74 | pid_t clientPid) |
| 75 | { |
| 76 | m_parameters = parameters; |
| 77 | |
| 78 | m_setup.setApplicationId(applicationId); |
| 79 | m_setup.setServiceId(serviceId); |
| 80 | m_setup.setClientPid(clientPid); |
| 81 | m_setup.exec(); |
| 82 | } |
| 83 | |
| 84 | void AccessRequestPrivate::onSetupFinished(QVariantMap reply) |
| 85 | { |
| 86 | Q_Q(AccessRequest); |
| 87 | uint accountId = reply[QStringLiteral("accountId")].toUInt(); |
| 88 | if (accountId == 0) { |
| 89 | q->setError(ONLINE_ACCOUNTS_ERROR_PERMISSION_DENIED, |
| 90 | "Authorization was not granted"); |
| 91 | } else { |
| 92 | Q_EMIT q->loadRequest(accountId, m_setup.serviceId()); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void AccessRequestPrivate::onAuthenticationFinished() |
| 97 | { |
| 98 | Q_Q(AccessRequest); |
| 99 | |
| 100 | /* We don't check for authentication errors here. Even if an error |
| 101 | * occurred, we still have to return the account info to the client. |
| 102 | * So, if an error occurred, we'll just return empty authentication data to |
| 103 | * the client. |
| 104 | */ |
| 105 | QList<QVariant> args; |
| 106 | QDBusArgument infoArg; |
| 107 | infoArg << m_accountInfo; |
| 108 | args << QVariant::fromValue(infoArg); |
| 109 | args << m_authenticator.reply(); |
| 110 | q->setReply(args); |
| 111 | } |
| 112 | |
| 113 | AccessRequest::AccessRequest(const CallContext &context, QObject *parent): |
| 114 | AsyncOperation(context, parent), |
| 115 | d_ptr(new AccessRequestPrivate(this)) |
| 116 | { |
| 117 | } |
| 118 | |
| 119 | AccessRequest::~AccessRequest() |
| 120 | { |
| 121 | delete d_ptr; |
| 122 | } |
| 123 | |
| 124 | void AccessRequest::requestAccess(const QString &applicationId, |
| 125 | const QString &serviceId, |
| 126 | const QVariantMap ¶meters, |
| 127 | pid_t clientPid) |
| 128 | { |
| 129 | Q_D(AccessRequest); |
| 130 | d->requestAccess(applicationId, serviceId, parameters, clientPid); |
| 131 | } |
| 132 | |
| 133 | void AccessRequest::setAccountInfo(const AccountInfo &accountInfo, |
| 134 | const Accounts::AuthData &authData) |
| 135 | { |
| 136 | Q_D(AccessRequest); |
| 137 | d->m_accountInfo = accountInfo; |
| 138 | d->m_authenticator.authenticate(authData, d->m_parameters); |
| 139 | } |
| 140 | |
| 141 | #include "access_request.moc" |