retrieve secret working

This commit is contained in:
2023-04-02 10:58:50 +10:00
parent ef2e4ee4e9
commit 2554c7f4ca
4 changed files with 117 additions and 12 deletions

View File

@@ -1,6 +1,9 @@
package controllers
import (
"ccsecrets/models"
"ccsecrets/utils/token"
"fmt"
"net/http"
"github.com/gin-gonic/gin"
@@ -14,8 +17,40 @@ type RetrieveInput struct {
func RetrieveSecret(c *gin.Context) {
var input RetrieveInput
// Validate the input matches our struct
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
fmt.Printf("StoreSecret received JSON input '%v'\n", input)
// Get the user and role id of the requestor
user_id, err := token.ExtractTokenID(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
u, err := models.GetUserRoleByID(user_id)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// Populate fields
s := models.Secret{}
s.RoleId = u.RoleId
s.DeviceName = input.DeviceName
s.DeviceCategory = input.DeviceCategory
results, err := models.GetSecrets(&s)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// output results as json
c.JSON(http.StatusOK, gin.H{"message": "success", "data": results})
}

View File

@@ -50,12 +50,13 @@ func StoreSecret(c *gin.Context) {
}
// This is just here for testing to make sure that decryption works
_, err = s.DecryptSecret()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"Error decrypting secret": err.Error()})
return
}
/*
_, err = s.DecryptSecret()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"Error decrypting secret": err.Error()})
return
}
*/
_, err = s.SaveSecret()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"Error saving secret": err.Error()})