updated certs endpoint and delete certs/reate certs/delete endpoints
This commit is contained in:
1 parent
b42e9ac7da
commit
f05ebf2ae7
7 files changed
+71
-64
No files matched your search
+11
-51
@@ -1,61 +1,21 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"scm/crt"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Certs(w http.ResponseWriter, r *http.Request) {
|
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)
|
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) {
|
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
|
var requestData model.CreateCertRequest
|
||||||
if err := json.NewDecoder(r.Body).Decode(&requestData); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(&requestData); err != nil {
|
||||||
http.Error(w, "invalid JSON format", http.StatusBadRequest)
|
http.Error(w, "invalid JSON format", http.StatusBadRequest)
|
||||||
|
|||||||
@@ -9,10 +9,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func DeleteCert(w http.ResponseWriter, r *http.Request) {
|
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
|
var requestData model.DeleteCertRequest
|
||||||
if err := json.NewDecoder(r.Body).Decode(&requestData); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(&requestData); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
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) => {
|
cards.addEventListener("click", (e) => {
|
||||||
if (e.target.classList.contains("delcert")) {
|
if (e.target.classList.contains("delcert")) {
|
||||||
if (confirm("Are you sure you want to delete this certificate?")) {
|
if (confirm("Are you sure you want to delete this certificate?")) {
|
||||||
fetch(window.location.origin + `/api/certs/delete`, {
|
fetch(window.location.origin + `/api/certs`, {
|
||||||
method: "POST",
|
method: "DELETE",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ create_certificate_form.addEventListener("submit", (event) => {
|
|||||||
const passwordValue = password.value;
|
const passwordValue = password.value;
|
||||||
const organizationunitValue = organizationunit.value;
|
const organizationunitValue = organizationunit.value;
|
||||||
|
|
||||||
fetch(window.location.origin + "/api/certs/create", {
|
fetch(window.location.origin + "/api/certs", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
|
|||||||
@@ -10,6 +10,4 @@ func Configure() {
|
|||||||
http.HandleFunc("/api/certs", api.Certs)
|
http.HandleFunc("/api/certs", api.Certs)
|
||||||
http.HandleFunc("/api/certs/root", api.RootCert)
|
http.HandleFunc("/api/certs/root", api.RootCert)
|
||||||
http.HandleFunc("/api/certs/download/", api.CertKeyDownloadZip)
|
http.HandleFunc("/api/certs/download/", api.CertKeyDownloadZip)
|
||||||
http.HandleFunc("/api/certs/create", api.CreateCert)
|
|
||||||
http.HandleFunc("/api/certs/delete", api.DeleteCert)
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user