From ad85946120b9388c448b4970800e921967072ae9 Mon Sep 17 00:00:00 2001 From: Arya Kiran Date: Tue, 17 Jan 2023 20:36:47 +0530 Subject: [PATCH] add ssh key value for signup --- main.go | 2 -- pages/signup.go | 25 ++++++++++++++++++------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index ba3c690..b6f3247 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "github.com/ProjectSegfault/publapi/pages" "github.com/gofiber/fiber/v2" log "github.com/sirupsen/logrus" @@ -81,7 +80,6 @@ func userdata(username string) Userinfo { user.desc = confparse(username, "desc") user.email = confparse(username, "email") user.loc = confparse(username, "loc") - //fmt.Println(user) return user } diff --git a/pages/signup.go b/pages/signup.go index b25341d..65473ae 100644 --- a/pages/signup.go +++ b/pages/signup.go @@ -1,10 +1,9 @@ package pages import ( - "fmt" - "os" - + "encoding/base64" "github.com/gofiber/fiber/v2" + "os" "github.com/containrrr/shoutrrr" @@ -16,8 +15,15 @@ func SignupPage(c *fiber.Ctx) error { username := c.FormValue("username") email := c.FormValue("email") - if username == "" || email == "" { - log.Error("Username or email is empty", username, email) + ssh := c.FormValue("ssh") + 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) } @@ -28,13 +34,18 @@ func SignupPage(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusInternalServerError) } defer f.Close() - bashscript := "#!/bin/bash \n" + "# Path: /var/publapi/users/" + username + ".sh\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 /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" + "chmod 600 /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) } - 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 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 {