Files
smt/controllers/unlock.go
Nathan Coad dc9ffceb3e
All checks were successful
continuous-integration/drone/push Build is passing
verify unlock key against hash on disk
2024-01-09 21:22:16 +11:00

46 lines
1.1 KiB
Go

package controllers
import (
"log"
"net/http"
"smt/models"
"github.com/gin-gonic/gin"
)
type UnlockInput struct {
SecretKey string `json:"secretKey"`
}
// Unlock receives secret key and store it in memory
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.Println("Unlock received JSON input")
if models.CheckKeyProvided() {
c.JSON(http.StatusBadRequest, gin.H{"error": "secret key can only be provided once after service start"})
return
}
// 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"})
}