summaryrefslogtreecommitdiff
path: root/pkg/helpers
diff options
context:
space:
mode:
authorJulien Dessaux2024-11-17 00:05:22 +0100
committerJulien Dessaux2024-12-17 23:19:18 +0100
commit25ed1188ed970a19675befef12afe68045565c4a (patch)
tree5cfc55047d833400028fb69a7f069f5cedecaacd /pkg/helpers
parentfix(tfstated): hash passwords instead of relying on the database encryption key (diff)
downloadtfstated-25ed1188ed970a19675befef12afe68045565c4a.tar.gz
tfstated-25ed1188ed970a19675befef12afe68045565c4a.tar.bz2
tfstated-25ed1188ed970a19675befef12afe68045565c4a.zip
chore(tfstated): refactor helpers to their own package
Diffstat (limited to '')
-rw-r--r--pkg/helpers/crypto.go21
-rw-r--r--pkg/helpers/error.go17
-rw-r--r--pkg/helpers/json.go (renamed from cmd/tfstated/helpers.go)17
3 files changed, 41 insertions, 14 deletions
diff --git a/pkg/helpers/crypto.go b/pkg/helpers/crypto.go
new file mode 100644
index 0000000..ce73cd3
--- /dev/null
+++ b/pkg/helpers/crypto.go
@@ -0,0 +1,21 @@
+package helpers
+
+import (
+ "crypto/sha256"
+
+ "git.adyxax.org/adyxax/tfstated/pkg/scrypto"
+ "golang.org/x/crypto/pbkdf2"
+)
+
+const (
+ PBKDF2Iterations = 600000
+ SaltSize = 32
+)
+
+func GenerateSalt() []byte {
+ return scrypto.RandomBytes(SaltSize)
+}
+
+func HashPassword(password string, salt []byte) []byte {
+ return pbkdf2.Key([]byte(password), salt, PBKDF2Iterations, 32, sha256.New)
+}
diff --git a/pkg/helpers/error.go b/pkg/helpers/error.go
new file mode 100644
index 0000000..006759d
--- /dev/null
+++ b/pkg/helpers/error.go
@@ -0,0 +1,17 @@
+package helpers
+
+import (
+ "fmt"
+ "net/http"
+)
+
+func ErrorResponse(w http.ResponseWriter, status int, err error) {
+ type errorResponse struct {
+ Msg string `json:"msg"`
+ Status int `json:"status"`
+ }
+ _ = Encode(w, status, &errorResponse{
+ Msg: fmt.Sprintf("%+v", err),
+ Status: status,
+ })
+}
diff --git a/cmd/tfstated/helpers.go b/pkg/helpers/json.go
index 33e14a4..664a984 100644
--- a/cmd/tfstated/helpers.go
+++ b/pkg/helpers/json.go
@@ -1,4 +1,4 @@
-package main
+package helpers
import (
"encoding/json"
@@ -7,14 +7,14 @@ import (
"net/http"
)
-func decode(r *http.Request, data any) error {
+func Decode(r *http.Request, data any) error {
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
return fmt.Errorf("failed to decode json: %w", err)
}
return nil
}
-func encode(w http.ResponseWriter, status int, data any) error {
+func Encode(w http.ResponseWriter, status int, data any) error {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
if err := json.NewEncoder(w).Encode(data); err != nil {
@@ -23,14 +23,3 @@ func encode(w http.ResponseWriter, status int, data any) error {
}
return nil
}
-
-func errorResponse(w http.ResponseWriter, status int, err error) error {
- type errorResponse struct {
- Msg string `json:"msg"`
- Status int `json:"status"`
- }
- return encode(w, status, &errorResponse{
- Msg: fmt.Sprintf("%+v", err),
- Status: status,
- })
-}