| 1 | /* |
| 2 | * This file is part of libOnlineAccounts |
| 3 | * |
| 4 | * Copyright (C) 2015-2016 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 "authentication_data.h" |
| 22 | |
| 23 | #include <QDBusArgument> |
| 24 | #include <QDBusMessage> |
| 25 | #include <QDebug> |
| 26 | #include <LomiriOnlineAccountsDaemon/dbus_constants.h> |
| 27 | #include "error_p.h" |
| 28 | #include "pending_call_p.h" |
| 29 | |
| 30 | using namespace OnlineAccounts; |
| 31 | |
| 32 | static QVariant expandDBusArguments(const QVariant &variant) |
| 33 | { |
| 34 | if (variant.userType() == qMetaTypeId<QDBusArgument>()) { |
| 35 | QDBusArgument argument = variant.value<QDBusArgument>(); |
| 36 | if (argument.currentType() == QDBusArgument::MapType) { |
| 37 | /* Assume that all maps are a{sv} */ |
| 38 | QVariantMap map = qdbus_cast<QVariantMap>(argument); |
| 39 | QVariantMap expandedMap; |
| 40 | QMapIterator<QString, QVariant> it(map); |
| 41 | while (it.hasNext()) { |
| 42 | it.next(); |
| 43 | expandedMap.insert(it.key(), expandDBusArguments(it.value())); |
| 44 | } |
| 45 | return expandedMap; |
| 46 | } else if (argument.currentType() == QDBusArgument::ArrayType) { |
| 47 | if (argument.currentSignature() == "aay") { |
| 48 | QList<QByteArray> arrayList; |
| 49 | argument >> arrayList; |
| 50 | return QVariant::fromValue(arrayList); |
| 51 | } else { |
| 52 | /* We don't know how to handle other types */ |
| 53 | qCWarning(DBG_ONLINE_ACCOUNTS) << "unhandled type" << |
| 54 | argument.currentSignature(); |
| 55 | return argument.asVariant(); |
| 56 | } |
| 57 | } else { |
| 58 | /* We don't know how to handle other types */ |
| 59 | return argument.asVariant(); |
| 60 | } |
| 61 | } else { |
| 62 | return variant; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | namespace OnlineAccounts { |
| 67 | |
| 68 | class AuthenticationReplyPrivate |
| 69 | { |
| 70 | public: |
| 71 | inline AuthenticationReplyPrivate(AuthenticationMethod method, |
| 72 | const PendingCall &call); |
| 73 | virtual ~AuthenticationReplyPrivate() {}; |
| 74 | |
| 75 | void setError(const Error &error) { m_error = error; } |
| 76 | |
| 77 | const QVariantMap &data() const { return m_replyData; } |
| 78 | |
| 79 | private: |
| 80 | friend class AuthenticationReply; |
| 81 | AuthenticationMethod m_authenticationMethod; |
| 82 | PendingCall m_pendingCall; |
| 83 | Error m_error; |
| 84 | QVariantMap m_replyData; |
| 85 | }; |
| 86 | |
| 87 | class OAuth2ReplyPrivate: public AuthenticationReplyPrivate |
| 88 | { |
| 89 | }; |
| 90 | |
| 91 | class OAuth1ReplyPrivate: public AuthenticationReplyPrivate |
| 92 | { |
| 93 | }; |
| 94 | |
| 95 | class SaslReplyPrivate: public AuthenticationReplyPrivate |
| 96 | { |
| 97 | }; |
| 98 | |
| 99 | class PasswordReplyPrivate: public AuthenticationReplyPrivate |
| 100 | { |
| 101 | }; |
| 102 | |
| 103 | } // namespace |
| 104 | |
| 105 | AuthenticationReplyPrivate::AuthenticationReplyPrivate(AuthenticationMethod method, |
| 106 | const PendingCall &call): |
| 107 | m_authenticationMethod(method), |
| 108 | m_pendingCall(call) |
| 109 | { |
| 110 | const PendingCallPrivate *pCall = call.d.constData(); |
| 111 | |
| 112 | if (m_authenticationMethod != AuthenticationMethodUnknown && |
| 113 | pCall->authenticationMethod() != AuthenticationMethodUnknown && |
| 114 | m_authenticationMethod != pCall->authenticationMethod()) { |
| 115 | setError(Error(Error::WrongType, "Authentication method mismatch")); |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | if (!m_pendingCall.isFinished()) { |
| 120 | m_pendingCall.waitForFinished(); |
| 121 | } |
| 122 | |
| 123 | if (Q_UNLIKELY(pCall->dbusCall().isError())) { |
| 124 | setError(errorFromDBus(pCall->dbusCall().error())); |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | QDBusMessage msg = pCall->dbusCall().reply(); |
| 129 | PendingCallPrivate::InvokedMethod invokedMethod = pCall->invokedMethod(); |
| 130 | if (invokedMethod == PendingCallPrivate::Authenticate) { |
| 131 | m_replyData = expandDBusArguments(msg.arguments().at(0)).toMap(); |
| 132 | } else if (invokedMethod == PendingCallPrivate::RequestAccess) { |
| 133 | m_replyData = expandDBusArguments(msg.arguments().at(1)).toMap(); |
| 134 | } else { |
| 135 | qFatal("Unknown invoked method %d", invokedMethod); |
| 136 | } |
| 137 | qCDebug(DBG_ONLINE_ACCOUNTS) << "reply data:" << m_replyData; |
| 138 | } |
| 139 | |
| 140 | AuthenticationReply::AuthenticationReply(const PendingCall &call): |
| 141 | d_ptr(new AuthenticationReplyPrivate(AuthenticationMethodUnknown, call)) |
| 142 | { |
| 143 | } |
| 144 | |
| 145 | AuthenticationReply::AuthenticationReply(AuthenticationReplyPrivate *priv): |
| 146 | d_ptr(priv) |
| 147 | { |
| 148 | } |
| 149 | |
| 150 | AuthenticationReply::~AuthenticationReply() |
| 151 | { |
| 152 | delete d_ptr; |
| 153 | } |
| 154 | |
| 155 | Error AuthenticationReply::error() const |
| 156 | { |
| 157 | Q_D(const AuthenticationReply); |
| 158 | return d->m_error; |
| 159 | } |
| 160 | |
| 161 | QVariantMap AuthenticationReply::data() const |
| 162 | { |
| 163 | Q_D(const AuthenticationReply); |
| 164 | return d->data(); |
| 165 | } |
| 166 | |
| 167 | /* OAuth 2.0 */ |
| 168 | |
| 169 | OAuth2Reply::OAuth2Reply(const PendingCall &call): |
| 170 | AuthenticationReply(new AuthenticationReplyPrivate(AuthenticationMethodOAuth2, call)) |
| 171 | { |
| 172 | } |
| 173 | |
| 174 | OAuth2Reply::~OAuth2Reply() |
| 175 | { |
| 176 | } |
| 177 | |
| 178 | QByteArray OAuth2Reply::accessToken() const |
| 179 | { |
| 180 | Q_D(const AuthenticationReply); |
| 181 | return d->data().value(ONLINE_ACCOUNTS_AUTH_KEY_ACCESS_TOKEN).toByteArray(); |
| 182 | } |
| 183 | |
| 184 | int OAuth2Reply::expiresIn() const |
| 185 | { |
| 186 | Q_D(const AuthenticationReply); |
| 187 | return d->data().value(ONLINE_ACCOUNTS_AUTH_KEY_EXPIRES_IN).toInt(); |
| 188 | } |
| 189 | |
| 190 | QList<QByteArray> OAuth2Reply::grantedScopes() const |
| 191 | { |
| 192 | Q_D(const AuthenticationReply); |
| 193 | return d->data().value(ONLINE_ACCOUNTS_AUTH_KEY_GRANTED_SCOPES).value<QList<QByteArray> >(); |
| 194 | } |
| 195 | |
| 196 | /* OAuth 1.0a */ |
| 197 | |
| 198 | OAuth1Reply::OAuth1Reply(const PendingCall &call): |
| 199 | AuthenticationReply(new AuthenticationReplyPrivate(AuthenticationMethodOAuth1, call)) |
| 200 | { |
| 201 | } |
| 202 | |
| 203 | OAuth1Reply::~OAuth1Reply() |
| 204 | { |
| 205 | } |
| 206 | |
| 207 | QByteArray OAuth1Reply::consumerKey() const |
| 208 | { |
| 209 | Q_D(const AuthenticationReply); |
| 210 | return d->data().value(ONLINE_ACCOUNTS_AUTH_KEY_CONSUMER_KEY).toByteArray(); |
| 211 | } |
| 212 | |
| 213 | QByteArray OAuth1Reply::consumerSecret() const |
| 214 | { |
| 215 | Q_D(const AuthenticationReply); |
| 216 | return d->data().value(ONLINE_ACCOUNTS_AUTH_KEY_CONSUMER_SECRET).toByteArray(); |
| 217 | } |
| 218 | |
| 219 | QByteArray OAuth1Reply::token() const |
| 220 | { |
| 221 | Q_D(const AuthenticationReply); |
| 222 | return d->data().value(ONLINE_ACCOUNTS_AUTH_KEY_TOKEN).toByteArray(); |
| 223 | } |
| 224 | |
| 225 | QByteArray OAuth1Reply::tokenSecret() const |
| 226 | { |
| 227 | Q_D(const AuthenticationReply); |
| 228 | return d->data().value(ONLINE_ACCOUNTS_AUTH_KEY_TOKEN_SECRET).toByteArray(); |
| 229 | } |
| 230 | |
| 231 | QByteArray OAuth1Reply::signatureMethod() const |
| 232 | { |
| 233 | Q_D(const AuthenticationReply); |
| 234 | return d->data().value(ONLINE_ACCOUNTS_AUTH_KEY_SIGNATURE_METHOD).toByteArray(); |
| 235 | } |
| 236 | |
| 237 | /* SASL */ |
| 238 | |
| 239 | SaslReply::SaslReply(const PendingCall &call): |
| 240 | AuthenticationReply(new AuthenticationReplyPrivate(AuthenticationMethodSasl, call)) |
| 241 | { |
| 242 | } |
| 243 | |
| 244 | SaslReply::~SaslReply() |
| 245 | { |
| 246 | } |
| 247 | |
| 248 | QString SaslReply::chosenMechanism() const |
| 249 | { |
| 250 | Q_D(const AuthenticationReply); |
| 251 | return d->data().value(ONLINE_ACCOUNTS_AUTH_KEY_CHOSEN_MECHANISM).toString(); |
| 252 | } |
| 253 | |
| 254 | QByteArray SaslReply::response() const |
| 255 | { |
| 256 | Q_D(const AuthenticationReply); |
| 257 | return d->data().value(ONLINE_ACCOUNTS_AUTH_KEY_RESPONSE).toByteArray(); |
| 258 | } |
| 259 | |
| 260 | SaslReply::State SaslReply::state() const |
| 261 | { |
| 262 | Q_D(const AuthenticationReply); |
| 263 | int state = d->data().value(ONLINE_ACCOUNTS_AUTH_KEY_STATE).toInt(); |
| 264 | switch (state) { |
| 265 | case ONLINE_ACCOUNTS_AUTH_SASL_STATE_FINISHED: return Finished; |
| 266 | case ONLINE_ACCOUNTS_AUTH_SASL_STATE_CONTINUE: return Continue; |
| 267 | default: |
| 268 | qCWarning(DBG_ONLINE_ACCOUNTS) << "Unknown SASL state" << state; |
| 269 | return Finished; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /* Password */ |
| 274 | |
| 275 | PasswordReply::PasswordReply(const PendingCall &call): |
| 276 | AuthenticationReply(new AuthenticationReplyPrivate(AuthenticationMethodPassword, call)) |
| 277 | { |
| 278 | } |
| 279 | |
| 280 | PasswordReply::~PasswordReply() |
| 281 | { |
| 282 | } |
| 283 | |
| 284 | QByteArray PasswordReply::username() const |
| 285 | { |
| 286 | Q_D(const AuthenticationReply); |
| 287 | return d->data().value(ONLINE_ACCOUNTS_AUTH_KEY_USERNAME).toByteArray(); |
| 288 | } |
| 289 | |
| 290 | QByteArray PasswordReply::password() const |
| 291 | { |
| 292 | Q_D(const AuthenticationReply); |
| 293 | return d->data().value(ONLINE_ACCOUNTS_AUTH_KEY_PASSWORD).toByteArray(); |
| 294 | } |