diff --git a/src/core/mainwindow.cpp b/src/core/mainwindow.cpp index c530a9f8c..441b655a4 100644 --- a/src/core/mainwindow.cpp +++ b/src/core/mainwindow.cpp @@ -594,7 +594,7 @@ MainWindow::MainWindow(Application *app, SystemTrayIcon *tray_icon, OSD *osd, co connect(tidal_view_->songs_collection_view(), SIGNAL(AddToPlaylistSignal(QMimeData*)), SLOT(AddToPlaylist(QMimeData*))); connect(tidal_view_->search_view(), SIGNAL(AddToPlaylist(QMimeData*)), SLOT(AddToPlaylist(QMimeData*))); if (TidalService *tidalservice = qobject_cast<TidalService*> (app_->internet_services()->ServiceBySource(Song::Source_Tidal))) - connect(this, SIGNAL(AuthorisationUrlReceived(QUrl)), tidalservice, SLOT(AuthorisationUrlReceived(QUrl))); + connect(this, SIGNAL(AuthorizationUrlReceived(QUrl)), tidalservice, SLOT(AuthorizationUrlReceived(QUrl))); #endif // Playlist menu @@ -2073,7 +2073,7 @@ void MainWindow::CommandlineOptionsReceived(const CommandlineOptions &options) { #ifdef HAVE_TIDAL for (const QUrl &url : options.urls()) { if (url.scheme() == "tidal" && url.host() == "login") { - emit AuthorisationUrlReceived(url); + emit AuthorizationUrlReceived(url); return; } } diff --git a/src/core/mainwindow.h b/src/core/mainwindow.h index 0bb4eae4d..330e3506a 100644 --- a/src/core/mainwindow.h +++ b/src/core/mainwindow.h @@ -133,7 +133,7 @@ class MainWindow : public QMainWindow, public PlatformInterface { void IntroPointReached(); - void AuthorisationUrlReceived(const QUrl &url); + void AuthorizationUrlReceived(const QUrl &url); private slots: void FilePathChanged(const QString& path); diff --git a/src/settings/tidalsettingspage.cpp b/src/settings/tidalsettingspage.cpp index 90df87c46..7775af814 100644 --- a/src/settings/tidalsettingspage.cpp +++ b/src/settings/tidalsettingspage.cpp @@ -55,7 +55,7 @@ TidalSettingsPage::TidalSettingsPage(SettingsDialog *parent) connect(ui_->login_state, SIGNAL(LogoutClicked()), SLOT(LogoutClicked())); connect(ui_->oauth, SIGNAL(toggled(bool)), SLOT(OAuthClicked(bool))); - connect(this, SIGNAL(Login()), service_, SLOT(StartAuthorisation())); + connect(this, SIGNAL(Authorize(QString)), service_, SLOT(StartAuthorization(QString))); connect(this, SIGNAL(Login(QString, QString, QString)), service_, SLOT(SendLogin(QString, QString, QString))); connect(service_, SIGNAL(LoginFailure(QString)), SLOT(LoginFailure(QString))); @@ -153,7 +153,7 @@ void TidalSettingsPage::LoginClicked() { QMessageBox::critical(this, tr("Configuration incomplete"), tr("Missing Tidal client ID.")); return; } - emit Login(); + emit Authorize(ui_->client_id->text()); } else { if (ui_->api_token->text().isEmpty() || ui_->username->text().isEmpty() || ui_->password->text().isEmpty()) { diff --git a/src/settings/tidalsettingspage.h b/src/settings/tidalsettingspage.h index 87cd6a6cf..f61a14174 100644 --- a/src/settings/tidalsettingspage.h +++ b/src/settings/tidalsettingspage.h @@ -53,8 +53,8 @@ class TidalSettingsPage : public SettingsPage { bool eventFilter(QObject *object, QEvent *event); signals: - void Login(); - void Login(const QString &api_token, const QString &username, const QString &password); + void Authorize(QString client_id); + void Login(QString api_token, QString username, QString password); private slots: void OAuthClicked(const bool enabled); diff --git a/src/tidal/tidalservice.cpp b/src/tidal/tidalservice.cpp index 5a0aaa35a..05ef9980b 100644 --- a/src/tidal/tidalservice.cpp +++ b/src/tidal/tidalservice.cpp @@ -49,6 +49,7 @@ #include "core/database.h" #include "core/song.h" #include "core/utilities.h" +#include "core/timeconstants.h" #include "internet/internetsearchview.h" #include "collection/collectionbackend.h" #include "collection/collectionmodel.h" @@ -93,14 +94,18 @@ TidalService::TidalService(Application *app, QObject *parent) songs_collection_sort_model_(new QSortFilterProxyModel(this)), timer_search_delay_(new QTimer(this)), timer_login_attempt_(new QTimer(this)), + timer_refresh_login_(new QTimer(this)), favorite_request_(new TidalFavoriteRequest(this, network_, this)), + enabled_(false), + oauth_(false), user_id_(0), - search_delay_(1500), artistssearchlimit_(1), albumssearchlimit_(1), songssearchlimit_(1), fetchalbums_(true), download_album_covers_(true), + expires_in_(0), + login_time_(0), pending_search_id_(0), next_pending_search_id_(1), search_id_(0), @@ -152,8 +157,12 @@ TidalService::TidalService(Application *app, QObject *parent) connect(timer_search_delay_, SIGNAL(timeout()), SLOT(StartSearch())); timer_login_attempt_->setSingleShot(true); + timer_login_attempt_->setInterval(kTimeResetLoginAttempts); connect(timer_login_attempt_, SIGNAL(timeout()), SLOT(ResetLoginAttempts())); + timer_refresh_login_->setSingleShot(true); + connect(timer_refresh_login_, SIGNAL(timeout()), SLOT(RequestAccessToken())); + connect(this, SIGNAL(Login()), SLOT(SendLogin())); connect(this, SIGNAL(Login(QString, QString, QString)), SLOT(SendLogin(QString, QString, QString))); @@ -174,6 +183,7 @@ TidalService::TidalService(Application *app, QObject *parent) connect(favorite_request_, SIGNAL(SongsRemoved(SongList)), songs_collection_backend_, SLOT(DeleteSongs(SongList))); ReloadSettings(); + LoadSession(); } @@ -226,11 +236,34 @@ void TidalService::ShowConfig() { app_->OpenSettingsDialogAtPage(SettingsDialog::Page_Tidal); } +void TidalService::LoadSession() { + + QSettings s; + s.beginGroup(TidalSettingsPage::kSettingsGroup); + user_id_ = s.value("user_id").toInt(); + country_code_ = s.value("country_code", "US").toString(); + access_token_ = s.value("access_token").toString(); + refresh_token_ = s.value("refresh_token").toString(); + session_id_ = s.value("session_id").toString(); + expires_in_ = s.value("expires_in").toLongLong(); + login_time_ = s.value("login_time").toLongLong(); + s.endGroup(); + + if (!refresh_token_.isEmpty()) { + qint64 time = expires_in_ - (QDateTime::currentDateTime().toTime_t() - login_time_); + if (time < 6) time = 6; + timer_refresh_login_->setInterval(time * kMsecPerSec); + timer_refresh_login_->start(); + } + +} + void TidalService::ReloadSettings() { QSettings s; s.beginGroup(TidalSettingsPage::kSettingsGroup); + enabled_ = s.value("enabled", false).toBool(); oauth_ = s.value("oauth", false).toBool(); client_id_ = s.value("client_id").toString(); api_token_ = s.value("api_token").toString(); @@ -241,7 +274,7 @@ void TidalService::ReloadSettings() { else password_ = QString::fromUtf8(QByteArray::fromBase64(password)); quality_ = s.value("quality", "LOSSLESS").toString(); - search_delay_ = s.value("searchdelay", 1500).toInt(); + quint64 search_delay = s.value("searchdelay", 1500).toInt(); artistssearchlimit_ = s.value("artistssearchlimit", 4).toInt(); albumssearchlimit_ = s.value("albumssearchlimit", 10).toInt(); songssearchlimit_ = s.value("songssearchlimit", 10).toInt(); @@ -250,25 +283,15 @@ void TidalService::ReloadSettings() { download_album_covers_ = s.value("downloadalbumcovers", true).toBool(); stream_url_method_ = static_cast<TidalSettingsPage::StreamUrlMethod>(s.value("streamurl").toInt()); - user_id_ = s.value("user_id").toInt(); - country_code_ = s.value("country_code", "US").toString(); - access_token_ = s.value("access_token").toString(); - refresh_token_ = s.value("refresh_token").toString(); - session_id_ = s.value("session_id").toString(); - expiry_time_ = s.value("expiry_time").toDateTime(); - s.endGroup(); + timer_search_delay_->setInterval(search_delay); + } -void TidalService::StartAuthorisation() { - - login_sent_ = true; - ++login_attempts_; - if (timer_login_attempt_->isActive()) timer_login_attempt_->stop(); - timer_login_attempt_->setInterval(kTimeResetLoginAttempts); - timer_login_attempt_->start(); +void TidalService::StartAuthorization(const QString client_id) { + client_id_ = client_id; code_verifier_ = Utilities::CryptographicRandomString(44); code_challenge_ = QString(QCryptographicHash::hash(code_verifier_.toUtf8(), QCryptographicHash::Sha256).toBase64(QByteArray::Base64UrlEncoding)); @@ -294,30 +317,31 @@ void TidalService::StartAuthorisation() { } -void TidalService::AuthorisationUrlReceived(const QUrl &url) { +void TidalService::AuthorizationUrlReceived(const QUrl &url) { - qLog(Debug) << "Tidal: Authorisation URL Received" << url; + qLog(Debug) << "Tidal: Authorization URL Received" << url; QUrlQuery url_query(url); if (url_query.hasQueryItem("token_type") && url_query.hasQueryItem("expires_in") && url_query.hasQueryItem("access_token")) { access_token_ = url_query.queryItemValue("access_token").toUtf8(); - int expires_in = url_query.queryItemValue("expires_in").toInt(); - expiry_time_ = QDateTime::currentDateTime().addSecs(expires_in - 120); + if (url_query.hasQueryItem("refresh_token")) { + refresh_token_ = url_query.queryItemValue("refresh_token").toUtf8(); + } + expires_in_ = url_query.queryItemValue("expires_in").toInt(); + login_time_ = QDateTime::currentDateTime().toTime_t(); session_id_.clear(); QSettings s; s.beginGroup(TidalSettingsPage::kSettingsGroup); s.setValue("access_token", access_token_); - s.setValue("expiry_time", expiry_time_); - s.remove("refresh_token"); + s.setValue("refresh_token", refresh_token_); + s.setValue("expires_in", expires_in_); + s.setValue("login_time", login_time_); s.remove("session_id"); s.endGroup(); - login_attempts_ = 0; - if (timer_login_attempt_->isActive()) timer_login_attempt_->stop(); - emit LoginComplete(true); emit LoginSuccess(); } @@ -327,27 +351,7 @@ void TidalService::AuthorisationUrlReceived(const QUrl &url) { QString code = url_query.queryItemValue("code"); QString state = url_query.queryItemValue("state"); - const ParamList params = ParamList() << Param("code", code) - << Param("client_id", client_id_) - << Param("grant_type", "authorization_code") - << Param("redirect_uri", kOAuthRedirectUrl) - << Param("scope", "r_usr w_usr") - << Param("code_verifier", code_verifier_); - - QUrlQuery new_url_query; - for (const Param ¶m : params) { - new_url_query.addQueryItem(QUrl::toPercentEncoding(param.first), QUrl::toPercentEncoding(param.second)); - } - - QUrl new_url(kOAuthAccessTokenUrl); - QNetworkRequest request = QNetworkRequest(new_url); - QByteArray query = new_url_query.toString(QUrl::FullyEncoded).toUtf8(); - - login_errors_.clear(); - QNetworkReply *reply = network_->post(request, query); - replies_ << reply; - connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(HandleLoginSSLErrors(QList<QSslError>))); - connect(reply, &QNetworkReply::finished, [=] { AccessTokenRequestFinished(reply); }); + RequestAccessToken(code); } @@ -358,6 +362,44 @@ void TidalService::AuthorisationUrlReceived(const QUrl &url) { } +void TidalService::RequestAccessToken(const QString &code) { + + timer_refresh_login_->stop(); + + ParamList params = ParamList() << Param("client_id", client_id_); + + if (!code.isEmpty()) { + params << Param("grant_type", "authorization_code"); + params << Param("code", code); + params << Param("code_verifier", code_verifier_); + params << Param("redirect_uri", kOAuthRedirectUrl); + params << Param("scope", "r_usr w_usr"); + } + else if (!refresh_token_.isEmpty() && enabled_ && oauth_) { + params << Param("grant_type", "refresh_token"); + params << Param("refresh_token", refresh_token_); + } + else { + return; + } + + QUrlQuery url_query; + for (const Param ¶m : params) { + url_query.addQueryItem(QUrl::toPercentEncoding(param.first), QUrl::toPercentEncoding(param.second)); + } + + QUrl url(kOAuthAccessTokenUrl); + QNetworkRequest request = QNetworkRequest(url); + QByteArray query = url_query.toString(QUrl::FullyEncoded).toUtf8(); + + login_errors_.clear(); + QNetworkReply *reply = network_->post(request, query); + replies_ << reply; + connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(HandleLoginSSLErrors(QList<QSslError>))); + connect(reply, &QNetworkReply::finished, [=] { AccessTokenRequestFinished(reply); }); + +} + void TidalService::HandleLoginSSLErrors(QList<QSslError> ssl_errors) { for (QSslError &ssl_error : ssl_errors) { @@ -373,8 +415,6 @@ void TidalService::AccessTokenRequestFinished(QNetworkReply *reply) { disconnect(reply, nullptr, this, nullptr); reply->deleteLater(); - login_sent_ = false; - if (reply->error() != QNetworkReply::NoError || reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) { if (reply->error() != QNetworkReply::NoError && reply->error() < 200) { // This is a network error, there is nothing more to do. @@ -433,49 +473,45 @@ void TidalService::AccessTokenRequestFinished(QNetworkReply *reply) { return; } - if (!json_obj.contains("access_token") || - !json_obj.contains("refresh_token") || - !json_obj.contains("expires_in") || - !json_obj.contains("user") - ) { - LoginError("Authentication reply from server is missing access_token, refresh_token, expires_in or user", json_obj); + if (!json_obj.contains("access_token") || !json_obj.contains("expires_in")) { + LoginError("Authentication reply from server is missing access_token or expires_in", json_obj); return; } access_token_ = json_obj["access_token"].toString(); - refresh_token_ = json_obj["refresh_token"].toString(); - int expires_in = json_obj["expires_in"].toInt(); - expiry_time_ = QDateTime::currentDateTime().addSecs(expires_in - 120); - - QJsonValue json_user = json_obj["user"]; - if (!json_user.isObject()) { - LoginError("Authentication reply from server has Json user that is not an object.", json_doc); - return; + expires_in_ = json_obj["expires_in"].toInt(); + if (json_obj.contains("refresh_token")) { + refresh_token_ = json_obj["refresh_token"].toString(); } - QJsonObject json_obj_user = json_user.toObject(); - if (json_obj_user.isEmpty()) { - LoginError("Authentication reply from server has empty Json user object.", json_doc); - return; + login_time_ = QDateTime::currentDateTime().toTime_t(); + + if (json_obj.contains("user") && json_obj["user"].isObject()) { + QJsonObject obj_user = json_obj["user"].toObject(); + if (obj_user.contains("countryCode") && obj_user.contains("userId")) { + country_code_ = obj_user["countryCode"].toString(); + user_id_ = obj_user["userId"].toInt(); + } } - country_code_ = json_obj_user["countryCode"].toString(); - user_id_ = json_obj_user["userId"].toInt(); session_id_.clear(); QSettings s; s.beginGroup(TidalSettingsPage::kSettingsGroup); s.setValue("access_token", access_token_); s.setValue("refresh_token", refresh_token_); - s.setValue("expiry_time", expiry_time_); + s.setValue("expires_in", expires_in_); + s.setValue("login_time", login_time_); s.setValue("country_code", country_code_); s.setValue("user_id", user_id_); s.remove("session_id"); s.endGroup(); - qLog(Debug) << "Tidal: Login successful" << "user id" << user_id_ << "access token" << access_token_; + if (expires_in_ > 0) { + timer_refresh_login_->setInterval(expires_in_ * kMsecPerSec); + timer_refresh_login_->start(); + } - login_attempts_ = 0; - if (timer_login_attempt_->isActive()) timer_login_attempt_->stop(); + qLog(Debug) << "Tidal: Login successful" << "user id" << user_id_ << "access token" << access_token_; emit LoginComplete(true); emit LoginSuccess(); @@ -490,9 +526,8 @@ void TidalService::SendLogin(const QString &api_token, const QString &username, login_sent_ = true; ++login_attempts_; - if (timer_login_attempt_->isActive()) timer_login_attempt_->stop(); - timer_login_attempt_->setInterval(kTimeResetLoginAttempts); timer_login_attempt_->start(); + timer_refresh_login_->stop(); const ParamList params = ParamList() << Param("token", (api_token.isEmpty() ? api_token_ : api_token)) << Param("username", username) @@ -601,13 +636,13 @@ void TidalService::HandleAuthReply(QNetworkReply *reply) { user_id_ = json_obj["userId"].toInt(); access_token_.clear(); refresh_token_.clear(); - expiry_time_ = QDateTime(); QSettings s; s.beginGroup(TidalSettingsPage::kSettingsGroup); s.remove("access_token"); s.remove("refresh_token"); - s.remove("expiry_time"); + s.remove("expires_in"); + s.remove("login_time"); s.setValue("user_id", user_id_); s.setValue("session_id", session_id_); s.setValue("country_code", country_code_); @@ -616,7 +651,7 @@ void TidalService::HandleAuthReply(QNetworkReply *reply) { qLog(Debug) << "Tidal: Login successful" << "user id" << user_id_ << "session id" << session_id_ << "country code" << country_code_; login_attempts_ = 0; - if (timer_login_attempt_->isActive()) timer_login_attempt_->stop(); + timer_login_attempt_->stop(); emit LoginComplete(true); emit LoginSuccess(); @@ -625,9 +660,12 @@ void TidalService::HandleAuthReply(QNetworkReply *reply) { void TidalService::Logout() { + user_id_ = 0; + country_code_.clear(); access_token_.clear(); session_id_.clear(); - expiry_time_ = QDateTime(); + expires_in_ = 0; + login_time_ = 0; QSettings s; s.beginGroup(TidalSettingsPage::kSettingsGroup); @@ -635,9 +673,12 @@ void TidalService::Logout() { s.remove("country_code"); s.remove("access_token"); s.remove("session_id"); - s.remove("expiry_time"); + s.remove("expires_in"); + s.remove("login_time"); s.endGroup(); + timer_refresh_login_->stop(); + } void TidalService::ResetLoginAttempts() { @@ -854,7 +895,6 @@ int TidalService::Search(const QString &text, InternetSearchView::SearchType typ timer_search_delay_->stop(); return pending_search_id_; } - timer_search_delay_->setInterval(search_delay_); timer_search_delay_->start(); return pending_search_id_; diff --git a/src/tidal/tidalservice.h b/src/tidal/tidalservice.h index 753d41122..284e91b6b 100644 --- a/src/tidal/tidalservice.h +++ b/src/tidal/tidalservice.h @@ -83,7 +83,6 @@ class TidalService : public InternetService { QString username() { return username_; } QString password() { return password_; } QString quality() { return quality_; } - int search_delay() { return search_delay_; } int artistssearchlimit() { return artistssearchlimit_; } int albumssearchlimit() { return albumssearchlimit_; } int songssearchlimit() { return songssearchlimit_; } @@ -137,8 +136,9 @@ class TidalService : public InternetService { private slots: void ExitReceived(); - void StartAuthorisation(); - void AuthorisationUrlReceived(const QUrl &url); + void StartAuthorization(const QString client_id); + void AuthorizationUrlReceived(const QUrl &url); + void RequestAccessToken(const QString &code = QString()); void HandleLoginSSLErrors(QList<QSslError> ssl_errors); void AccessTokenRequestFinished(QNetworkReply *reply); void SendLogin(); @@ -164,6 +164,7 @@ class TidalService : public InternetService { typedef QPair<QString, QString> Param; typedef QList<Param> ParamList; + void LoadSession(); void SendSearch(); void LoginError(const QString &error = QString(), const QVariant &debug = QVariant()); @@ -200,6 +201,7 @@ class TidalService : public InternetService { QTimer *timer_search_delay_; QTimer *timer_login_attempt_; + QTimer *timer_refresh_login_; std::shared_ptr<TidalRequest> artists_request_; std::shared_ptr<TidalRequest> albums_request_; @@ -207,6 +209,7 @@ class TidalService : public InternetService { std::shared_ptr<TidalRequest> search_request_; TidalFavoriteRequest *favorite_request_; + bool enabled_; bool oauth_; QString client_id_; QString api_token_; @@ -215,7 +218,6 @@ class TidalService : public InternetService { QString username_; QString password_; QString quality_; - int search_delay_; int artistssearchlimit_; int albumssearchlimit_; int songssearchlimit_; @@ -227,7 +229,8 @@ class TidalService : public InternetService { QString access_token_; QString refresh_token_; QString session_id_; - QDateTime expiry_time_; + quint64 expires_in_; + quint64 login_time_; int pending_search_id_; int next_pending_search_id_;