secure jwt
This commit is contained in:
43
core/secrets.go
Normal file
43
core/secrets.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
)
|
||||
|
||||
type NKodeSecrets struct {
|
||||
JwtSecret []byte `json:"jwt_secret"`
|
||||
}
|
||||
|
||||
func ReadSecrets(filePath string) (NKodeSecrets, error) {
|
||||
// Initialize an empty NKodeSecrets struct
|
||||
var secrets NKodeSecrets
|
||||
|
||||
// Read the contents of the file
|
||||
data, err := ioutil.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return secrets, fmt.Errorf("error reading secrets file: %w", err)
|
||||
}
|
||||
|
||||
// Unmarshal JSON data into the NKodeSecrets struct
|
||||
err = json.Unmarshal(data, &secrets)
|
||||
if err != nil {
|
||||
return secrets, fmt.Errorf("error unmarshaling secrets: %w", err)
|
||||
}
|
||||
|
||||
return secrets, nil
|
||||
}
|
||||
|
||||
func GetJwtSecret(filePath string) []byte {
|
||||
secrets, err := ReadSecrets(filePath)
|
||||
if err != nil {
|
||||
log.Fatal("can't read secrets: ", err)
|
||||
}
|
||||
if secrets.JwtSecret == nil {
|
||||
log.Fatal("wt secret is nil")
|
||||
}
|
||||
return secrets.JwtSecret
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user