more cleanups

This commit is contained in:
2024-09-29 14:35:33 +02:00
parent dc6fb02d5f
commit 97b006259b
4 changed files with 92 additions and 314 deletions

View File

@@ -8,10 +8,11 @@ import (
"os"
"strings"
log "log/slog"
"github.com/ProjectSegfault/publapi/utils"
"github.com/containrrr/shoutrrr"
"github.com/gofiber/fiber/v2"
log "github.com/sirupsen/logrus"
)
type formValues struct {
@@ -26,6 +27,44 @@ type CaptchaResponse struct {
Success bool `json:"success"`
}
func CaptchaCheck(response string, secret string) bool {
// Check the captcha validation.
params := url.Values{}
params.Add("response", response)
params.Add("secret", secret)
body := strings.NewReader(params.Encode())
req, err := http.NewRequest("POST", "https://hcaptcha.com/siteverify", body)
if err != nil {
log.Error("couldn't make request to hCaptcha verification API", log.Any("err", err))
return false
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Error("couldn't do request to hCaptcha verification API", log.Any("err", err))
return false
}
defer resp.Body.Close()
bod, err := io.ReadAll(resp.Body)
if err != nil {
log.Error("couldn't read response body", log.Any("err", err))
return false
}
sb := string(bod)
captchaResponse := CaptchaResponse{}
err = json.Unmarshal([]byte(sb), &captchaResponse)
if err != nil {
log.Error("couldn't unmarshal hCaptcha response into a CaptchaResponse struct", log.Any("err", err))
}
return captchaResponse.Success
}
// SignupPage is the signup page handler
func SignupPage(c *fiber.Ctx) error {
SignupIP, SignupIPExists := os.LookupEnv("PUBLAPI_SIGNUP_IP")
@@ -45,7 +84,6 @@ func SignupPage(c *fiber.Ctx) error {
}
if formValues.CaptchaResponse == "" {
log.Error("Nice try, but the registration won't work unless you answer the captcha.")
return c.SendStatus(fiber.StatusBadRequest)
}
@@ -67,45 +105,13 @@ func SignupPage(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusOK)
}
// Check the captcha validation.
// get captcha secret
captchaSecret, _ := os.LookupEnv("PUBLAPI_CAPTCHA_SECRET")
params := url.Values{}
params.Add("response", formValues.CaptchaResponse)
params.Add("secret", captchaSecret)
body := strings.NewReader(params.Encode())
req, err := http.NewRequest("POST", "https://hcaptcha.com/siteverify", body)
if err != nil {
// handle err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Error("Something went wrong fetching the HCatpcha API: ", err)
}
defer resp.Body.Close()
bod, err := io.ReadAll(resp.Body)
if err != nil {
log.Error("Error reading captcha response body", err)
}
sb := string(bod)
log.Info("Captcha response: ", sb)
captchaResponse := CaptchaResponse{}
err = json.Unmarshal([]byte(sb), &captchaResponse)
if err != nil {
log.Error("Error unmarshalling captcha response", err)
}
if !captchaResponse.Success {
if !CaptchaCheck(formValues.CaptchaResponse, captchaSecret) {
log.Error("Captcha validation failed")
return c.JSON(fiber.Map{
"username": formValues.CaptchaResponse,
"username": formValues.Username,
"message": "Sorry, but the captcha validation failed. Please try again.",
"status": c.Response().StatusCode(),
})
@@ -132,7 +138,7 @@ func SignupPage(c *fiber.Ctx) error {
defer f.Close()
chmoderr := os.Chmod("/var/publapi/users/"+formValues.Username+".sh", 0700)
if chmoderr != nil {
log.Error(err)
UserError("couldn't chmod users script with permissions 0700", formValues.Username, chmoderr)
}
bashScript := strings.ReplaceAll(utils.BashScript, "{{sshkey}}", formValues.SshPublicKey)
bashScript = strings.ReplaceAll(bashScript, "{{email}}", formValues.Email)
@@ -164,7 +170,7 @@ func SignupPage(c *fiber.Ctx) error {
"New user signup! Please review /var/publapi/users/"+formValues.Username+".sh to approve or deny the user. IP: "+formValues.IPAddress+" Email: "+formValues.Email,
)
if err != nil {
log.Error("Error sending notification to admins", err)
log.Error("error sending notification to admins", log.Any("err", err))
//return c.SendStatus(fiber.StatusInternalServerError)
}
return c.JSON(fiber.Map{

View File

@@ -73,8 +73,7 @@ func userData(username, usersonline, ops string) UserInfo {
if crerr != nil {
UserError("couldn't stat user", username, crerr)
}
crdstr := string(crd)
crdstr = strings.TrimSuffix(crdstr, "\n")
crdstr := strings.TrimSuffix(string(crd), "\n")
filename := "/home/" + username + "/meta-info.toml"
_, error := os.Stat(filename)
if error != nil {