usersList, ok := users["users"].([]any)
if !ok || len(usersList) == 0 {
utils.Logger.Printf("Login failed. User '%s' not found", req.Username)
- /* http.Error(w, "User/Password combination not found", http.StatusNotFound)
- return */
}
// Find user
}
// Register handles user registration
+// The API endpoint
func RegisterHandler(w http.ResponseWriter, r *http.Request) {
// Parse the request body
var req RegisterRequest
})
}
+// The actual register function (can also be called from migration)
func Register(username string, password string) (bool, error) {
+ utils.UsersFileMutex.Lock()
+ defer utils.UsersFileMutex.Unlock()
+
// Get users
users, err := utils.GetUsers()
if err != nil {
return
}
+ utils.UsersFileMutex.Lock()
+ defer utils.UsersFileMutex.Unlock()
+
// Get user data
users, err := utils.GetUsers()
if err != nil {
return
}
+ utils.UsersFileMutex.Lock()
+ defer utils.UsersFileMutex.Unlock()
+
// Get User data
users, err := utils.GetUsers()
if err != nil {
return
}
+ utils.UsersFileMutex.Unlock()
+
// Delete directory of the user with all his data
if err := utils.DeleteUserData(userID); err != nil {
utils.JSONResponse(w, http.StatusOK, map[string]any{
// Mutexes für Dateizugriffe
var (
- usersFileMutex sync.RWMutex // Für users.json
+ UsersFileMutex sync.RWMutex // Für users.json
userSettingsMutex sync.RWMutex // Für Benutzereinstellungen
)
// GetUsers retrieves the users from the users.json file
func GetUsers() (map[string]any, error) {
- usersFileMutex.RLock()
- defer usersFileMutex.RUnlock()
-
// Try to open the users.json file
filePath := filepath.Join(Settings.DataPath, "users.json")
file, err := os.Open(filePath)
// WriteUsers writes the users to the users.json file
func WriteUsers(content map[string]any) error {
- usersFileMutex.Lock()
- defer usersFileMutex.Unlock()
// Create the users.json file
filePath := filepath.Join(Settings.DataPath, "users.json")
// saves the hash, salt and encrypted derived key of the backup codes to the users.json file
func SaveBackupCodes(userID int, codes []map[string]any) error {
+ UsersFileMutex.Lock()
+ defer UsersFileMutex.Unlock()
+
// Get the current users
users, err := GetUsers()
if err != nil {
"log"
"net/http"
"os"
+ "strings"
)
// Global logger
}
fmt.Printf("Logout After Days: %d\n", Settings.LogoutAfterDays)
+ if allowedHosts := os.Getenv("ALLOWED_HOSTS"); allowedHosts != "" {
+ // Split allowedHosts by comma and trim spaces
+ hosts := strings.Split(allowedHosts, ",")
+ for i, host := range hosts {
+ hosts[i] = strings.TrimSpace(host)
+ }
+ Settings.AllowedHosts = hosts
+ }
+ fmt.Printf("Allowed Hosts: %v\n", Settings.AllowedHosts)
+
if indent := os.Getenv("INDENT"); indent != "" {
// Parse indent to int
var ind int
// Reference: https://github.com/fernet/spec/blob/master/Spec.md
const (
- fernetVersion byte = 0x80
- maxClockSkew int64 = 60 // seconds
+ fernetVersion byte = 0x80
)
// FernetDecrypt decrypts a Fernet token using the given key
}
// Extract parts
- // timestamp := tokenBytes[1:9]
iv := tokenBytes[9:25]
ciphertext := tokenBytes[25 : len(tokenBytes)-32]
- // hmacValue := tokenBytes[len(tokenBytes)-32:]
// Generate encryption key from the master key
- // signingKey := key[:16] // Unused for now, will be needed if HMAC verification is enabled
encryptionKey := key[16:32]
- // Verify HMAC (optional for migration, commented out for now)
- // TODO: Uncomment if signature verification is needed
- /*
- h := hmac.New(sha256.New, signingKey)
- h.Write(tokenBytes[:len(tokenBytes)-32])
- calculatedHMAC := h.Sum(nil)
- if subtle.ConstantTimeCompare(calculatedHMAC, hmacValue) != 1 {
- return "", fmt.Errorf("invalid token signature")
- }
- */
-
// Create cipher
block, err := aes.NewCipher(encryptionKey)
if err != nil {
// Returns the derivedKey, if successfully validating password, otherwise empty string
// Return the amount of backup codes available for the user (-1 if password does not match or if backup code was NOT used).
func CheckPasswordForUser(userID int, password string) (string, int, error) {
+ UsersFileMutex.Lock()
+ defer UsersFileMutex.Unlock()
+
// Get users
users, err := GetUsers()
if err != nil {