updated certs endpoint and delete certs/reate certs/delete endpoints
This commit is contained in:
+11
-51
@@ -1,61 +1,21 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"scm/crt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func Certs(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "POST" {
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
GetCerts(w, r)
|
||||
return
|
||||
case "POST":
|
||||
CreateCert(w, r)
|
||||
return
|
||||
case "DELETE":
|
||||
DeleteCert(w, r)
|
||||
return
|
||||
default:
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
var certs_info []map[string]any
|
||||
entires, err := os.ReadDir(os.Getenv("CERTS_PATH"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
for _, entry := range entires {
|
||||
if entry.IsDir() {
|
||||
if entry.Name() == ".git" || entry.Name() == "root" {
|
||||
continue
|
||||
}
|
||||
id, err := uuid.NewRandom()
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, "{\"error\":\"%v\"}", err)
|
||||
return
|
||||
}
|
||||
certdata, err := os.ReadFile(fmt.Sprintf("%s/%s/%s.crt", os.Getenv("CERTS_PATH"), entry.Name(), entry.Name()))
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, "{\"error\":\"%v\"}", err)
|
||||
return
|
||||
}
|
||||
keydata, err := os.ReadFile(fmt.Sprintf("%s/%s/%s.key", os.Getenv("CERTS_PATH"), entry.Name(), entry.Name()))
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, "{\"error\":\"%v\"}", err)
|
||||
return
|
||||
}
|
||||
new_cert := make(map[string]any)
|
||||
new_cert["id"] = id.String()
|
||||
new_cert["domain"] = entry.Name()
|
||||
new_cert["href"] = fmt.Sprintf("https://%s", entry.Name())
|
||||
new_cert["cert"] = string(certdata)
|
||||
new_cert["key"] = string(keydata)
|
||||
new_cert["certinfo"] = crt.ParseCert(fmt.Sprintf("%s/%s/%s.crt", os.Getenv("CERTS_PATH"), entry.Name(), entry.Name()))
|
||||
certs_info = append(certs_info, new_cert)
|
||||
}
|
||||
}
|
||||
jsondata, err := json.Marshal(certs_info)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(jsondata)
|
||||
}
|
||||
|
||||
@@ -14,10 +14,6 @@ import (
|
||||
)
|
||||
|
||||
func CreateCert(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "GET" {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
var requestData model.CreateCertRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&requestData); err != nil {
|
||||
http.Error(w, "invalid JSON format", http.StatusBadRequest)
|
||||
|
||||
@@ -9,10 +9,6 @@ import (
|
||||
)
|
||||
|
||||
func DeleteCert(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "GET" {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
var requestData model.DeleteCertRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&requestData); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"scm/crt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func GetCerts(w http.ResponseWriter, r *http.Request) {
|
||||
var certs_info []map[string]any
|
||||
entires, err := os.ReadDir(os.Getenv("CERTS_PATH"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
for _, entry := range entires {
|
||||
if entry.IsDir() {
|
||||
if entry.Name() == ".git" || entry.Name() == "root" {
|
||||
continue
|
||||
}
|
||||
id, err := uuid.NewRandom()
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, "{\"error\":\"%v\"}", err)
|
||||
return
|
||||
}
|
||||
certdata, err := os.ReadFile(fmt.Sprintf("%s/%s/%s.crt", os.Getenv("CERTS_PATH"), entry.Name(), entry.Name()))
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, "{\"error\":\"%v\"}", err)
|
||||
return
|
||||
}
|
||||
keydata, err := os.ReadFile(fmt.Sprintf("%s/%s/%s.key", os.Getenv("CERTS_PATH"), entry.Name(), entry.Name()))
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, "{\"error\":\"%v\"}", err)
|
||||
return
|
||||
}
|
||||
new_cert := make(map[string]any)
|
||||
new_cert["id"] = id.String()
|
||||
new_cert["domain"] = entry.Name()
|
||||
new_cert["href"] = fmt.Sprintf("https://%s", entry.Name())
|
||||
new_cert["cert"] = string(certdata)
|
||||
new_cert["key"] = string(keydata)
|
||||
new_cert["certinfo"] = crt.ParseCert(fmt.Sprintf("%s/%s/%s.crt", os.Getenv("CERTS_PATH"), entry.Name(), entry.Name()))
|
||||
certs_info = append(certs_info, new_cert)
|
||||
}
|
||||
}
|
||||
jsondata, err := json.Marshal(certs_info)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(jsondata)
|
||||
}
|
||||
@@ -103,8 +103,8 @@ if (preview) {
|
||||
cards.addEventListener("click", (e) => {
|
||||
if (e.target.classList.contains("delcert")) {
|
||||
if (confirm("Are you sure you want to delete this certificate?")) {
|
||||
fetch(window.location.origin + `/api/certs/delete`, {
|
||||
method: "POST",
|
||||
fetch(window.location.origin + `/api/certs`, {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
|
||||
@@ -15,7 +15,7 @@ create_certificate_form.addEventListener("submit", (event) => {
|
||||
const passwordValue = password.value;
|
||||
const organizationunitValue = organizationunit.value;
|
||||
|
||||
fetch(window.location.origin + "/api/certs/create", {
|
||||
fetch(window.location.origin + "/api/certs", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
|
||||
Reference in New Issue
Block a user