maddy

git clone git://git.lin.moe/fmaddy/maddy.git

 1package sasllogin
 2
 3import "github.com/emersion/go-sasl"
 4
 5// Copy-pasted from old emersion/go-sasl version
 6
 7// Authenticates users with an username and a password.
 8type LoginAuthenticator func(username, password string) error
 9type loginState int
10
11const (
12	loginNotStarted loginState = iota
13	loginWaitingUsername
14	loginWaitingPassword
15)
16
17type loginServer struct {
18	state              loginState
19	username, password string
20	authenticate       LoginAuthenticator
21}
22
23// A server implementation of the LOGIN authentication mechanism, as described
24// in https://tools.ietf.org/html/draft-murchison-sasl-login-00.
25//
26// LOGIN is obsolete and should only be enabled for legacy clients that cannot
27// be updated to use PLAIN.
28func NewLoginServer(authenticator LoginAuthenticator) sasl.Server {
29	return &loginServer{authenticate: authenticator}
30}
31
32func (a *loginServer) Next(response []byte) (challenge []byte, done bool, err error) {
33	switch a.state {
34	case loginNotStarted:
35		// Check for initial response field, as per RFC4422 section 3
36		if response == nil {
37			challenge = []byte("Username:")
38			break
39		}
40		a.state++
41		fallthrough
42	case loginWaitingUsername:
43		a.username = string(response)
44		challenge = []byte("Password:")
45	case loginWaitingPassword:
46		a.password = string(response)
47		err = a.authenticate(a.username, a.password)
48		done = true
49	default:
50		err = sasl.ErrUnexpectedClientResponse
51	}
52	a.state++
53	return
54}