2023-01-07 21:03:51 +05:30
|
|
|
package pages
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
|
|
|
|
"github.com/sethvargo/go-password/password"
|
|
|
|
|
|
|
|
"github.com/containrrr/shoutrrr"
|
2023-01-07 22:14:25 +05:30
|
|
|
|
|
|
|
"go.uber.org/zap"
|
2023-01-07 21:03:51 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// SignupPage is the signup page handler
|
|
|
|
func SignupPage(c *fiber.Ctx) error {
|
2023-01-07 22:14:25 +05:30
|
|
|
// set up logger
|
|
|
|
logger, _ := zap.NewProduction()
|
|
|
|
defer logger.Sync()
|
|
|
|
|
2023-01-07 21:03:51 +05:30
|
|
|
username := c.FormValue("username")
|
|
|
|
email := c.FormValue("email")
|
|
|
|
if username == "" || email == "" {
|
2023-01-07 22:14:25 +05:30
|
|
|
logger.Error("username or email is empty", zap.String("username", username), zap.String("email", email))
|
2023-01-07 21:03:51 +05:30
|
|
|
return c.SendStatus(fiber.StatusBadRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate password
|
|
|
|
pass, err := password.Generate(30, 10, 10, false, false)
|
|
|
|
if err != nil {
|
2023-01-07 22:14:25 +05:30
|
|
|
logger.Error("failed to generate password", zap.Error(err))
|
2023-01-07 21:03:51 +05:30
|
|
|
return c.SendStatus(fiber.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create user file
|
|
|
|
f, err := os.Create("/var/publapi/users/" + username + ".sh")
|
|
|
|
if err != nil {
|
2023-01-07 22:14:25 +05:30
|
|
|
logger.Error("failed to create user file", zap.Error(err))
|
2023-01-07 21:03:51 +05:30
|
|
|
return c.SendStatus(fiber.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
bashscript := "#!/bin/bash \n" +
|
|
|
|
"# Path: /var/publapi/users/.sh\n" +
|
|
|
|
"# This file is generated by publapi. Do not edit this file.\n" +
|
2023-01-07 21:12:28 +05:30
|
|
|
"useradd -Um -s /bin/bash " + username + "\n" +
|
2023-01-07 21:32:06 +05:30
|
|
|
"printf %s\n%s\n" + pass + " " + pass + " | passwd " + username + "\n" +
|
2023-01-07 21:12:28 +05:30
|
|
|
"echo " + username + "'s account has been created!"
|
2023-01-07 21:03:51 +05:30
|
|
|
|
|
|
|
fmt.Println(bashscript)
|
|
|
|
|
|
|
|
// write to file
|
|
|
|
_, err = f.WriteString(bashscript)
|
|
|
|
if err != nil {
|
2023-01-07 22:14:25 +05:30
|
|
|
logger.Error("failed to write to user file", zap.Error(err))
|
2023-01-07 21:03:51 +05:30
|
|
|
return c.SendStatus(fiber.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
2023-01-07 21:32:06 +05:30
|
|
|
// send notification to admins
|
|
|
|
err = shoutrrr.Send(os.Getenv("PUBLAPI_SHOUTRRRURL"), "New user signup! Please review /var/publapi/users/"+username+".sh to approve or deny the user.")
|
|
|
|
if err != nil {
|
2023-01-07 22:14:25 +05:30
|
|
|
logger.Error("failed to send notification to admins", zap.Error(err))
|
2023-01-07 21:32:06 +05:30
|
|
|
return c.SendStatus(fiber.StatusInternalServerError)
|
|
|
|
}
|
2023-01-07 21:03:51 +05:30
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"username": username,
|
|
|
|
"message": "User created! Please allow us 24 hours or more to review your account.",
|
|
|
|
"status": c.Response().StatusCode(),
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|