1
0
Fork 0

Make authentication a little more pluggable.

This commit is contained in:
Matt Goodall 2012-07-10 12:27:29 +01:00
parent 5966340a3a
commit 32df77187c
1 changed files with 18 additions and 7 deletions

View File

@ -120,16 +120,27 @@ type tlsProceed struct {
}
func authenticate(stream *Stream, mechanisms []string, user, password string) error {
log.Println("authenticate, mechanisms=", mechanisms)
if !stringSliceContains(mechanisms, "PLAIN") {
return errors.New("Only PLAIN supported for now")
for _, handler := range authHandlers {
if !stringSliceContains(mechanisms, handler.Mechanism) {
continue
}
if err := handler.Fn(stream, user, password); err == nil {
log.Printf("Authentication (%s) successful", handler.Mechanism)
return nil
}
}
return authenticatePlain(stream, user, password)
return errors.New("No supported SASL mechanism found.")
}
type authHandler struct {
Mechanism string
Fn func(*Stream, string, string) error
}
var authHandlers = []authHandler{
authHandler{"PLAIN", authenticatePlain},
}
func authenticatePlain(stream *Stream, user, password string) error {
auth := saslAuth{Mechanism: "PLAIN", Message: saslEncodePlain(user, password)}