welcome now puts credentials instead of generating

This commit is contained in:
Elia Zammuto
2021-07-30 16:06:59 +02:00
parent 28fecbc50c
commit 7f643345ce
6 changed files with 83 additions and 84 deletions

View File

@@ -92,17 +92,16 @@ bool authenticate(resp_https_t response, req_https_t request) {
return false;
}
//If credentials are shown, redirect the user to a /welcome page
if(config::sunshine.username.empty()){
send_redirect(response,request,"/welcome");
return false;
}
auto fg = util::fail_guard([&]() {
send_unauthorized(response, request);
});
//If credentials are shown, redirect the user to a /welcome page
if(config::sunshine.showCredentials){
send_redirect(response,request,"/welcome");
fg.disable();
return false;
}
auto auth = request->header.find("authorization");
if(auth == request->header.end()) {
return false;
@@ -203,7 +202,7 @@ void getPasswordPage(resp_https_t response, req_https_t request) {
void getWelcomePage(resp_https_t response, req_https_t request) {
print_req(request);
if(!config::sunshine.showCredentials){
if(!config::sunshine.username.empty()){
send_redirect(response,request,"/");
return;
}
@@ -362,29 +361,6 @@ void getConfig(resp_https_t response, req_https_t request) {
}
}
void getPlainPassword(resp_https_t response, req_https_t request) {
print_req(request);
pt::ptree outputTree;
auto g = util::fail_guard([&]() {
std::ostringstream data;
pt::write_json(data, outputTree);
response->write(data.str());
});
if(config::sunshine.showCredentials){
outputTree.put("status", "true");
outputTree.put("username", config::sunshine.username);
outputTree.put("password", config::sunshine.plainPassword);
config::sunshine.showCredentials = false;
config::sunshine.plainPassword = "";
} else {
outputTree.put("status", "false");
}
}
void saveConfig(resp_https_t response, req_https_t request) {
if(!authenticate(response, request)) return;
@@ -421,7 +397,7 @@ void saveConfig(resp_https_t response, req_https_t request) {
}
void savePassword(resp_https_t response, req_https_t request) {
if(!authenticate(response, request)) return;
if(!config::sunshine.username.empty() && !authenticate(response, request)) return;
print_req(request);
@@ -440,27 +416,31 @@ void savePassword(resp_https_t response, req_https_t request) {
try {
//TODO: Input Validation
pt::read_json(ss, inputTree);
auto username = inputTree.get<std::string>("currentUsername");
auto username = inputTree.count("currentUsername") > 0 ? inputTree.get<std::string>("currentUsername") : "";
auto newUsername = inputTree.get<std::string>("newUsername");
auto password = inputTree.get<std::string>("currentPassword");
auto password = inputTree.count("currentPassword") > 0 ? inputTree.get<std::string>("currentPassword") : "";
auto newPassword = inputTree.get<std::string>("newPassword");
auto confirmPassword = inputTree.get<std::string>("confirmNewPassword");
if(newUsername.length() == 0) newUsername = username;
auto hash = util::hex(crypto::hash(password + config::sunshine.salt)).to_string();
if(username == config::sunshine.username && hash == config::sunshine.password) {
if(newPassword != confirmPassword) {
outputTree.put("status", false);
outputTree.put("error", "Password Mismatch");
}
http::save_user_creds(config::sunshine.credentials_file, newUsername, newPassword);
http::reload_user_creds(config::sunshine.credentials_file);
outputTree.put("status", true);
}
else {
if(newUsername.length() == 0){
outputTree.put("status", false);
outputTree.put("error", "Invalid Current Credentials");
outputTree.put("error", "Invalid Username");
} else {
auto hash = util::hex(crypto::hash(password + config::sunshine.salt)).to_string();
if(config::sunshine.username.empty() || (username == config::sunshine.username && hash == config::sunshine.password)) {
if(newPassword != confirmPassword) {
outputTree.put("status", false);
outputTree.put("error", "Password Mismatch");
} else {
http::save_user_creds(config::sunshine.credentials_file, newUsername, newPassword);
http::reload_user_creds(config::sunshine.credentials_file);
outputTree.put("status", true);
}
}
else {
outputTree.put("status", false);
outputTree.put("error", "Invalid Current Credentials");
}
}
}
catch(std::exception &e) {
@@ -525,7 +505,6 @@ void start() {
server.resource["^/api/config$"]["POST"] = saveConfig;
server.resource["^/api/password$"]["POST"] = savePassword;
server.resource["^/api/apps/([0-9]+)$"]["DELETE"] = deleteApp;
server.resource["^/api/setup/password$"]["GET"] = getPlainPassword;
server.config.reuse_address = true;
server.config.address = "0.0.0.0"s;
server.config.port = port_https;