improve checks
This commit is contained in:
@@ -36,6 +36,19 @@ func Register(c *gin.Context) {
|
||||
u.UserName = input.Username
|
||||
u.Password = input.Password
|
||||
|
||||
//remove spaces in username
|
||||
u.UserName = html.EscapeString(strings.TrimSpace(u.UserName))
|
||||
|
||||
// Check if user already exists
|
||||
testUser, _ := models.GetUserByName(u.UserName)
|
||||
fmt.Printf("Register checking if user already exists : '%v'\n", testUser)
|
||||
if (models.User{} == testUser) {
|
||||
fmt.Printf("Register confirmed no existing username\n")
|
||||
} else {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Attempt to register conflicting username"})
|
||||
return
|
||||
}
|
||||
|
||||
//turn password into hash
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
@@ -46,9 +59,6 @@ func Register(c *gin.Context) {
|
||||
}
|
||||
u.Password = string(hashedPassword)
|
||||
|
||||
//remove spaces in username
|
||||
u.UserName = html.EscapeString(strings.TrimSpace(u.UserName))
|
||||
|
||||
_, err = u.SaveUser()
|
||||
|
||||
if err != nil {
|
||||
|
@@ -3,6 +3,7 @@ package controllers
|
||||
import (
|
||||
"ccsecrets/models"
|
||||
"ccsecrets/utils/token"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
@@ -17,6 +18,52 @@ 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("RetrieveSecret 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
|
||||
}
|
||||
|
||||
if len(results) > 1 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": errors.New("found multiple matching secrets, use retrieveMultiple instead")})
|
||||
return
|
||||
}
|
||||
|
||||
// output results as json
|
||||
c.JSON(http.StatusOK, gin.H{"message": "success", "data": results})
|
||||
}
|
||||
|
||||
func RetrieveMultpleSecrets(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()})
|
||||
|
@@ -41,6 +41,19 @@ func StoreSecret(c *gin.Context) {
|
||||
s.RoleId = 1
|
||||
}
|
||||
|
||||
// If this secret already exists in the database then generate an error
|
||||
checkExists, err := models.GetSecrets(&s)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if len(checkExists) > 0 {
|
||||
fmt.Printf("StoreSecret not storing secret with '%d' already matching secrets.\n", len(checkExists))
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "StoreSecret attempting to store secret already defined. API calls for update/delete don't yet exist"})
|
||||
return
|
||||
}
|
||||
|
||||
// Encrypt secret
|
||||
s.Secret = input.SecretValue
|
||||
_, err = s.EncryptSecret()
|
||||
@@ -49,14 +62,6 @@ func StoreSecret(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 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.SaveSecret()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"Error saving secret": err.Error()})
|
||||
|
Reference in New Issue
Block a user