| 1 | /* |
| 2 | * This file is part of lomiri-online-accounts-ui |
| 3 | * |
| 4 | * Copyright (C) 2013 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 "browser-request.h" |
| 22 | |
| 23 | #include <online-accounts-common/debug.h> |
| 24 | #include "dialog.h" |
| 25 | #include <online-accounts-common/globals.h> |
| 26 | #include <online-accounts-common/i18n.h> |
| 27 | |
| 28 | #include <LomiriOnlineAccountsPlugin/request-handler.h> |
| 29 | #include <QDir> |
| 30 | #include <QFile> |
| 31 | #include <QJsonArray> |
| 32 | #include <QJsonDocument> |
| 33 | #include <QList> |
| 34 | #include <QQmlContext> |
| 35 | #include <QQmlEngine> |
| 36 | #include <QRegularExpression> |
| 37 | #include <QStandardPaths> |
| 38 | #include <QTimer> |
| 39 | #include <QVariant> |
| 40 | #include <SignOn/uisessiondata.h> |
| 41 | #include <SignOn/uisessiondata_priv.h> |
| 42 | |
| 43 | using namespace SignOnUi; |
| 44 | |
| 45 | #ifndef SIGNONUI_FAIL_TIMEOUT |
| 46 | #define SIGNONUI_FAIL_TIMEOUT 3000 |
| 47 | #endif |
| 48 | |
| 49 | namespace SignOnUi { |
| 50 | |
| 51 | class BrowserRequestPrivate: public QObject |
| 52 | { |
| 53 | Q_OBJECT |
| 54 | Q_DECLARE_PUBLIC(BrowserRequest) |
| 55 | Q_PROPERTY(QString title READ title CONSTANT) |
| 56 | Q_PROPERTY(QUrl pageComponentUrl READ pageComponentUrl CONSTANT) |
| 57 | Q_PROPERTY(QUrl currentUrl READ currentUrl WRITE setCurrentUrl) |
| 58 | Q_PROPERTY(QUrl startUrl READ startUrl CONSTANT) |
| 59 | Q_PROPERTY(QUrl finalUrl READ finalUrl CONSTANT) |
| 60 | Q_PROPERTY(QString rootDir READ rootDir CONSTANT) |
| 61 | |
| 62 | public: |
| 63 | BrowserRequestPrivate(BrowserRequest *request); |
| 64 | ~BrowserRequestPrivate(); |
| 65 | |
| 66 | void start(); |
| 67 | |
| 68 | QString title() const { return q_ptr->windowTitle(); } |
| 69 | void setCurrentUrl(const QUrl &url); |
| 70 | QUrl pageComponentUrl() const; |
| 71 | QUrl currentUrl() const { return m_currentUrl; } |
| 72 | QUrl startUrl() const { return m_startUrl; } |
| 73 | QUrl finalUrl() const { return m_finalUrl; } |
| 74 | QUrl responseUrl() const { return m_responseUrl; } |
| 75 | QString rootDir() const { return m_rootDir; } |
| 76 | |
| 77 | QString rootDirForIdentity() const; |
| 78 | |
| 79 | public Q_SLOTS: |
| 80 | void setCookies(const QVariant &cookies); |
| 81 | void cancel(); |
| 82 | void onLoadStarted(); |
| 83 | void onLoadFinished(bool ok); |
| 84 | |
| 85 | private Q_SLOTS: |
| 86 | void onFailTimer(); |
| 87 | void onFinished(); |
| 88 | |
| 89 | Q_SIGNALS: |
| 90 | void authenticated(); |
| 91 | |
| 92 | private: |
| 93 | void buildDialog(const QVariantMap ¶ms); |
| 94 | void closeView(); |
| 95 | bool pathsAreEqual(const QString &p1, const QString &p2); |
| 96 | |
| 97 | private: |
| 98 | Dialog *m_dialog; |
| 99 | QRegularExpression m_pathRegExp; |
| 100 | QUrl m_currentUrl; |
| 101 | QUrl m_startUrl; |
| 102 | QUrl m_finalUrl; |
| 103 | QUrl m_responseUrl; |
| 104 | QString m_rootDir; |
| 105 | QTimer m_failTimer; |
| 106 | mutable BrowserRequest *q_ptr; |
| 107 | }; |
| 108 | |
| 109 | } // namespace |
| 110 | |
| 111 | BrowserRequestPrivate::BrowserRequestPrivate(BrowserRequest *request): |
| 112 | QObject(request), |
| 113 | m_dialog(0), |
| 114 | m_pathRegExp("/*^"), |
| 115 | q_ptr(request) |
| 116 | { |
| 117 | m_failTimer.setSingleShot(true); |
| 118 | m_failTimer.setInterval(SIGNONUI_FAIL_TIMEOUT); |
| 119 | QObject::connect(&m_failTimer, SIGNAL(timeout()), |
| 120 | this, SLOT(onFailTimer())); |
| 121 | } |
| 122 | |
| 123 | BrowserRequestPrivate::~BrowserRequestPrivate() |
| 124 | { |
| 125 | DEBUG(); |
| 126 | closeView(); |
| 127 | delete m_dialog; |
| 128 | } |
| 129 | |
| 130 | bool BrowserRequestPrivate::pathsAreEqual(const QString &p1, const QString &p2) |
| 131 | { |
| 132 | QString p1copy(p1); |
| 133 | QString p2copy(p2); |
| 134 | return p1copy.remove(m_pathRegExp) == p2copy.remove(m_pathRegExp); |
| 135 | } |
| 136 | |
| 137 | QString BrowserRequestPrivate::rootDirForIdentity() const |
| 138 | { |
| 139 | Q_Q(const BrowserRequest); |
| 140 | QString cachePath = |
| 141 | QStandardPaths::writableLocation(QStandardPaths::CacheLocation); |
| 142 | return cachePath + QString("/id-%1-%2").arg(q->identity()).arg(q->providerId()); |
| 143 | } |
| 144 | |
| 145 | void BrowserRequestPrivate::start() |
| 146 | { |
| 147 | Q_Q(BrowserRequest); |
| 148 | |
| 149 | const QVariantMap ¶ms = q->parameters(); |
| 150 | DEBUG() << params; |
| 151 | |
| 152 | QDir rootDir(rootDirForIdentity()); |
| 153 | if (!rootDir.exists()) { |
| 154 | rootDir.mkpath("."); |
| 155 | } |
| 156 | |
| 157 | m_finalUrl = params.value(SSOUI_KEY_FINALURL).toString(); |
| 158 | m_startUrl = params.value(SSOUI_KEY_OPENURL).toString(); |
| 159 | m_rootDir = rootDir.absolutePath(); |
| 160 | if (!q->hasHandler()) { |
| 161 | buildDialog(params); |
| 162 | |
| 163 | QObject::connect(m_dialog, SIGNAL(finished(int)), |
| 164 | this, SLOT(onFinished())); |
| 165 | |
| 166 | m_dialog->engine()->addImportPath(PLUGIN_PRIVATE_MODULE_DIR); |
| 167 | m_dialog->rootContext()->setContextProperty("request", this); |
| 168 | m_dialog->setSource(QUrl("qrc:/qml/SignOnUiPage.qml")); |
| 169 | } else { |
| 170 | DEBUG() << "Setting request on handler"; |
| 171 | q->handler()->setRequest(this); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | QUrl BrowserRequestPrivate::pageComponentUrl() const |
| 176 | { |
| 177 | Q_Q(const BrowserRequest); |
| 178 | /* We define the X-PageComponent key to let the clients override the QML |
| 179 | * component to be used to build the authentication page. |
| 180 | * To prevent a malicious client to show it's own UI, we require that the |
| 181 | * file path begins with "/usr/share/signon-ui/" (where Ubuntu click |
| 182 | * packages cannot install files). |
| 183 | */ |
| 184 | QUrl providedUrl = q->clientData().value("X-PageComponent").toString(); |
| 185 | if (providedUrl.isValid() && providedUrl.isLocalFile() && |
| 186 | providedUrl.path().startsWith("/usr/share/signon-ui/")) { |
| 187 | return providedUrl; |
| 188 | } else { |
| 189 | return QStringLiteral("DefaultPage.qml"); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | void BrowserRequestPrivate::setCurrentUrl(const QUrl &url) |
| 194 | { |
| 195 | DEBUG() << "Url changed:" << url; |
| 196 | if (m_failTimer.isActive()) { |
| 197 | m_failTimer.start(); |
| 198 | } |
| 199 | |
| 200 | m_currentUrl = url; |
| 201 | if (url.host() == m_finalUrl.host() && |
| 202 | pathsAreEqual(url.path(), m_finalUrl.path())) { |
| 203 | m_responseUrl = url; |
| 204 | if (m_dialog != 0) { |
| 205 | if (!m_dialog->isVisible()) { |
| 206 | /* Do not show the notification page. */ |
| 207 | m_dialog->accept(); |
| 208 | } else { |
| 209 | /* Replace the web page with an information screen */ |
| 210 | /* TODO */ |
| 211 | Q_EMIT authenticated(); |
| 212 | } |
| 213 | } else { |
| 214 | DEBUG(); |
| 215 | Q_EMIT authenticated(); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | void BrowserRequestPrivate::setCookies(const QVariant &cookies) |
| 221 | { |
| 222 | DEBUG() << cookies; |
| 223 | |
| 224 | QJsonArray jsonCookies = |
| 225 | QJsonArray::fromVariantList(cookies.toList()); |
| 226 | |
| 227 | /* Save the cookies into a text file */ |
| 228 | QFile file(m_rootDir + "/cookies.json"); |
| 229 | if (Q_LIKELY(file.open(QIODevice::WriteOnly | QIODevice::Text))) { |
| 230 | QJsonDocument doc(jsonCookies); |
| 231 | file.write(doc.toJson()); |
| 232 | file.close(); |
| 233 | } |
| 234 | |
| 235 | onFinished(); |
| 236 | } |
| 237 | |
| 238 | void BrowserRequestPrivate::cancel() |
| 239 | { |
| 240 | Q_Q(BrowserRequest); |
| 241 | |
| 242 | DEBUG() << "Client requested to cancel"; |
| 243 | q->setCanceled(); |
| 244 | closeView(); |
| 245 | } |
| 246 | |
| 247 | void BrowserRequestPrivate::onLoadStarted() |
| 248 | { |
| 249 | m_failTimer.stop(); |
| 250 | } |
| 251 | |
| 252 | void BrowserRequestPrivate::onLoadFinished(bool ok) |
| 253 | { |
| 254 | Q_Q(BrowserRequest); |
| 255 | |
| 256 | DEBUG() << "Load finished" << ok; |
| 257 | |
| 258 | if (!ok) { |
| 259 | m_failTimer.start(); |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | if (m_dialog && !m_dialog->isVisible()) { |
| 264 | if (m_responseUrl.isEmpty()) { |
| 265 | q->setWindow(m_dialog); |
| 266 | } else { |
| 267 | Q_EMIT authenticated(); |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | void BrowserRequestPrivate::onFailTimer() |
| 273 | { |
| 274 | Q_Q(BrowserRequest); |
| 275 | |
| 276 | DEBUG() << "Page loading failed"; |
| 277 | closeView(); |
| 278 | QVariantMap result; |
| 279 | result[SSOUI_KEY_ERROR] = SignOn::QUERY_ERROR_NETWORK; |
| 280 | q->setResult(result); |
| 281 | } |
| 282 | |
| 283 | void BrowserRequestPrivate::onFinished() |
| 284 | { |
| 285 | Q_Q(BrowserRequest); |
| 286 | |
| 287 | DEBUG() << "Browser dialog closed"; |
| 288 | QObject::disconnect(m_dialog, SIGNAL(finished(int)), |
| 289 | this, SLOT(onFinished())); |
| 290 | |
| 291 | QVariantMap reply; |
| 292 | QUrl url = m_responseUrl.isEmpty() ? m_currentUrl : m_responseUrl; |
| 293 | reply[SSOUI_KEY_URLRESPONSE] = url.toString(); |
| 294 | |
| 295 | closeView(); |
| 296 | |
| 297 | q->setResult(reply); |
| 298 | } |
| 299 | |
| 300 | void BrowserRequestPrivate::buildDialog(const QVariantMap ¶ms) |
| 301 | { |
| 302 | m_dialog = new Dialog; |
| 303 | |
| 304 | QString title; |
| 305 | if (params.contains(SSOUI_KEY_TITLE)) { |
| 306 | title = params[SSOUI_KEY_TITLE].toString(); |
| 307 | } else if (params.contains(SSOUI_KEY_CAPTION)) { |
| 308 | title = OnlineAccountsUi::_("Web authentication for %1", |
| 309 | SIGNONUI_I18N_DOMAIN). |
| 310 | arg(params[SSOUI_KEY_CAPTION].toString()); |
| 311 | } else { |
| 312 | title = OnlineAccountsUi::_("Web authentication", |
| 313 | SIGNONUI_I18N_DOMAIN); |
| 314 | } |
| 315 | |
| 316 | m_dialog->setTitle(title); |
| 317 | |
| 318 | DEBUG() << "Dialog was built"; |
| 319 | } |
| 320 | |
| 321 | void BrowserRequestPrivate::closeView() |
| 322 | { |
| 323 | Q_Q(BrowserRequest); |
| 324 | |
| 325 | DEBUG(); |
| 326 | if (q->hasHandler()) { |
| 327 | q->handler()->setRequest(0); |
| 328 | } else if (m_dialog) { |
| 329 | m_dialog->close(); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | BrowserRequest::BrowserRequest(int id, |
| 334 | const QString &clientProfile, |
| 335 | const QVariantMap ¶meters, |
| 336 | QObject *parent): |
| 337 | Request(id, clientProfile, parameters, parent), |
| 338 | d_ptr(new BrowserRequestPrivate(this)) |
| 339 | { |
| 340 | } |
| 341 | |
| 342 | BrowserRequest::~BrowserRequest() |
| 343 | { |
| 344 | delete d_ptr; |
| 345 | } |
| 346 | |
| 347 | void BrowserRequest::start() |
| 348 | { |
| 349 | Q_D(BrowserRequest); |
| 350 | |
| 351 | Request::start(); |
| 352 | d->start(); |
| 353 | } |
| 354 | |
| 355 | #include "browser-request.moc" |