Skip to content

Commit 23eb1d7

Browse files
committed
wip
1 parent 93bdd4e commit 23eb1d7

23 files changed

+161
-168
lines changed

src/cmd/cmd.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ void setupCredentials(SyncCTX &ctx)
279279
f.close();
280280
}
281281

282-
ctx.account->setCredentials(HttpCredentialsText::create(ctx.options.interactive, ctx.user, password));
282+
ctx.account->setCredentials(HttpCredentialsText::create(ctx.account.get(), ctx.options.interactive, ctx.user, password));
283283
if (ctx.options.trustSSL) {
284284
QObject::connect(ctx.account->accessManager(), &QNetworkAccessManager::sslErrors, [](QNetworkReply *reply, const QList<QSslError> &errors) {
285285
reply->ignoreSslErrors(errors);

src/cmd/httpcredentialstext.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ QString queryPassword(const QString &user)
7474
}
7575
}
7676

77-
HttpCredentialsText::HttpCredentialsText(const QString &user, const QString &password)
78-
: OCC::HttpCredentials(OCC::DetermineAuthTypeJob::AuthType::Basic, user, password)
77+
HttpCredentialsText::HttpCredentialsText(OCC::Account *account, const QString &user, const QString &password)
78+
: OCC::HttpCredentials(account, OCC::DetermineAuthTypeJob::AuthType::Basic, user, password)
7979
{
8080
if (user.isEmpty()) {
8181
qFatal("Invalid credentials: Username is empty");
@@ -85,7 +85,7 @@ HttpCredentialsText::HttpCredentialsText(const QString &user, const QString &pas
8585
}
8686
}
8787

88-
HttpCredentialsText *HttpCredentialsText::create(bool interactive, const QString &_user, const QString &_password)
88+
HttpCredentialsText *HttpCredentialsText::create(OCC::Account *account, bool interactive, const QString &_user, const QString &_password)
8989
{
9090
QString user = _user;
9191
QString password = _password;
@@ -101,7 +101,7 @@ HttpCredentialsText *HttpCredentialsText::create(bool interactive, const QString
101101
password = queryPassword(user);
102102
}
103103
}
104-
return new HttpCredentialsText(user, password);
104+
return new HttpCredentialsText(account, user, password);
105105
}
106106

107107
void HttpCredentialsText::askFromUser()

src/cmd/httpcredentialstext.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@
1717
*/
1818
#pragma once
1919

20+
#include "accountfwd.h"
2021
#include "creds/httpcredentials.h"
2122

2223
class HttpCredentialsText : public OCC::HttpCredentials
2324
{
2425
Q_OBJECT
2526
public:
26-
static HttpCredentialsText *create(bool interactive, const QString &user, const QString &password);
27+
static HttpCredentialsText *create(OCC::Account *account, bool interactive, const QString &user, const QString &password);
2728

2829
void askFromUser() override;
2930

3031
private:
31-
HttpCredentialsText(const QString &user, const QString &password);
32+
HttpCredentialsText(OCC::Account *account, const QString &user, const QString &password);
3233
};

src/gui/accountmanager.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ bool AccountManager::restore()
119119
if (auto acc = loadAccountHelper(*settings)) {
120120
acc->_id = accountId;
121121
if (auto accState = AccountState::loadFromSettings(acc, *settings)) {
122+
// If we never fetched credentials, do that now - otherwise connection attempts
123+
// make little sense.
124+
if (!accState->account()->credentials()) {
125+
accState->account()->setCredentials(HttpCredentialsGui::fromSettings(accState.get()));
126+
}
122127
addAccountState(std::move(accState));
123128
}
124129
}
@@ -297,8 +302,6 @@ AccountPtr AccountManager::loadAccountHelper(QSettings &settings)
297302
continue;
298303
acc->_settingsMap.insert(key, settings.value(key));
299304
}
300-
acc->setCredentials(new HttpCredentialsGui);
301-
302305
// now the server cert, it is in the general group
303306
settings.beginGroup(QStringLiteral("General"));
304307
const auto certs = QSslCertificate::fromData(settings.value(caCertsKeyC()).toByteArray());

src/gui/accountstate.cpp

+5-11
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "gui/spacemigration.h"
2828
#include "gui/tlserrordialog.h"
2929

30+
#include "creds/httpcredentialsgui.h"
3031
#include "logger.h"
3132
#include "settingsdialog.h"
3233
#include "socketapi/socketapi.h"
@@ -88,7 +89,6 @@ AccountState::AccountState(AccountPtr account)
8889
, _queueGuard(_account->jobQueue())
8990
, _state(AccountState::Disconnected)
9091
, _connectionStatus(ConnectionValidator::Undefined)
91-
, _waitingForNewCredentials(false)
9292
, _maintenanceToConnectedDelay(1min + minutes(QRandomGenerator::global()->generate() % 4)) // 1-5min delay
9393
{
9494
qRegisterMetaType<AccountState *>("AccountState*");
@@ -166,6 +166,7 @@ std::unique_ptr<AccountState> AccountState::loadFromSettings(AccountPtr account,
166166
accountState->setState(SignedOut);
167167
}
168168
accountState->_supportsSpaces = settings.value(supportsSpacesC(), false).toBool();
169+
// accountState->account()->setCredentials(new HttpCredentialsGui(accountState.get(), l));
169170
return accountState;
170171
}
171172

@@ -273,7 +274,6 @@ void AccountState::freshConnectionAttempt()
273274
void AccountState::signIn()
274275
{
275276
if (_state == SignedOut) {
276-
_waitingForNewCredentials = false;
277277
setState(Disconnected);
278278
// persist that we are no longer signed out
279279
Q_EMIT account()->wantsAccountSaved(account().data());
@@ -296,7 +296,7 @@ void AccountState::checkConnectivity(bool blockJobs)
296296
setState(Connecting);
297297
}
298298
qCWarning(lcAccountState) << "checkConnectivity blocking:" << blockJobs;
299-
if (isSignedOut() || _waitingForNewCredentials) {
299+
if (isSignedOut()) {
300300
return;
301301
}
302302
if (_tlsDialog) {
@@ -314,12 +314,6 @@ void AccountState::checkConnectivity(bool blockJobs)
314314
return;
315315
}
316316

317-
// If we never fetched credentials, do that now - otherwise connection attempts
318-
// make little sense.
319-
if (!account()->credentials()->wasFetched()) {
320-
_waitingForNewCredentials = true;
321-
account()->credentials()->fetchFromKeychain();
322-
}
323317
if (account()->hasCapabilities()) {
324318
// IF the account is connected the connection check can be skipped
325319
// if the last successful etag check job is not so long ago.
@@ -356,7 +350,6 @@ void AccountState::checkConnectivity(bool blockJobs)
356350
connect(_tlsDialog, &TlsErrorDialog::accepted, _tlsDialog, [certs, blockJobs, this]() {
357351
_account->addApprovedCerts(certs);
358352
_tlsDialog.clear();
359-
_waitingForNewCredentials = false;
360353
checkConnectivity(blockJobs);
361354
});
362355
connect(_tlsDialog, &TlsErrorDialog::rejected, this, [certs, this]() {
@@ -382,7 +375,8 @@ void AccountState::checkConnectivity(bool blockJobs)
382375
}
383376
} else {
384377
// Check the server and then the auth.
385-
if (_waitingForNewCredentials) {
378+
// TODO
379+
if (false) {
386380
mode = ConnectionValidator::ValidationMode::ValidateServer;
387381
} else {
388382
_connectionValidator->setClearCookies(true);

src/gui/accountstate.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ protected Q_SLOTS:
181181
State _state;
182182
ConnectionStatus _connectionStatus;
183183
QStringList _connectionErrors;
184-
bool _waitingForNewCredentials;
184+
bool _waitingForNewCredentials = false;
185185
QDateTime _timeOfLastETagCheck;
186186
QPointer<ConnectionValidator> _connectionValidator;
187187
QPointer<UpdateUrlDialog> _updateUrlDialog;

src/gui/application.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,8 @@ void Application::slotAccountStateAdded(AccountStatePtr accountState) const
213213
// Hook up the folder manager slots to the account state's signals:
214214
connect(accountState.data(), &AccountState::stateChanged,
215215
_folderManager.data(), &FolderMan::slotAccountStateChanged);
216-
connect(accountState->account().data(), &Account::serverVersionChanged,
217-
_folderManager.data(), [account = accountState->account().data()] {
218-
FolderMan::instance()->slotServerVersionChanged(account);
219-
});
220-
accountState->checkConnectivity();
216+
connect(accountState->account().data(), &Account::serverVersionChanged, _folderManager.data(),
217+
[account = accountState->account().data()] { FolderMan::instance()->slotServerVersionChanged(account); });
221218
}
222219

223220
void Application::slotCleanup()

src/gui/creds/httpcredentialsgui.cpp

+52-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
#include "creds/httpcredentialsgui.h"
17+
1718
#include "account.h"
1819
#include "application.h"
1920
#include "basicloginwidget.h"
@@ -29,11 +30,28 @@
2930
#include <QNetworkReply>
3031
#include <QTimer>
3132

33+
namespace {
34+
auto isOAuthC()
35+
{
36+
return QStringLiteral("oauth");
37+
}
38+
39+
const QString userC()
40+
{
41+
return QStringLiteral("user");
42+
}
43+
}
44+
3245

3346
namespace OCC {
3447

3548
Q_LOGGING_CATEGORY(lcHttpCredentialsGui, "sync.credentials.http.gui", QtInfoMsg)
3649

50+
HttpCredentialsGui::HttpCredentialsGui(Account *account)
51+
: HttpCredentials(account)
52+
{
53+
}
54+
3755
void HttpCredentialsGui::openBrowser()
3856
{
3957
OC_ASSERT(isUsingOAuth());
@@ -58,11 +76,13 @@ void HttpCredentialsGui::askFromUser()
5876

5977
void HttpCredentialsGui::askFromUserAsync()
6078
{
79+
// TODO are we logged out
80+
//_account
6181
if (isUsingOAuth()) {
6282
restartOAuth();
6383
} else {
6484
// First, we will check what kind of auth we need.
65-
auto job = new DetermineAuthTypeJob(_account->sharedFromThis(), this);
85+
auto job = new DetermineAuthTypeJob(account()->sharedFromThis(), this);
6686
QObject::connect(job, &DetermineAuthTypeJob::authType, this, [this](DetermineAuthTypeJob::AuthType type) {
6787
_authType = type;
6888
if (type == DetermineAuthTypeJob::AuthType::OAuth) {
@@ -114,7 +134,7 @@ void HttpCredentialsGui::showDialog()
114134
// make sure it's cleaned up since it's not owned by the account settings (also prevents memory leaks)
115135
dialog->setAttribute(Qt::WA_DeleteOnClose);
116136

117-
dialog->setTopLabelText(tr("Please enter your password to log in to the account %1.").arg(_account->displayName()));
137+
dialog->setTopLabelText(tr("Please enter your password to log in to the account %1.").arg(account()->displayName()));
118138

119139
auto *contentWidget = qobject_cast<BasicLoginWidget *>(dialog->contentWidget());
120140
contentWidget->forceUsername(user());
@@ -160,7 +180,7 @@ QUrl HttpCredentialsGui::authorisationLink() const
160180

161181
void HttpCredentialsGui::restartOAuth()
162182
{
163-
_asyncAuth.reset(new AccountBasedOAuth(_account->sharedFromThis(), this));
183+
_asyncAuth.reset(new AccountBasedOAuth(account()->sharedFromThis(), this));
164184
connect(_asyncAuth.data(), &OAuth::result,
165185
this, &HttpCredentialsGui::asyncAuthResult);
166186
connect(_asyncAuth.data(), &OAuth::destroyed,
@@ -169,4 +189,33 @@ void HttpCredentialsGui::restartOAuth()
169189
emit authorisationLinkChanged();
170190
}
171191

192+
HttpCredentialsGui::HttpCredentialsGui(AccountState *accountState, const QString &loginUser, const QString &password)
193+
: HttpCredentials(accountState->account().get(), DetermineAuthTypeJob::AuthType::Basic, loginUser, password)
194+
{
195+
}
196+
197+
HttpCredentialsGui::HttpCredentialsGui(AccountState *accountState, const QString &davUser, const QString &password, const QString &refreshToken)
198+
: HttpCredentials(accountState->account().get(), DetermineAuthTypeJob::AuthType::OAuth, davUser, password)
199+
{
200+
_refreshToken = refreshToken;
201+
}
202+
203+
HttpCredentialsGui *HttpCredentialsGui::fromSettings(AccountState *accountState)
204+
{
205+
auto *out = new HttpCredentialsGui(accountState->account().get());
206+
out->_user = accountState->account()->credentialSetting(out, userC()).toString();
207+
Q_ASSERT(!out->_user.isEmpty());
208+
209+
const auto isOauth = accountState->account()->credentialSetting(out, isOAuthC()).toBool();
210+
out->_authType = isOauth ? DetermineAuthTypeJob::AuthType::OAuth : DetermineAuthTypeJob::AuthType::Basic;
211+
212+
out->fetchFromKeychain();
213+
return out;
214+
}
215+
void HttpCredentialsGui::persist()
216+
{
217+
account()->setCredentialSetting(this, userC(), _user);
218+
account()->setCredentialSetting(this, isOAuthC(), isUsingOAuth());
219+
HttpCredentials::persist();
220+
}
172221
} // namespace OCC

src/gui/creds/httpcredentialsgui.h

+12-14
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,17 @@ class HttpCredentialsGui : public HttpCredentials
2929
{
3030
Q_OBJECT
3131
public:
32-
HttpCredentialsGui()
33-
: HttpCredentials()
34-
{
35-
}
36-
HttpCredentialsGui(const QString &loginUser, const QString &password)
37-
: HttpCredentials(DetermineAuthTypeJob::AuthType::Basic, loginUser, password)
38-
{
39-
}
40-
41-
HttpCredentialsGui(const QString &davUser, const QString &password, const QString &refreshToken)
42-
: HttpCredentials(DetermineAuthTypeJob::AuthType::OAuth, davUser, password)
43-
{
44-
_refreshToken = refreshToken;
45-
}
32+
static HttpCredentialsGui *fromSettings(AccountState *accountState);
33+
34+
HttpCredentialsGui(AccountState *accountState, const QString &loginUser, const QString &password);
35+
36+
HttpCredentialsGui(AccountState *accountState, const QString &davUser, const QString &password, const QString &refreshToken);
4637

4738
void openBrowser();
4839

40+
41+
void persist() override;
42+
4943
QUrl authorisationLink() const;
5044

5145
/**
@@ -69,6 +63,10 @@ private slots:
6963

7064
void oAuthErrorOccurred();
7165

66+
protected:
67+
HttpCredentialsGui(Account *account);
68+
;
69+
7270
private:
7371
QScopedPointer<AccountBasedOAuth, QScopedPointerObjectDeleteLater<AccountBasedOAuth>> _asyncAuth;
7472
QPointer<QWidget> _loginRequiredDialog;

src/gui/newwizard/setupwizardaccountbuilder.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ HttpBasicAuthenticationStrategy::HttpBasicAuthenticationStrategy(const QString &
3737
{
3838
}
3939

40-
HttpCredentialsGui *HttpBasicAuthenticationStrategy::makeCreds()
40+
HttpCredentialsGui *HttpBasicAuthenticationStrategy::makeCreds(AccountState *accountState)
4141
{
42-
return new HttpCredentialsGui(loginUser(), _password);
42+
return new HttpCredentialsGui(accountState, loginUser(), _password);
4343
}
4444

4545
bool HttpBasicAuthenticationStrategy::isValid()
@@ -68,10 +68,10 @@ OAuth2AuthenticationStrategy::OAuth2AuthenticationStrategy(const QString &token,
6868
{
6969
}
7070

71-
HttpCredentialsGui *OAuth2AuthenticationStrategy::makeCreds()
71+
HttpCredentialsGui *OAuth2AuthenticationStrategy::makeCreds(AccountState *accountState)
7272
{
7373
Q_ASSERT(isValid());
74-
return new HttpCredentialsGui(davUser(), _token, _refreshToken);
74+
return new HttpCredentialsGui(accountState, davUser(), _token, _refreshToken);
7575
}
7676

7777
bool OAuth2AuthenticationStrategy::isValid()
@@ -111,7 +111,7 @@ void SetupWizardAccountBuilder::setLegacyWebFingerUsername(const QString &userna
111111
_legacyWebFingerUsername = username;
112112
}
113113

114-
AccountPtr SetupWizardAccountBuilder::build()
114+
AccountStatePtr SetupWizardAccountBuilder::build()
115115
{
116116
auto newAccountPtr = Account::create(QUuid::createUuid());
117117

@@ -127,8 +127,6 @@ AccountPtr SetupWizardAccountBuilder::build()
127127

128128
// TODO: perhaps _authenticationStrategy->setUpAccountPtr(...) would be more elegant? no need for getters then
129129
newAccountPtr->setDavUser(_authenticationStrategy->davUser());
130-
newAccountPtr->setCredentials(_authenticationStrategy->makeCreds());
131-
132130
newAccountPtr->setDavDisplayName(_displayName);
133131

134132
newAccountPtr->addApprovedCerts({ _customTrustedCaCertificates.begin(), _customTrustedCaCertificates.end() });
@@ -137,7 +135,10 @@ AccountPtr SetupWizardAccountBuilder::build()
137135
newAccountPtr->setDefaultSyncRoot(_defaultSyncTargetDir);
138136
}
139137

140-
return newAccountPtr;
138+
auto accountState = AccountManager::instance()->addAccount(newAccountPtr);
139+
newAccountPtr->setCredentials(_authenticationStrategy->makeCreds(accountState));
140+
141+
return accountState;
141142
}
142143

143144
bool SetupWizardAccountBuilder::hasValidCredentials() const

0 commit comments

Comments
 (0)