gPanel/pkg/api/ssh/getkeys.go
2018-01-10 20:57:21 -06:00

52 lines
1.3 KiB
Go

package ssh
import (
"encoding/json"
"log"
"net/http"
"strconv"
"github.com/Ennovar/gPanel/pkg/system"
)
func GetKeys(res http.ResponseWriter, req *http.Request, logger *log.Logger) bool {
if req.Method != "POST" {
logger.Println(req.URL.Path + "::" + req.Method + "::" + strconv.Itoa(http.StatusMethodNotAllowed) + "::" + http.StatusText(http.StatusMethodNotAllowed))
http.Error(res, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return false
}
var requestData struct {
Username string `json:"username"`
}
err := json.NewDecoder(req.Body).Decode(&requestData)
if err != nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusBadRequest)
return false
}
var keys []string
if keys, err = system.GetAuthorizedKeys(requestData.Username); err != nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusInternalServerError)
return false
}
if len(keys) > 0 {
b, err := json.Marshal(keys)
if err != nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusInternalServerError)
return false
}
res.WriteHeader(http.StatusOK)
res.Write(b)
return true
}
res.WriteHeader(http.StatusNoContent)
return true
}