Skip to content

Commit 7cb8670

Browse files
committed
Token auth
1 parent 99101aa commit 7cb8670

File tree

6 files changed

+12
-68
lines changed

6 files changed

+12
-68
lines changed

src/cmd/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# Currently disabled until we implemented app token support
2-
return()
31
add_executable(cmd
42
cmd.cpp
3+
tokencredentials.cpp
4+
tokencredentials.h
55
)
66
set_target_properties(cmd PROPERTIES OUTPUT_NAME "${APPLICATION_EXECUTABLE}cmd")
77
ecm_mark_nongui_executable(cmd)

src/cmd/cmd.cpp

Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
*/
1616

1717
#include "account.h"
18+
#include "cmd/tokencredentials.h"
1819
#include "common/syncjournaldb.h"
1920
#include "common/version.h"
2021
#include "configfile.h" // ONLY ACCESS THE STATIC FUNCTIONS!
21-
#include "httpcredentialstext.h"
2222
#include "libsync/logger.h"
2323
#include "libsync/theme.h"
2424
#include "networkjobs/checkserverjobfactory.h"
@@ -51,9 +51,7 @@ struct CmdOptions
5151
QUrl server_url;
5252

5353
QString remoteFolder;
54-
QString config_directory;
55-
QString user;
56-
QString password;
54+
QByteArray token;
5755
QString proxy;
5856
bool silent = false;
5957
bool trustSSL = false;
@@ -76,7 +74,6 @@ struct SyncCTX
7674
CmdOptions options;
7775
bool promptRemoveAllFiles;
7876
AccountPtr account;
79-
QString user;
8077
};
8178

8279
/* If the selective sync list is different from before, we need to disable the read from db
@@ -192,18 +189,6 @@ void setupCredentials(SyncCTX &ctx)
192189
// 2. From options
193190
// 3. From prompt (if interactive)
194191

195-
const auto &url = ctx.options.target_url;
196-
ctx.user = url.userName();
197-
QString password = url.password();
198-
199-
if (!ctx.options.user.isEmpty()) {
200-
ctx.user = ctx.options.user;
201-
}
202-
203-
if (!ctx.options.password.isEmpty()) {
204-
password = ctx.options.password;
205-
}
206-
207192
if (!ctx.options.proxy.isNull()) {
208193
QString host;
209194
uint32_t port = 0;
@@ -243,7 +228,7 @@ void setupCredentials(SyncCTX &ctx)
243228
f.close();
244229
}
245230

246-
ctx.account->setCredentials(HttpCredentialsText::create(ctx.options.interactive, ctx.user, password));
231+
ctx.account->setCredentials(new TokenCredentials(std::move(ctx.options.token)));
247232
if (ctx.options.trustSSL) {
248233
QObject::connect(ctx.account->accessManager(), &QNetworkAccessManager::sslErrors, qApp,
249234
[](QNetworkReply *reply, const QList<QSslError> &errors) { reply->ignoreSslErrors(errors); });
@@ -284,8 +269,7 @@ CmdOptions parseOptions(const QStringList &app_args)
284269
auto unsyncedfoldersOption = addOption({ { QStringLiteral("unsyncedfolders") }, QStringLiteral("File containing the list of unsynced remote folders (selective sync)"), QStringLiteral("file") });
285270

286271
auto serverOption = addOption({{QStringLiteral("server")}, QStringLiteral("Use [url] as the location of the server."), QStringLiteral("url")});
287-
auto userOption = addOption({ { QStringLiteral("u"), QStringLiteral("user") }, QStringLiteral("Use [name] as the login name"), QStringLiteral("name") });
288-
auto passwordOption = addOption({{QStringLiteral("p"), QStringLiteral("password")}, QStringLiteral("Use [pass] as password"), QStringLiteral("password")});
272+
auto tokenOption = addOption({{QStringLiteral("t"), QStringLiteral("token")}, QStringLiteral("Access token"), QStringLiteral("token")});
289273

290274
auto nonInterActiveOption = addOption({ { QStringLiteral("non-interactive") }, QStringLiteral("Do not block execution with interaction") });
291275
auto maxRetriesOption = addOption({ { QStringLiteral("max-sync-retries") }, QStringLiteral("Retries maximum n times (default to 3)"), QStringLiteral("n") });
@@ -347,11 +331,8 @@ CmdOptions parseOptions(const QStringList &app_args)
347331
} else {
348332
options.server_url = options.target_url;
349333
}
350-
if (parser.isSet(userOption)) {
351-
options.user = parser.value(userOption);
352-
}
353-
if (parser.isSet(passwordOption)) {
354-
options.password = parser.value(passwordOption);
334+
if (parser.isSet(tokenOption)) {
335+
options.token = parser.value(tokenOption).toUtf8();
355336
}
356337
if (parser.isSet(excludeOption)) {
357338
options.exclude = parser.value(excludeOption);
@@ -417,16 +398,8 @@ int main(int argc, char **argv)
417398
ctx.options.server_url = ctx.options.server_url.adjusted(QUrl::RemoveUserInfo);
418399
ctx.options.target_url = ctx.options.target_url.adjusted(QUrl::RemoveUserInfo);
419400

420-
const QUrl baseUrl = [&ctx] {
421-
auto tmp = ctx.options.server_url;
422-
// Find the url leading to the dav root
423-
QStringList splitted = tmp.path().split(ctx.account->davPath());
424-
tmp.setPath(splitted.value(0));
425-
return tmp;
426-
}();
427-
428401

429-
ctx.account->setUrl(baseUrl);
402+
ctx.account->setUrl(ctx.options.server_url);
430403

431404
auto *checkServerJob = CheckServerJobFactory(ctx.account->accessManager()).startJob(ctx.account->url(), qApp);
432405

@@ -460,24 +433,9 @@ int main(int argc, char **argv)
460433
exit(EXIT_FAILURE);
461434
}
462435

463-
auto userJob = new JsonApiJob(ctx.account, QStringLiteral("ocs/v1.php/cloud/user"), {}, {}, nullptr);
464-
QObject::connect(userJob, &JsonApiJob::finishedSignal, qApp, [userJob, ctx = ctx]() mutable {
465-
const QJsonObject data = userJob->data().value(QStringLiteral("ocs")).toObject().value(QStringLiteral("data")).toObject();
466-
ctx.account->setDavUser(data.value(QStringLiteral("id")).toString());
467-
ctx.account->setDavDisplayName(data.value(QStringLiteral("display-name")).toString());
468-
469-
if (ctx.options.server_url == ctx.options.target_url) {
470-
// guess dav path
471-
if (!ctx.options.target_url.path().contains(ctx.account->davPath())) {
472-
ctx.options.target_url = OCC::Utility::concatUrlPath(ctx.options.target_url, ctx.account->davPath());
473-
}
474-
}
475-
476-
// much lower age than the default since this utility is usually made to be run right after a change in the tests
477-
SyncEngine::minimumFileAgeForUpload = std::chrono::seconds(0);
478-
sync(ctx);
479-
});
480-
userJob->start();
436+
// much lower age than the default since this utility is usually made to be run right after a change in the tests
437+
SyncEngine::minimumFileAgeForUpload = std::chrono::seconds(0);
438+
sync(ctx);
481439
});
482440
capabilitiesJob->start();
483441
} else {

src/libsync/creds/abstractcredentials.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ class OPENCLOUD_SYNC_EXPORT AbstractCredentials : public QObject
4444
*/
4545
virtual void setAccount(Account *account);
4646

47-
virtual QString authType() const = 0;
4847
virtual AccessManager *createAM() const = 0;
4948

5049
/** Whether there are credentials that can be used for a connection attempt. */

src/libsync/creds/httpcredentials.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,6 @@ HttpCredentials::HttpCredentials(const QString &accessToken)
8080
{
8181
}
8282

83-
QString HttpCredentials::authType() const
84-
{
85-
return QStringLiteral("http");
86-
}
87-
88-
void HttpCredentials::setAccount(Account *account)
89-
{
90-
AbstractCredentials::setAccount(account);
91-
}
92-
9383
AccessManager *HttpCredentials::createAM() const
9484
{
9585
AccessManager *am = new HttpCredentialsAccessManager(this);

src/libsync/creds/httpcredentials.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ class OPENCLOUD_SYNC_EXPORT HttpCredentials : public AbstractCredentials
5353

5454
explicit HttpCredentials(const QString &accessToken);
5555

56-
QString authType() const override;
5756
AccessManager *createAM() const override;
5857
bool ready() const override;
5958
void fetchFromKeychain() override;
@@ -67,7 +66,6 @@ class OPENCLOUD_SYNC_EXPORT HttpCredentials : public AbstractCredentials
6766
*/
6867
bool refreshAccessToken();
6968

70-
void setAccount(Account *account) override;
7169

7270
protected:
7371
HttpCredentials() = default;

test/testutils/syncenginetestutils.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,6 @@ class FakeCredentials : public OCC::AbstractCredentials
508508
{
509509
}
510510

511-
QString authType() const override { return QStringLiteral("test"); }
512511
OCC::AccessManager *createAM() const override { return _am; }
513512
bool ready() const override { return true; }
514513
void fetchFromKeychain() override { }

0 commit comments

Comments
 (0)