From 1eaa4a67879aeef8c891c31209baf82c67a87abb Mon Sep 17 00:00:00 2001 From: luocai Date: Thu, 31 Oct 2024 19:40:29 +0800 Subject: [PATCH] add code. --- WebApplication/CMakeLists.txt | 4 ++ WebApplication/Hello.cpp | 29 +++++++++++ WebApplication/Hello.h | 5 ++ WebApplication/Session.cpp | 80 +++++++++++++++++++++++++++++++ WebApplication/Session.h | 25 ++++++++++ WebApplication/User.cpp | 4 ++ WebApplication/User.h | 22 +++++++++ WebApplication/WebApplication.cpp | 3 +- resource/deploy.sh | 12 ++--- 9 files changed, 177 insertions(+), 7 deletions(-) create mode 100644 WebApplication/Session.cpp create mode 100644 WebApplication/Session.h create mode 100644 WebApplication/User.cpp create mode 100644 WebApplication/User.h diff --git a/WebApplication/CMakeLists.txt b/WebApplication/CMakeLists.txt index 62ef84f..cf6aa21 100644 --- a/WebApplication/CMakeLists.txt +++ b/WebApplication/CMakeLists.txt @@ -1,6 +1,8 @@ add_library(WebApplication WebApplication.h WebApplication.cpp Hello.h Hello.cpp + Session.h Session.cpp + User.h User.cpp ) target_include_directories(WebApplication @@ -16,5 +18,7 @@ target_link_libraries(WebApplication PUBLIC wt PUBLIC wttest PUBLIC wthttp + PUBLIC wtdbo + PUBLIC wtdbosqlite3 PUBLIC Universal ) \ No newline at end of file diff --git a/WebApplication/Hello.cpp b/WebApplication/Hello.cpp index ffcd86e..86caca6 100644 --- a/WebApplication/Hello.cpp +++ b/WebApplication/Hello.cpp @@ -1,5 +1,11 @@ #include "Hello.h" #include "BoostLog.h" +#include "Session.h" +#include +#include +#include +#include +#include #include #include #include @@ -7,7 +13,11 @@ #include Hello::Hello(const Wt::WEnvironment &env, bool embedded) : Wt::WApplication(env) { + LOG(info) << "app root: " << appRoot(); + m_session = std::make_unique(appRoot() + "auth.db"); + m_session->login().changed().connect(this, &Hello::authEvent); setTitle("Hello world"); + setTheme(std::make_shared()); Wt::WContainerWidget *top = nullptr; if (!embedded) { top = root(); @@ -45,8 +55,27 @@ Hello::Hello(const Wt::WEnvironment &env, bool embedded) : Wt::WApplication(env) b->clicked().connect(this, &Hello::greet); m_nameEdit->enterPressed().connect(this, &Hello::greet); + + auto authWidget = std::make_unique(Session::auth(), m_session->users(), m_session->login()); + authWidget->model()->addPasswordAuth(&Session::passwordAuth()); + authWidget->setRegistrationEnabled(true); + authWidget->processEnvironment(); + top->addWidget(std::move(authWidget)); +} + +Hello::~Hello() { } void Hello::greet() { m_greeting->setText("Hello there, " + m_nameEdit->text()); } + +void Hello::authEvent() { + if (m_session->login().loggedIn()) { + const Wt::Auth::User &u = m_session->login().user(); + LOG(info) << "User " << u.id() << " (" << u.identity(Wt::Auth::Identity::LoginName) << ")" + << " logged in."; + } else { + LOG(info) << "User logged out."; + } +} diff --git a/WebApplication/Hello.h b/WebApplication/Hello.h index becb1b8..735c5c4 100644 --- a/WebApplication/Hello.h +++ b/WebApplication/Hello.h @@ -3,16 +3,21 @@ #include +class Session; + class Hello : public Wt::WApplication { public: Hello(const Wt::WEnvironment &env, bool embedded); + ~Hello(); protected: void greet(); + void authEvent(); private: Wt::WLineEdit *m_nameEdit = nullptr; Wt::WText *m_greeting = nullptr; + std::unique_ptr m_session; }; #endif // __HELLO_H__ \ No newline at end of file diff --git a/WebApplication/Session.cpp b/WebApplication/Session.cpp new file mode 100644 index 0000000..4ca949f --- /dev/null +++ b/WebApplication/Session.cpp @@ -0,0 +1,80 @@ +#include "Session.h" +#include "BoostLog.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +Wt::Auth::AuthService myAuthService; +Wt::Auth::PasswordService myPasswordService(myAuthService); +std::vector> myOAuthServices; + +Session::Session(const std::string &sqliteDb) { + auto connection = std::make_unique(sqliteDb); + + connection->setProperty("show-queries", "true"); + + setConnection(std::move(connection)); + + mapClass("user"); + mapClass("auth_info"); + mapClass("auth_identity"); + mapClass("auth_token"); + + try { + createTables(); + LOG(info) << "Created database."; + } catch (Wt::Dbo::Exception &e) { + LOG(error) << e.what() << ", using existing database"; + } + + m_users = std::make_unique(*this); +} + +Session::~Session() { +} + +Wt::Auth::AbstractUserDatabase &Session::users() { + return *m_users; +} + +Wt::Auth::Login &Session::login() { + return m_login; +} + +void Session::configureAuth() { + myAuthService.setAuthTokensEnabled(true, "logincookie"); + // myAuthService.setEmailVerificationEnabled(true); + // myAuthService.setEmailVerificationRequired(true); + + auto verifier = std::make_unique(); + verifier->addHashFunction(std::make_unique(7)); + myPasswordService.setVerifier(std::move(verifier)); + myPasswordService.setPasswordThrottle(std::make_unique()); + myPasswordService.setStrengthValidator(std::make_unique()); + + if (Wt::Auth::GoogleService::configured()) { + myOAuthServices.push_back(std::make_unique(myAuthService)); + } + + if (Wt::Auth::FacebookService::configured()) { + myOAuthServices.push_back(std::make_unique(myAuthService)); + } + + for (const auto &oAuthService : myOAuthServices) { + oAuthService->generateRedirectEndpoint(); + } +} + +const Wt::Auth::AuthService &Session::auth() { + return myAuthService; +} + +const Wt::Auth::PasswordService &Session::passwordAuth() { + return myPasswordService; +} diff --git a/WebApplication/Session.h b/WebApplication/Session.h new file mode 100644 index 0000000..6adddcd --- /dev/null +++ b/WebApplication/Session.h @@ -0,0 +1,25 @@ +#ifndef __SESSION_H__ +#define __SESSION_H__ + +#include "User.h" +#include +#include + +using UserDatabase = Wt::Auth::Dbo::UserDatabase; + +class Session : public Wt::Dbo::Session { +public: + Session(const std::string &sqliteDb); + ~Session(); + Wt::Auth::AbstractUserDatabase &users(); + Wt::Auth::Login &login(); + static void configureAuth(); + static const Wt::Auth::AuthService &auth(); + static const Wt::Auth::PasswordService &passwordAuth(); + +private: + std::unique_ptr m_users; + Wt::Auth::Login m_login; +}; + +#endif // __SESSION_H__ \ No newline at end of file diff --git a/WebApplication/User.cpp b/WebApplication/User.cpp new file mode 100644 index 0000000..1ff4e06 --- /dev/null +++ b/WebApplication/User.cpp @@ -0,0 +1,4 @@ +#include "User.h" +#include + +DBO_INSTANTIATE_TEMPLATES(User) \ No newline at end of file diff --git a/WebApplication/User.h b/WebApplication/User.h new file mode 100644 index 0000000..3d76d6b --- /dev/null +++ b/WebApplication/User.h @@ -0,0 +1,22 @@ +#ifndef __USER_H__ +#define __USER_H__ + +#include +#include + +class User; +using AuthInfo = Wt::Auth::Dbo::AuthInfo; + +class User { +public: + template + void persist(Action &a) { + // Wt::Dbo::field(a, name, "name"); + } + // std::string name; + // int age; +}; + +DBO_EXTERN_TEMPLATES(User) + +#endif // __USER_H__ \ No newline at end of file diff --git a/WebApplication/WebApplication.cpp b/WebApplication/WebApplication.cpp index 266ff80..154d193 100644 --- a/WebApplication/WebApplication.cpp +++ b/WebApplication/WebApplication.cpp @@ -1,6 +1,7 @@ #include "WebApplication.h" #include "BoostLog.h" #include "Hello.h" +#include "Session.h" #include static std::unique_ptr createApplication(const Wt::WEnvironment &env) { @@ -20,9 +21,9 @@ WebApplication::WebApplication() { m_server = std::make_unique("./build", args); m_server->addEntryPoint(Wt::EntryPointType::Application, createApplication); m_server->addEntryPoint(Wt::EntryPointType::WidgetSet, createWidgetSet, "/gui/hello.js"); + Session::configureAuth(); m_thread = std::thread(&WebApplication::run, this); - } catch (const std::exception &e) { LOG(error) << e.what(); } diff --git a/resource/deploy.sh b/resource/deploy.sh index fad2fb7..b6498d2 100755 --- a/resource/deploy.sh +++ b/resource/deploy.sh @@ -2,8 +2,8 @@ set -e -SERVER_ADDRESS=192.168.3.3 -SERVER_PORT=5022 +SERVER_ADDRESS=172.16.103.87 +SERVER_PORT=22 USER=root function update() { @@ -24,6 +24,10 @@ function init() { scp -P $SERVER_PORT resource/frps.service $USER@$SERVER_ADDRESS:/etc/systemd/system/frps.service ssh $USER@$SERVER_ADDRESS -p $SERVER_PORT < /dev/null 2>&1; then echo "OpenResty has installed." else @@ -58,10 +62,6 @@ function init() { apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin fi - if [ ! -d /root/Server ]; then - mkdir -p /root/Server /root/Server/logs - fi - if [ "$(docker ps -q -f name=librespeed)" ]; then echo "librespeed has running." else