11package auth
22
33import (
4+ "context"
45 "crypto/rand"
56 "encoding/base64"
67 "encoding/json"
@@ -24,7 +25,7 @@ func ValidateUsername(username string) string {
2425 return ""
2526}
2627
27- func createUserStore (usersJsonPath string ) (* xsync.MapOf [string , string ], chan [] byte ) {
28+ func createUserStore (usersJsonPath string ) (* xsync.MapOf [string , string ], context. CancelFunc ) {
2829 // Create default users.json file
2930 _ , err := os .Stat (usersJsonPath )
3031 if os .IsNotExist (err ) {
@@ -42,13 +43,17 @@ func createUserStore(usersJsonPath string) (*xsync.MapOf[string, string], chan [
4243 }
4344
4445 users := xsync .NewMapOf [string , string ]()
45- initialFile , updates , err := system .ReadAndWatchFile (usersJsonPath )
46+ fileUpdates , cancel , err := system .ReadAndWatchFile (usersJsonPath )
4647 if err != nil {
48+ // panic here, as this is critical for authenticator and we don't want to continue without it
4749 log .Panicln ("An error occurred while reading " + usersJsonPath + "! " + err .Error ())
4850 }
4951 go (func () {
5052 for {
51- newFile := <- updates
53+ newFile , ok := <- fileUpdates
54+ if ! ok {
55+ return
56+ }
5257 var usersJson map [string ]string
5358 err = json .Unmarshal (newFile , & usersJson )
5459 if err != nil {
@@ -58,14 +63,7 @@ func createUserStore(usersJsonPath string) (*xsync.MapOf[string, string], chan [
5863 updateUserStoreFromMap (users , usersJson )
5964 }
6065 })()
61- var usersJson map [string ]string
62- err = json .Unmarshal (initialFile , & usersJson )
63- if err != nil {
64- log .Println ("An error occurred while parsing " + usersJsonPath + "! " + err .Error ())
65- return users , updates
66- }
67- updateUserStoreFromMap (users , usersJson )
68- return users , updates
66+ return users , cancel
6967}
7068
7169func updateUserStoreFromMap (users * xsync.MapOf [string , string ], userMap map [string ]string ) error {
0 commit comments