Skip to content

Commit e74d8b3

Browse files
committed
server: unify logic to decode SASL response
1 parent 170fe35 commit e74d8b3

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

conn.go

+16-17
Original file line numberDiff line numberDiff line change
@@ -773,15 +773,11 @@ func (c *Conn) handleAuth(arg string) {
773773
// Parse client initial response if there is one
774774
var ir []byte
775775
if len(parts) > 1 {
776-
if parts[1] == "=" {
777-
ir = []byte{}
778-
} else {
779-
var err error
780-
ir, err = base64.StdEncoding.DecodeString(parts[1])
781-
if err != nil {
782-
c.writeResponse(454, EnhancedCode{4, 7, 0}, "Invalid base64 data")
783-
return
784-
}
776+
var err error
777+
ir, err = decodeSASLResponse(parts[1])
778+
if err != nil {
779+
c.writeResponse(454, EnhancedCode{4, 7, 0}, "Invalid base64 data")
780+
return
785781
}
786782
}
787783

@@ -820,21 +816,24 @@ func (c *Conn) handleAuth(arg string) {
820816
return
821817
}
822818

823-
if encoded == "=" {
824-
response = []byte{}
825-
} else {
826-
response, err = base64.StdEncoding.DecodeString(encoded)
827-
if err != nil {
828-
c.writeResponse(454, EnhancedCode{4, 7, 0}, "Invalid base64 data")
829-
return
830-
}
819+
response, err = decodeSASLResponse(encoded)
820+
if err != nil {
821+
c.writeResponse(454, EnhancedCode{4, 7, 0}, "Invalid base64 data")
822+
return
831823
}
832824
}
833825

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

830+
func decodeSASLResponse(s string) ([]byte, error) {
831+
if s == "=" {
832+
return []byte{}, nil
833+
}
834+
return base64.StdEncoding.DecodeString(s)
835+
}
836+
838837
func (c *Conn) authMechanisms() []string {
839838
if authSession, ok := c.Session().(AuthSession); ok {
840839
return authSession.AuthMechanisms()

0 commit comments

Comments
 (0)