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,14 +120,25 @@ type tlsProceed struct {
} }
func authenticate(stream *Stream, mechanisms []string, user, password string) error { func authenticate(stream *Stream, mechanisms []string, user, password string) error {
for _, handler := range authHandlers {
log.Println("authenticate, mechanisms=", mechanisms) if !stringSliceContains(mechanisms, handler.Mechanism) {
continue
if !stringSliceContains(mechanisms, "PLAIN") { }
return errors.New("Only PLAIN supported for now") if err := handler.Fn(stream, user, password); err == nil {
log.Printf("Authentication (%s) successful", handler.Mechanism)
return nil
}
}
return errors.New("No supported SASL mechanism found.")
} }
return authenticatePlain(stream, user, password) 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 { func authenticatePlain(stream *Stream, user, password string) error {