Files
smt/controllers/unlock.go
Nathan Coad 484acd1822
All checks were successful
continuous-integration/drone/push Build is passing
more implementation of runtime unlock
2023-12-28 11:14:16 +11:00

42 lines
944 B
Go

package controllers
import (
"log"
"net/http"
"smt/models"
"github.com/gin-gonic/gin"
)
type UnlockInput struct {
SecretKey string `json:"secretKey"`
}
// receive secret key and store it using the Key model
func Unlock(c *gin.Context) {
var input UnlockInput
// Validate the input matches our struct
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
log.Printf("Unlock received JSON input '%v'\n", input)
// check that the key is 32 bytes long
if len(input.SecretKey) != 32 {
c.JSON(http.StatusBadRequest, gin.H{"error": "secret key provided is invalid, must be exactly 32 bytes long"})
return
}
err := models.ReceiveKey(input.SecretKey)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err})
return
}
// output results as json
c.JSON(http.StatusOK, gin.H{"message": "success", "data": "secret key successfully processed"})
}