From ef479a9e26d5222af9c835a19eeb85d4a6557e0f Mon Sep 17 00:00:00 2001 From: yangheran Date: Tue, 18 Jun 2024 11:05:05 +0800 Subject: [PATCH] Support password hash --- src/account.h | 9 +++++++++ src/api/requests.cpp | 38 +++++++++++++++++++++++++++++++++-- src/api/requests.h | 10 +++++++-- src/api/server-info.h | 6 +++++- src/rpc/rpc-client.cpp | 13 ++++++++++-- src/rpc/rpc-client.h | 3 +++ src/ui/create-repo-dialog.cpp | 10 ++++++--- 7 files changed, 79 insertions(+), 10 deletions(-) diff --git a/src/account.h b/src/account.h index 5cbfcf73f..cd8ad061e 100644 --- a/src/account.h +++ b/src/account.h @@ -114,6 +114,15 @@ class Account { int getEncryptedLibraryVersion() const { return serverInfo.encryptedLibraryVersion; } + + QString getEncryptedLibraryPwdHashAlgo() const { + return serverInfo.pwdHashAlgo; + } + + QString getEncryptedLibraryPwdHashParams() const { + return serverInfo.pwdHashParams; + } + // require pro edtions and version at least at ... // excluding OSS Version bool isAtLeastProVersion(unsigned majorVersion, unsigned minorVersion, unsigned patchVersion) const { diff --git a/src/api/requests.cpp b/src/api/requests.cpp index 9af02ab7f..f6cef9dd5 100644 --- a/src/api/requests.cpp +++ b/src/api/requests.cpp @@ -259,7 +259,10 @@ CreateRepoRequest::CreateRepoRequest(const Account& account, int enc_version, const QString& repo_id, const QString& magic, - const QString& random_key) + const QString& random_key, + const QString& pwd_hash_algo, + const QString& pwd_hash_params, + const QString& pwd_hash) : SeafileApiRequest(account.getAbsoluteUrl(kCreateRepoUrl), SeafileApiRequest::METHOD_POST, account.token) @@ -270,6 +273,16 @@ CreateRepoRequest::CreateRepoRequest(const Account& account, setFormParam("repo_id", repo_id); setFormParam("magic", magic); setFormParam("random_key", random_key); + + if (!pwd_hash_algo.isEmpty()) { + setFormParam("pwd_hash_algo", pwd_hash_algo); + } + if (!pwd_hash_params.isEmpty()) { + setFormParam("pwd_hash_params", pwd_hash_params); + } + if (!pwd_hash.isEmpty()) { + setFormParam("pwd_hash", pwd_hash); + } } CreateRepoRequest::CreateRepoRequest(const Account& account, @@ -279,7 +292,10 @@ CreateRepoRequest::CreateRepoRequest(const Account& account, const QString& repo_id, const QString& magic, const QString& random_key, - const QString& salt) + const QString& salt, + const QString& pwd_hash_algo, + const QString& pwd_hash_params, + const QString& pwd_hash) : SeafileApiRequest(account.getAbsoluteUrl(kCreateRepoUrl), SeafileApiRequest::METHOD_POST, account.token) @@ -291,6 +307,16 @@ CreateRepoRequest::CreateRepoRequest(const Account& account, setFormParam("magic", magic); setFormParam("random_key", random_key); setFormParam("salt", salt); + + if (!pwd_hash_algo.isEmpty()) { + setFormParam("pwd_hash_algo", pwd_hash_algo); + } + if (!pwd_hash_params.isEmpty()) { + setFormParam("pwd_hash_params", pwd_hash_params); + } + if (!pwd_hash.isEmpty()) { + setFormParam("pwd_hash", pwd_hash); + } } void CreateRepoRequest::requestSuccess(QNetworkReply& reply) @@ -759,6 +785,14 @@ void ServerInfoRequest::requestSuccess(QNetworkReply& reply) ret.customBrand = dict["desktop-custom-brand"].toString(); } + if (dict.contains("encrypted_library_pwd_hash_algo")) { + ret.pwdHashAlgo = dict["encrypted_library_pwd_hash_algo"].toString(); + } + + if (dict.contains("encrypted_library_pwd_params")) { + ret.pwdHashParams = dict["encrypted_library_pwd_params"].toString(); + } + emit success(ret); } diff --git a/src/api/requests.h b/src/api/requests.h index 742a3b2d9..7dc72d458 100644 --- a/src/api/requests.h +++ b/src/api/requests.h @@ -152,7 +152,10 @@ class CreateRepoRequest : public SeafileApiRequest int enc_version, const QString& repo_id, const QString& magic, - const QString& random_key); + const QString& random_key, + const QString& pwd_hash_algo, + const QString& pwd_hash_params, + const QString& pwd_hash); // Constructor for seafile encryption v3 CreateRepoRequest(const Account& account, const QString& name, @@ -161,7 +164,10 @@ class CreateRepoRequest : public SeafileApiRequest const QString& repo_id, const QString& magic, const QString& random_key, - const QString& salt); + const QString& salt, + const QString& pwd_hash_algo, + const QString& pwd_hash_params, + const QString& pwd_hash); protected slots: void requestSuccess(QNetworkReply& reply); diff --git a/src/api/server-info.h b/src/api/server-info.h index 3af3c62f3..4d497e008 100644 --- a/src/api/server-info.h +++ b/src/api/server-info.h @@ -16,6 +16,8 @@ class ServerInfo { bool clientSSOViaLocalBrowser; QString customBrand; QString customLogo; + QString pwdHashAlgo; + QString pwdHashParams; ServerInfo() : majorVersion(0), @@ -39,7 +41,9 @@ class ServerInfo { fileSearch == rhs.fileSearch && disableSyncWithAnyFolder == rhs.disableSyncWithAnyFolder && customBrand == rhs.customBrand && - customLogo == rhs.customLogo; + customLogo == rhs.customLogo && + pwdHashAlgo == rhs.pwdHashAlgo && + pwdHashParams == rhs.pwdHashParams; } bool operator!= (const ServerInfo &rhs) const { diff --git a/src/rpc/rpc-client.cpp b/src/rpc/rpc-client.cpp index adc4b0ffc..b0d0bd571 100644 --- a/src/rpc/rpc-client.cpp +++ b/src/rpc/rpc-client.cpp @@ -870,7 +870,10 @@ int SeafileRpcClient::markFileLockState(const QString &repo_id, int SeafileRpcClient::generateMagicAndRandomKey(int enc_version, const QString &repo_id, const QString &passwd, + const QString &pwd_hash_algo, + const QString &pwd_hash_params, QString *magic, + QString *pwd_hash, QString *random_key, QString *salt) { @@ -879,10 +882,12 @@ int SeafileRpcClient::generateMagicAndRandomKey(int enc_version, seafile_rpc_client_, "seafile_generate_magic_and_random_key", SEAFILE_TYPE_ENCRYPTION_INFO, - &error, 3, + &error, 5, "int", enc_version, "string", toCStr(repo_id), - "string", toCStr(passwd)); + "string", toCStr(passwd), + "string", toCStr(pwd_hash_algo), + "string", toCStr(pwd_hash_params)); if (error) { qWarning("failed to generate magic and random_key : %s\n", error->message); g_error_free(error); @@ -892,9 +897,11 @@ int SeafileRpcClient::generateMagicAndRandomKey(int enc_version, char *c_magic = NULL; char *c_random_key = NULL; char *c_salt = NULL; + char *c_pwd_hash = NULL; if (enc_version == 3 || enc_version == 4) { g_object_get (obj, "magic", &c_magic, + "pwd_hash", &c_pwd_hash, "random_key", &c_random_key, "salt", &c_salt, NULL); @@ -903,11 +910,13 @@ int SeafileRpcClient::generateMagicAndRandomKey(int enc_version, } else { g_object_get (obj, "magic", &c_magic, + "pwd_hash", &c_pwd_hash, "random_key", &c_random_key, NULL); } *magic = QString(c_magic); + *pwd_hash = QString(c_pwd_hash); *random_key = QString(c_random_key); g_object_unref (obj); diff --git a/src/rpc/rpc-client.h b/src/rpc/rpc-client.h index bf8f2038b..e8360f3a6 100644 --- a/src/rpc/rpc-client.h +++ b/src/rpc/rpc-client.h @@ -115,7 +115,10 @@ class SeafileRpcClient : public QObject { int generateMagicAndRandomKey(int enc_version, const QString& repo_id, const QString& passwd, + const QString& pwd_hash_algo, + const QString& pwd_hash_params, QString *magic, + QString *pwd_hash, QString *random_key, QString *salt); diff --git a/src/ui/create-repo-dialog.cpp b/src/ui/create-repo-dialog.cpp index 90b92a0dc..2787b79b9 100644 --- a/src/ui/create-repo-dialog.cpp +++ b/src/ui/create-repo-dialog.cpp @@ -116,6 +116,10 @@ void CreateRepoDialog::createAction() // TODO: check server version is at least 4.3.x ? QString repo_id = QUuid::createUuid().toString().mid(1, 36); QString magic, random_key, salt; + QString pwd_hash_algo, pwd_hash_params, pwd_hash; + + pwd_hash_algo = seafApplet->accountManager()->currentAccount().getEncryptedLibraryPwdHashAlgo(); + pwd_hash_params = seafApplet->accountManager()->currentAccount().getEncryptedLibraryPwdHashAlgo(); int enc_version = seafApplet->accountManager()->currentAccount().getEncryptedLibraryVersion(); @@ -125,17 +129,17 @@ void CreateRepoDialog::createAction() } if (seafApplet->rpcClient()->generateMagicAndRandomKey( - enc_version, repo_id, passwd_, &magic, &random_key, &salt) < 0) { + enc_version, repo_id, passwd_, pwd_hash_algo, pwd_hash_params, &magic, &pwd_hash, &random_key, &salt) < 0) { seafApplet->warningBox(tr("Failed to generate encryption key for this library"), this); return; } if (enc_version == 3 || enc_version == 4) { request_ = new CreateRepoRequest( - account_, name_, name_, enc_version, repo_id, magic, random_key, salt); + account_, name_, name_, enc_version, repo_id, magic, random_key, salt, pwd_hash_algo, pwd_hash_params, pwd_hash); } else { request_ = new CreateRepoRequest( - account_, name_, name_, enc_version, repo_id, magic, random_key); + account_, name_, name_, enc_version, repo_id, magic, random_key, pwd_hash_algo, pwd_hash_params, pwd_hash); } } else { request_ = new CreateRepoRequest(account_, name_, name_, passwd_);