| 1 | // SPDX-FileCopyrightText: Nheko Contributors |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include <QDesktopServices> |
| 6 | |
| 7 | #include <set> |
| 8 | |
| 9 | #include <mtx/identifiers.hpp> |
| 10 | #include <mtx/requests.hpp> |
| 11 | #include <mtx/responses/login.hpp> |
| 12 | #include <mtx/responses/version.hpp> |
| 13 | |
| 14 | #include "Logging.h" |
| 15 | #include "LoginPage.h" |
| 16 | #include "MainWindow.h" |
| 17 | #include "MatrixClient.h" |
| 18 | #include "SSOHandler.h" |
| 19 | #include "UserSettingsPage.h" |
| 20 | |
| 21 | using namespace mtx::identifiers; |
| 22 | |
| 23 | LoginPage::LoginPage(QObject *parent) |
| 24 | : QObject(parent) |
| 25 | , inferredServerAddress_() |
| 26 | { |
| 27 | [[maybe_unused]] static auto ignored = qRegisterMetaType<mtx::responses::Login>(); |
| 28 | |
| 29 | connect(this, &LoginPage::versionOkCb, this, &LoginPage::versionOk, Qt::QueuedConnection); |
| 30 | connect(this, &LoginPage::versionErrorCb, this, &LoginPage::versionError, Qt::QueuedConnection); |
| 31 | connect( |
| 32 | this, |
| 33 | &LoginPage::loginOk, |
| 34 | this, |
| 35 | [this](const mtx::responses::Login &res) { |
| 36 | loggingIn_ = false; |
| 37 | emit loggingInChanged(); |
| 38 | |
| 39 | http::client()->set_user(res.user_id); |
| 40 | MainWindow::instance()->showChatPage(); |
| 41 | }, |
| 42 | Qt::QueuedConnection); |
| 43 | } |
| 44 | void |
| 45 | LoginPage::showError(const QString &msg) |
| 46 | { |
| 47 | loggingIn_ = false; |
| 48 | emit loggingInChanged(); |
| 49 | |
| 50 | error_ = msg; |
| 51 | emit errorOccurred(); |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | LoginPage::setHomeserver(const QString &hs) |
| 56 | { |
| 57 | if (hs != homeserver_) { |
| 58 | homeserver_ = hs; |
| 59 | homeserverValid_ = false; |
| 60 | emit homeserverChanged(); |
| 61 | http::client()->set_server(hs.toStdString()); |
| 62 | checkHomeserverVersion(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | LoginPage::onMatrixIdEntered() |
| 68 | { |
| 69 | clearErrors(); |
| 70 | |
| 71 | homeserverValid_ = false; |
| 72 | emit homeserverChanged(); |
| 73 | |
| 74 | User user; |
| 75 | try { |
| 76 | user = parse<User>(mxid_.toStdString()); |
| 77 | } catch (const std::exception &) { |
| 78 | mxidError_ = tr("You have entered an invalid Matrix ID e.g. @user:yourserver.example.com"); |
| 79 | emit mxidErrorChanged(); |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | if (user.hostname().empty() || user.localpart().empty()) { |
| 84 | mxidError_ = tr("You have entered an invalid Matrix ID e.g. @user:yourserver.example.com"); |
| 85 | emit mxidErrorChanged(); |
| 86 | return; |
| 87 | } else { |
| 88 | nhlog::net()->debug("hostname: {}", user.hostname()); |
| 89 | } |
| 90 | |
| 91 | if (user.hostname() != inferredServerAddress_.toStdString()) { |
| 92 | homeserverNeeded_ = false; |
| 93 | lookingUpHs_ = true; |
| 94 | emit lookingUpHsChanged(); |
| 95 | |
| 96 | http::client()->set_server(user.hostname()); |
| 97 | http::client()->verify_certificates( |
| 98 | !UserSettings::instance()->disableCertificateValidation()); |
| 99 | homeserver_ = QString::fromStdString(user.hostname()); |
| 100 | emit homeserverChanged(); |
| 101 | |
| 102 | http::client()->well_known( |
| 103 | [this, orginal_hostname = user.hostname()](const mtx::responses::WellKnown &res, |
| 104 | mtx::http::RequestErr err) { |
| 105 | // Ignore if server changed |
| 106 | auto currentUser = parse<User>(mxid_.toStdString()); |
| 107 | if (currentUser.hostname() != orginal_hostname) |
| 108 | return; |
| 109 | |
| 110 | if (err) { |
| 111 | if (err->status_code == 404) { |
| 112 | nhlog::net()->info("Autodiscovery: No .well-known."); |
| 113 | checkHomeserverVersion(); |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | if (!err->parse_error.empty()) { |
| 118 | emit versionErrorCb(tr("Autodiscovery failed. Received malformed response.")); |
| 119 | nhlog::net()->error("Autodiscovery failed. Received malformed response. {}", |
| 120 | err->parse_error); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | emit versionErrorCb(tr("Autodiscovery failed. Unknown error when " |
| 125 | "requesting .well-known.")); |
| 126 | nhlog::net()->error("Autodiscovery failed. Unknown error when " |
| 127 | "requesting .well-known. {}", |
| 128 | *err); |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | nhlog::net()->info("Autodiscovery: Discovered '" + res.homeserver.base_url + "'"); |
| 133 | http::client()->set_server(res.homeserver.base_url); |
| 134 | emit homeserverChanged(); |
| 135 | checkHomeserverVersion(); |
| 136 | }); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | void |
| 141 | LoginPage::checkHomeserverVersion() |
| 142 | { |
| 143 | clearErrors(); |
| 144 | |
| 145 | try { |
| 146 | User user = parse<User>(mxid_.toStdString()); |
| 147 | } catch (const std::exception &) { |
| 148 | mxidError_ = tr("You have entered an invalid Matrix ID e.g. @user:yourserver.example.com"); |
| 149 | emit mxidErrorChanged(); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | http::client()->versions([this](const mtx::responses::Versions &versions, |
| 154 | mtx::http::RequestErr err) { |
| 155 | if (err) { |
| 156 | if (err->status_code == 404) { |
| 157 | emit versionErrorCb(tr("The required endpoints were not found. " |
| 158 | "Possibly not a Matrix server.")); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | if (!err->parse_error.empty()) { |
| 163 | emit versionErrorCb(tr("Received malformed response. Make sure " |
| 164 | "the homeserver domain is valid.")); |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | nhlog::net()->error("Error requesting versions: {}", *err); |
| 169 | |
| 170 | emit versionErrorCb( |
| 171 | tr("An unknown error occured. Make sure the homeserver domain is valid.")); |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | if (std::find_if( |
| 176 | versions.versions.cbegin(), versions.versions.cend(), [](const std::string &v) { |
| 177 | static const std::set<std::string_view, std::less<>> supported{ |
| 178 | "v1.1", |
| 179 | "v1.2", |
| 180 | "v1.3", |
| 181 | "v1.4", |
| 182 | "v1.5", |
| 183 | "v1.6", |
| 184 | "v1.7", |
| 185 | "v1.8", |
| 186 | "v1.9", |
| 187 | "v1.10", |
| 188 | }; |
| 189 | return supported.count(v) != 0; |
| 190 | }) == versions.versions.cend()) { |
| 191 | emit versionErrorCb( |
| 192 | tr("The selected server does not support a version of the Matrix protocol, that this " |
| 193 | "client understands (%1 to %2). You can't sign in.") |
| 194 | .arg(u"v1.1", u"v1.10")); |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | http::client()->get_login([this](mtx::responses::LoginFlows flows, |
| 199 | mtx::http::RequestErr err) { |
| 200 | if (err || flows.flows.empty()) |
| 201 | emit versionOkCb(true, false, {}); |
| 202 | |
| 203 | QVariantList idps; |
| 204 | bool ssoSupported = false; |
| 205 | bool passwordSupported = false; |
| 206 | for (const auto &flow : flows.flows) { |
| 207 | if (flow.type == mtx::user_interactive::auth_types::sso) { |
| 208 | ssoSupported = true; |
| 209 | |
| 210 | for (const auto &idp : flow.identity_providers) { |
| 211 | SSOProvider prov; |
| 212 | if (idp.brand == "apple") |
| 213 | prov.name_ = tr("Sign in with Apple"); |
| 214 | else if (idp.brand == "facebook") |
| 215 | prov.name_ = tr("Continue with Facebook"); |
| 216 | else if (idp.brand == "google") |
| 217 | prov.name_ = tr("Sign in with Google"); |
| 218 | else if (idp.brand == "twitter") |
| 219 | prov.name_ = tr("Sign in with Twitter"); |
| 220 | else |
| 221 | prov.name_ = tr("Login using %1").arg(QString::fromStdString(idp.name)); |
| 222 | |
| 223 | prov.avatarUrl_ = QString::fromStdString(idp.icon); |
| 224 | prov.id_ = QString::fromStdString(idp.id); |
| 225 | idps.push_back(QVariant::fromValue(prov)); |
| 226 | } |
| 227 | |
| 228 | if (flow.identity_providers.empty()) { |
| 229 | SSOProvider prov; |
| 230 | prov.name_ = tr("SSO LOGIN"); |
| 231 | idps.push_back(QVariant::fromValue(prov)); |
| 232 | } |
| 233 | } else if (flow.type == mtx::user_interactive::auth_types::password) { |
| 234 | passwordSupported = true; |
| 235 | } |
| 236 | } |
| 237 | emit versionOkCb(passwordSupported, ssoSupported, idps); |
| 238 | }); |
| 239 | }); |
| 240 | } |
| 241 | |
| 242 | void |
| 243 | LoginPage::versionError(const QString &error) |
| 244 | { |
| 245 | showError(error); |
| 246 | |
| 247 | homeserverNeeded_ = true; |
| 248 | lookingUpHs_ = false; |
| 249 | homeserverValid_ = false; |
| 250 | emit lookingUpHsChanged(); |
| 251 | emit versionLookedUp(); |
| 252 | } |
| 253 | |
| 254 | void |
| 255 | LoginPage::versionOk(bool passwordSupported, bool ssoSupported, QVariantList idps) |
| 256 | { |
| 257 | passwordSupported_ = passwordSupported; |
| 258 | ssoSupported_ = ssoSupported; |
| 259 | identityProviders_ = idps; |
| 260 | |
| 261 | lookingUpHs_ = false; |
| 262 | homeserverValid_ = true; |
| 263 | emit homeserverChanged(); |
| 264 | emit lookingUpHsChanged(); |
| 265 | emit versionLookedUp(); |
| 266 | } |
| 267 | |
| 268 | void |
| 269 | LoginPage::onLoginButtonClicked(LoginMethod loginMethod, |
| 270 | const QString &userid, |
| 271 | const QString &password, |
| 272 | const QString &deviceName) |
| 273 | { |
| 274 | clearErrors(); |
| 275 | |
| 276 | User user; |
| 277 | |
| 278 | try { |
| 279 | user = parse<User>(userid.toStdString()); |
| 280 | } catch (const std::exception &) { |
| 281 | mxidError_ = tr("You have entered an invalid Matrix ID e.g. @user:yourserver.example.com"); |
| 282 | emit mxidErrorChanged(); |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | if (loginMethod == LoginMethod::Password) { |
| 287 | if (password.isEmpty()) |
| 288 | return showError(tr("Empty password")); |
| 289 | |
| 290 | http::client()->login( |
| 291 | user.localpart(), |
| 292 | password.toStdString(), |
| 293 | deviceName.trimmed().isEmpty() ? initialDeviceName_() : deviceName.toStdString(), |
| 294 | [this](const mtx::responses::Login &res, mtx::http::RequestErr err) { |
| 295 | if (err) { |
| 296 | auto error = err->matrix_error.error; |
| 297 | if (error.empty()) |
| 298 | error = err->parse_error; |
| 299 | |
| 300 | showError(QString::fromStdString(error)); |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | if (res.well_known) { |
| 305 | http::client()->set_server(res.well_known->homeserver.base_url); |
| 306 | nhlog::net()->info("Login requested to use server: " + |
| 307 | res.well_known->homeserver.base_url); |
| 308 | } |
| 309 | |
| 310 | emit loginOk(res); |
| 311 | }); |
| 312 | } else { |
| 313 | auto sso = new SSOHandler(); |
| 314 | connect(sso, |
| 315 | &SSOHandler::ssoSuccess, |
| 316 | this, |
| 317 | [this, sso, userid, deviceName](const std::string &token) { |
| 318 | mtx::requests::Login req{}; |
| 319 | req.token = token; |
| 320 | req.type = mtx::user_interactive::auth_types::token; |
| 321 | req.initial_device_display_name = deviceName.trimmed().isEmpty() |
| 322 | ? initialDeviceName_() |
| 323 | : deviceName.toStdString(); |
| 324 | http::client()->login( |
| 325 | req, [this](const mtx::responses::Login &res, mtx::http::RequestErr err) { |
| 326 | if (err) { |
| 327 | showError(QString::fromStdString(err->matrix_error.error)); |
| 328 | emit errorOccurred(); |
| 329 | return; |
| 330 | } |
| 331 | |
| 332 | if (res.well_known) { |
| 333 | http::client()->set_server(res.well_known->homeserver.base_url); |
| 334 | nhlog::net()->info("Login requested to use server: " + |
| 335 | res.well_known->homeserver.base_url); |
| 336 | } |
| 337 | |
| 338 | emit loginOk(res); |
| 339 | }); |
| 340 | sso->deleteLater(); |
| 341 | }); |
| 342 | connect(sso, &SSOHandler::ssoFailed, this, [this, sso]() { |
| 343 | showError(tr("SSO login failed")); |
| 344 | emit errorOccurred(); |
| 345 | sso->deleteLater(); |
| 346 | }); |
| 347 | |
| 348 | // password doubles as the idp id for SSO login |
| 349 | QDesktopServices::openUrl(QString::fromStdString( |
| 350 | http::client()->login_sso_redirect(sso->url(), password.toStdString()))); |
| 351 | } |
| 352 | |
| 353 | loggingIn_ = true; |
| 354 | emit loggingInChanged(); |
| 355 | } |
| 356 | |
| 357 | #include "moc_LoginPage.cpp" |