Skip to content

Commit

Permalink
server: unify logic to decode SASL response
Browse files Browse the repository at this point in the history
  • Loading branch information
emersion committed Apr 25, 2024
1 parent 170fe35 commit e74d8b3
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,15 +773,11 @@ func (c *Conn) handleAuth(arg string) {
// Parse client initial response if there is one
var ir []byte
if len(parts) > 1 {
if parts[1] == "=" {
ir = []byte{}
} else {
var err error
ir, err = base64.StdEncoding.DecodeString(parts[1])
if err != nil {
c.writeResponse(454, EnhancedCode{4, 7, 0}, "Invalid base64 data")
return
}
var err error
ir, err = decodeSASLResponse(parts[1])
if err != nil {
c.writeResponse(454, EnhancedCode{4, 7, 0}, "Invalid base64 data")
return
}
}

Expand Down Expand Up @@ -820,21 +816,24 @@ func (c *Conn) handleAuth(arg string) {
return
}

if encoded == "=" {
response = []byte{}
} else {
response, err = base64.StdEncoding.DecodeString(encoded)
if err != nil {
c.writeResponse(454, EnhancedCode{4, 7, 0}, "Invalid base64 data")
return
}
response, err = decodeSASLResponse(encoded)
if err != nil {
c.writeResponse(454, EnhancedCode{4, 7, 0}, "Invalid base64 data")
return
}
}

c.writeResponse(235, EnhancedCode{2, 0, 0}, "Authentication succeeded")
c.didAuth = true
}

func decodeSASLResponse(s string) ([]byte, error) {
if s == "=" {
return []byte{}, nil
}
return base64.StdEncoding.DecodeString(s)
}

func (c *Conn) authMechanisms() []string {
if authSession, ok := c.Session().(AuthSession); ok {
return authSession.AuthMechanisms()
Expand Down

0 comments on commit e74d8b3

Please sign in to comment.