Changeset 85 in code for trunk


Ignore:
Timestamp:
Mar 11, 2020, 6:09:32 PM (5 years ago)
Author:
contact
Message:

Add support for PASS to downstream

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/db.go

    r84 r85  
    1010type User struct {
    1111        Username string
    12         Password string
     12        Password string // hashed
    1313}
    1414
  • trunk/downstream.go

    r77 r85  
    77        "strings"
    88
     9        "golang.org/x/crypto/bcrypt"
    910        "gopkg.in/irc.v3"
    1011)
     
    1213type ircError struct {
    1314        Message *irc.Message
     15}
     16
     17func (err ircError) Error() string {
     18        return err.Message.String()
    1419}
    1520
     
    3641}
    3742
    38 func (err ircError) Error() string {
    39         return err.Message.String()
    40 }
     43var errAuthFailed = ircError{&irc.Message{
     44        Command: irc.ERR_PASSWDMISMATCH,
     45        Params:  []string{"*", "Invalid username or password"},
     46}}
    4147
    4248type consumption struct {
     
    5965        username   string
    6066        realname   string
     67        password   string   // empty after authentication
    6168        network    *network // can be nil
    6269}
     
    290297                }
    291298                dc.username = "~" + username
     299        case "PASS":
     300                if err := parseMessageParams(msg, &dc.password); err != nil {
     301                        return err
     302                }
    292303        default:
    293304                dc.logger.Printf("unhandled message: %v", msg)
     
    310321        }
    311322
     323        password := dc.password
     324        dc.password = ""
     325
    312326        u := dc.srv.getUser(username)
    313327        if u == nil {
    314                 dc.logger.Printf("failed authentication: unknown username %q", username)
    315                 dc.SendMessage(&irc.Message{
    316                         Prefix:  dc.srv.prefix(),
    317                         Command: irc.ERR_PASSWDMISMATCH,
    318                         Params:  []string{"*", "Invalid username or password"},
    319                 })
    320                 return nil
     328                dc.logger.Printf("failed authentication for %q: unknown username", username)
     329                return errAuthFailed
     330        }
     331
     332        err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
     333        if err != nil {
     334                dc.logger.Printf("failed authentication for %q: %v", username, err)
     335                return errAuthFailed
    321336        }
    322337
Note: See TracChangeset for help on using the changeset viewer.