From 89ec84a4cb5f8fea4214c6feaf6eafe2685295b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Wed, 24 Sep 2014 21:16:27 +0200 Subject: [PATCH] HaikuDepot: Some usage of the BKeyStore API ... completely untested and premature. The idea is that after a successful login with the web-app, the password used is stored in the keyring. Then HaikuDepot will restore just last the used username on next launch and retrieve the password from the keyring. One could also register multiple accounts in HaikuDepot and switch between them. --- src/apps/haikudepot/Model.cpp | 26 ++++++++++++++++++++++++- src/apps/haikudepot/Model.h | 5 ++++- src/apps/haikudepot/UserLoginWindow.cpp | 4 ++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/apps/haikudepot/Model.cpp b/src/apps/haikudepot/Model.cpp index d83245600f..03473597b0 100644 --- a/src/apps/haikudepot/Model.cpp +++ b/src/apps/haikudepot/Model.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -26,6 +27,9 @@ #define B_TRANSLATION_CONTEXT "Model" +static const char* kHaikuDepotKeyring = "HaikuDepot"; + + // #pragma mark - PackageFilters @@ -639,8 +643,28 @@ Model::StopPopulatingAllPackages() void -Model::SetAuthorization(const BString& username, const BString& password) +Model::SetUser(const BString& username) { + BPasswordKey key; + BKeyStore keyStore; + if (keyStore.GetKey(kHaikuDepotKeyring, B_KEY_TYPE_PASSWORD, username, + key) == B_OK) { + SetAuthorization(username, key.Password(), false); + } +} + + +void +Model::SetAuthorization(const BString& username, const BString& password, + bool storePassword) +{ + if (storePassword) { + BPasswordKey key(password, B_KEY_PURPOSE_WEB, username); + BKeyStore keyStore; + keyStore.AddKeyring(kHaikuDepotKeyring); + keyStore.AddKey(kHaikuDepotKeyring, key); + } + BAutolock locker(&fLock); fWebAppInterface.SetAuthorization(username, password); } diff --git a/src/apps/haikudepot/Model.h b/src/apps/haikudepot/Model.h index c968c1c7a4..487049c9ee 100644 --- a/src/apps/haikudepot/Model.h +++ b/src/apps/haikudepot/Model.h @@ -107,8 +107,10 @@ public: const BString& PreferredLanguage() const { return fPreferredLanguage; } + void SetUser(const BString& username); void SetAuthorization(const BString& username, - const BString& password); + const BString& password, + bool storePassword); private: static int32 _PopulateAllPackagesEntry(void* cookie); @@ -172,6 +174,7 @@ private: thread_id fPopulateAllPackagesThread; volatile bool fStopPopulatingAllPackages; + BString fPreferredLanguage; WebAppInterface fWebAppInterface; diff --git a/src/apps/haikudepot/UserLoginWindow.cpp b/src/apps/haikudepot/UserLoginWindow.cpp index 0a525cabba..bb47b532fb 100644 --- a/src/apps/haikudepot/UserLoginWindow.cpp +++ b/src/apps/haikudepot/UserLoginWindow.cpp @@ -346,7 +346,7 @@ UserLoginWindow::_AuthenticateThread() // the Token Bearer. See section 5.1.2 in the haiku-depot-web // documentation. error = ""; - fModel.SetAuthorization(nickName, passwordClear); + fModel.SetAuthorization(nickName, passwordClear, true); } else { error = B_TRANSLATE("Authentication failed. The user does " "not exist or the wrong password was supplied."); @@ -508,7 +508,7 @@ UserLoginWindow::_CreateAccountThread() fCaptchaToken = ""; _RequestCaptcha(); } else { - fModel.SetAuthorization(nickName, passwordClear); + fModel.SetAuthorization(nickName, passwordClear, true); _SetWorkerThread(-1); BMessenger(this).SendMessage(B_QUIT_REQUESTED);