aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/emersion/go-sasl/anonymous.go
blob: 8ccb817572a125f5cb0a65b0ed364b8a91ed1077 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package sasl

// The ANONYMOUS mechanism name.
const Anonymous = "ANONYMOUS"

type anonymousClient struct {
	Trace string
}

func (c *anonymousClient) Start() (mech string, ir []byte, err error) {
	mech = Anonymous
	ir = []byte(c.Trace)
	return
}

func (c *anonymousClient) Next(challenge []byte) (response []byte, err error) {
	return nil, ErrUnexpectedServerChallenge
}

// A client implementation of the ANONYMOUS authentication mechanism, as
// described in RFC 4505.
func NewAnonymousClient(trace string) Client {
	return &anonymousClient{trace}
}

// Get trace information from clients logging in anonymously.
type AnonymousAuthenticator func(trace string) error

type anonymousServer struct {
	done bool
	authenticate AnonymousAuthenticator
}

func (s *anonymousServer) Next(response []byte) (challenge []byte, done bool, err error) {
	if s.done {
		err = ErrUnexpectedClientResponse
		return
	}

	// No initial response, send an empty challenge
	if response == nil {
		return []byte{}, false, nil
	}

	s.done = true

	err = s.authenticate(string(response))
	done = true
	return
}

// A server implementation of the ANONYMOUS authentication mechanism, as
// described in RFC 4505.
func NewAnonymousServer(authenticator AnonymousAuthenticator) Server {
	return &anonymousServer{authenticate: authenticator}
}