@@ -26,6 +26,28 @@ func ValidateUsername(username string) string {
2626}
2727
2828func createUserStore (usersJsonPath string ) (* xsync.MapOf [string , string ], context.CancelFunc ) {
29+ userUpdates , cancel := readAndWatchUsers (usersJsonPath )
30+ users := xsync .NewMapOf [string , string ]()
31+ go (func () {
32+ for {
33+ newUsers , ok := <- userUpdates
34+ if ! ok {
35+ return
36+ }
37+ users .Clear () // Clear all pre-existing users
38+ for username , password := range newUsers {
39+ if msg := ValidateUsername (username ); msg == "" {
40+ users .Store (username , password )
41+ } else {
42+ log .Println (msg + " This account will be ignored and eventually removed!" )
43+ }
44+ }
45+ }
46+ })()
47+ return users , cancel
48+ }
49+
50+ func readAndWatchUsers (usersJsonPath string ) (<- chan map [string ]string , context.CancelFunc ) {
2951 // Create default users.json file
3052 _ , err := os .Stat (usersJsonPath )
3153 if os .IsNotExist (err ) {
@@ -42,13 +64,13 @@ func createUserStore(usersJsonPath string) (*xsync.MapOf[string, string], contex
4264 }
4365 }
4466
45- users := xsync .NewMapOf [string , string ]()
4667 fileUpdates , cancel , err := system .ReadAndWatchFile (usersJsonPath )
4768 if err != nil {
4869 // skipcq RVV-A0003
4970 // panic here, as this is critical for authenticator and we don't want to continue without it
5071 log .Panicln ("An error occurred while reading " + usersJsonPath + "! " + err .Error ())
5172 }
73+ userChannel := make (chan map [string ]string , 1 )
5274 go (func () {
5375 for {
5476 newFile , ok := <- fileUpdates
@@ -61,20 +83,8 @@ func createUserStore(usersJsonPath string) (*xsync.MapOf[string, string], contex
6183 log .Println ("An error occurred while parsing " + usersJsonPath + "! " + err .Error ())
6284 continue
6385 }
64- updateUserStoreFromMap ( users , usersJson )
86+ userChannel <- usersJson
6587 }
6688 })()
67- return users , cancel
68- }
69-
70- func updateUserStoreFromMap (users * xsync.MapOf [string , string ], userMap map [string ]string ) error {
71- users .Clear () // Clear all pre-existing users
72- for username , password := range userMap {
73- if msg := ValidateUsername (username ); msg == "" {
74- users .Store (username , password )
75- } else {
76- log .Println (msg + " This account will be ignored and eventually removed!" )
77- }
78- }
79- return nil
89+ return userChannel , cancel
8090}
0 commit comments