Changeset 726 in code for trunk/downstream.go
- Timestamp:
- Nov 29, 2021, 12:14:16 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/downstream.go
r725 r726 55 55 } 56 56 57 var errAuthFailed = ircError{&irc.Message{ 58 Command: irc.ERR_PASSWDMISMATCH, 59 Params: []string{"*", "Invalid username or password"}, 60 }} 57 // authError is an authentication error. 58 type authError struct { 59 // Internal error cause. This will not be revealed to the user. 60 err error 61 // Error cause which can safely be sent to the user without compromising 62 // security. 63 reason string 64 } 65 66 func (err *authError) Error() string { 67 return err.err.Error() 68 } 69 70 func (err *authError) Unwrap() error { 71 return err.err 72 } 73 74 // authErrorReason returns the user-friendly reason of an authentication 75 // failure. 76 func authErrorReason(err error) string { 77 if authErr, ok := err.(*authError); ok { 78 return authErr.reason 79 } else { 80 return "Authentication failed" 81 } 82 } 83 84 func newInvalidUsernameOrPasswordError(err error) error { 85 return &authError{ 86 err: err, 87 reason: "Invalid username or password", 88 } 89 } 61 90 62 91 func parseBouncerNetID(subcommand, s string) (int64, error) { … … 699 728 700 729 if err := dc.authenticate(ctx, credentials.plainUsername, credentials.plainPassword); err != nil { 701 dc.logger.Printf("SASL authentication error : %v", err)730 dc.logger.Printf("SASL authentication error for user %q: %v", credentials.plainUsername, err) 702 731 dc.endSASL(&irc.Message{ 703 732 Prefix: dc.srv.prefix(), 704 733 Command: irc.ERR_SASLFAIL, 705 Params: []string{ "Authentication failed"},734 Params: []string{dc.nick, authErrorReason(err)}, 706 735 }) 707 736 break … … 1149 1178 u, err := dc.srv.db.GetUser(ctx, username) 1150 1179 if err != nil { 1151 dc.logger.Printf("failed authentication for %q: user not found: %v", username, err) 1152 return errAuthFailed 1180 return newInvalidUsernameOrPasswordError(fmt.Errorf("user not found: %w", err)) 1153 1181 } 1154 1182 1155 1183 // Password auth disabled 1156 1184 if u.Password == "" { 1157 return errAuthFailed1185 return newInvalidUsernameOrPasswordError(fmt.Errorf("password auth disabled")) 1158 1186 } 1159 1187 1160 1188 err = bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password)) 1161 1189 if err != nil { 1162 dc.logger.Printf("failed authentication for %q: wrong password: %v", username, err) 1163 return errAuthFailed 1190 return newInvalidUsernameOrPasswordError(fmt.Errorf("wrong password")) 1164 1191 } 1165 1192 1166 1193 dc.user = dc.srv.getUser(username) 1167 1194 if dc.user == nil { 1168 dc.logger.Printf("failed authentication for %q: user not active", username) 1169 return errAuthFailed 1195 return fmt.Errorf("user not active") 1170 1196 } 1171 1197 dc.clientName = clientName … … 1191 1217 if dc.user == nil { 1192 1218 if err := dc.authenticate(ctx, dc.rawUsername, password); err != nil { 1193 return err 1219 dc.logger.Printf("PASS authentication error for user %q: %v", dc.rawUsername, err) 1220 return ircError{&irc.Message{ 1221 Command: irc.ERR_PASSWDMISMATCH, 1222 Params: []string{"*", authErrorReason(err)}, 1223 }} 1194 1224 } 1195 1225 }
Note:
See TracChangeset
for help on using the changeset viewer.