add ssh key value for signup

This commit is contained in:
Arya Kiran 2023-01-17 20:36:47 +05:30
parent d1c36f7af4
commit ad85946120
Signed by: arya
GPG Key ID: 842D12BDA50DF120
2 changed files with 18 additions and 9 deletions

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"github.com/ProjectSegfault/publapi/pages" "github.com/ProjectSegfault/publapi/pages"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -81,7 +80,6 @@ func userdata(username string) Userinfo {
user.desc = confparse(username, "desc") user.desc = confparse(username, "desc")
user.email = confparse(username, "email") user.email = confparse(username, "email")
user.loc = confparse(username, "loc") user.loc = confparse(username, "loc")
//fmt.Println(user)
return user return user
} }

View File

@ -1,10 +1,9 @@
package pages package pages
import ( import (
"fmt" "encoding/base64"
"os"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"os"
"github.com/containrrr/shoutrrr" "github.com/containrrr/shoutrrr"
@ -16,8 +15,15 @@ func SignupPage(c *fiber.Ctx) error {
username := c.FormValue("username") username := c.FormValue("username")
email := c.FormValue("email") email := c.FormValue("email")
if username == "" || email == "" { ssh := c.FormValue("ssh")
log.Error("Username or email is empty", username, email) if username == "" || email == "" || ssh == "" {
log.Error("username, email and ssh must be filled", username, email, ssh)
return c.SendStatus(fiber.StatusBadRequest)
}
sshke, err := base64.StdEncoding.DecodeString(ssh)
sshkey := string(sshke)
if err != nil {
log.Error("unable to decode base64 ssh key")
return c.SendStatus(fiber.StatusBadRequest) return c.SendStatus(fiber.StatusBadRequest)
} }
@ -28,13 +34,18 @@ func SignupPage(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusInternalServerError) return c.SendStatus(fiber.StatusInternalServerError)
} }
defer f.Close() defer f.Close()
bashscript := "#!/bin/bash \n" + bashscript := "#!/bin/bash \n" +
"# Path: /var/publapi/users/" + username + ".sh\n" + "# Path: /var/publapi/users/" + username + ".sh\n" +
"# This file is generated by publapi. Do not edit this file.\n" + "# This file is generated by publapi. Do not edit this file.\n" +
"echo \"email of " + username + " is " + email + "\"\n" +
"pass=\"$(tr -dc A-Za-z0-9 </dev/urandom | head -c 64)\"\n" + "pass=\"$(tr -dc A-Za-z0-9 </dev/urandom | head -c 64)\"\n" +
"useradd -Um -s /bin/bash " + username + "\n" + "useradd -Um -s /bin/bash " + username + "\n" +
"printf \"%s\\n%s\" \"${pass}\" \"${pass}\" | passwd " + username + "\n" + "printf \"%s\\n%s\" \"${pass}\" \"${pass}\" | passwd " + username + "\n" +
"mkdir /home/" + username + "/.ssh\n" +
"echo '" + sshkey + "' > /home/" + username + "/.ssh/authorized_keys\n" +
"chmod 700 /home/" + username + "/.ssh\n" +
"chmod 600 /home/" + username + "/.ssh/authorized_keys\n" +
"chown -R " + username + ":" + username + " /home/" + username + "/.ssh\n" +
"echo \"${pass}\" > /home/" + username + "/pass\n" + "echo \"${pass}\" > /home/" + username + "/pass\n" +
"chmod 600 /home/" + username + "/pass\n" + "chmod 600 /home/" + username + "/pass\n" +
"chown " + username + ":" + username + " /home/" + username + "/pass\n" + "chown " + username + ":" + username + " /home/" + username + "/pass\n" +
@ -47,7 +58,7 @@ func SignupPage(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusInternalServerError) return c.SendStatus(fiber.StatusInternalServerError)
} }
fmt.Println("Registration request for " + username + " has been submitted by the frontend and has been written to /var/publapi/users/" + username + ".sh") log.Info("Registration request for " + username + " has been submitted by the frontend and has been written to /var/publapi/users/" + username + ".sh")
// send notification to admins // 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.") 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 { if err != nil {