@@ -21,25 +21,25 @@ func activatedSystemAuthentication() (err error) {
2121 defaults ["authentication.xml" ] = false
2222 defaults ["authentication.api" ] = false
2323 err = authentication .SetDefaultUserData (defaults )
24-
24+ // Propagate error from SetDefaultUserData
2525 return
2626}
2727
2828func createFirstUserForAuthentication (username , password string ) (token string , err error ) {
29- var authenticationErr = func (err error ) {
30- if err != nil {
31- return
32- }
33- }
34-
3529 err = authentication .CreateDefaultUser (username , password )
36- authenticationErr (err )
30+ if err != nil {
31+ return "" , err
32+ }
3733
3834 token , err = authentication .UserAuthentication (username , password )
39- authenticationErr (err )
35+ if err != nil {
36+ return "" , err
37+ }
4038
4139 token , err = authentication .CheckTheValidityOfTheToken (token )
42- authenticationErr (err )
40+ if err != nil {
41+ return "" , err
42+ }
4343
4444 var userData = make (map [string ]any )
4545 userData ["username" ] = username
@@ -51,10 +51,14 @@ func createFirstUserForAuthentication(username, password string) (token string,
5151 userData ["defaultUser" ] = true
5252
5353 userID , err := authentication .GetUserID (token )
54- authenticationErr (err )
54+ if err != nil {
55+ return "" , err
56+ }
5557
5658 err = authentication .WriteUserData (userID , userData )
57- authenticationErr (err )
59+ if err != nil {
60+ return "" , err
61+ }
5862
5963 return
6064}
@@ -78,9 +82,19 @@ func basicAuth(r *http.Request, level string) (username string, err error) {
7882 return
7983 }
8084
81- payload , _ := base64 .StdEncoding .DecodeString (auth [1 ])
85+ payload , errDecode := base64 .StdEncoding .DecodeString (auth [1 ])
86+ if errDecode != nil {
87+ // If decoding fails, it's an invalid Authorization header.
88+ // The original err (user authentication failed) is appropriate.
89+ return "" , err // Return the original error
90+ }
8291 pair := strings .SplitN (string (payload ), ":" , 2 )
8392
93+ if len (pair ) != 2 {
94+ // If not two parts, it's an invalid format.
95+ return "" , err // Return the original error
96+ }
97+
8498 username = pair [0 ]
8599 var password = pair [1 ]
86100
@@ -145,13 +159,22 @@ func checkAuthorizationLevel(token, level string) (err error) {
145159 err = errors .New ("no authorization" )
146160 }
147161 } else {
162+ // Level not found, set to false and try to save.
163+ // The user does not have authorization regardless of save success.
148164 userData [level ] = false
149- authentication .WriteUserData (userID , userData )
150- //err = errors.New("No authorization")
165+ if writeErr := authentication .WriteUserData (userID , userData ); writeErr != nil {
166+ // Log the error, but the primary error (no authorization) stands.
167+ // log.Printf("Failed to write default authorization level for user %s, level %s: %v", userID, level, writeErr)
168+ }
169+ err = errors .New ("no authorization" ) // Ensure error is set if level was not found
151170 }
152171 } else {
153- authentication .WriteUserData (userID , userData )
154- //err = errors.New("No authorization")
172+ // UserData is empty, this is an unusual case.
173+ // Attempt to write, but the user definitely doesn't have authorization.
174+ if writeErr := authentication .WriteUserData (userID , userData ); writeErr != nil {
175+ // log.Printf("Failed to write empty userData for user %s, level %s: %v", userID, level, writeErr)
176+ }
177+ err = errors .New ("no authorization" ) // Ensure error is set if userData was empty
155178 }
156179
157180 return
0 commit comments