summaryrefslogtreecommitdiffstats
path: root/vendor/gopkg.in/gomail.v2/auth.go
blob: 4bcdd062027e6f914c190dbd429a1eb7fbd06d58 (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
57
58
59
60
61
62
63
64
65
66
67
package gomail

import (
	"bytes"
	"errors"
	"fmt"
	"net/smtp"
)

// plainAuth is an smtp.Auth that implements the PLAIN authentication mechanism.
// It fallbacks to the LOGIN mechanism if it is the only mechanism advertised
// by the server.
type plainAuth struct {
	username string
	password string
	host     string
	login    bool
}

func (a *plainAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
	if server.Name != a.host {
		return "", nil, errors.New("gomail: wrong host name")
	}

	var plain, login bool
	for _, a := range server.Auth {
		switch a {
		case "PLAIN":
			plain = true
		case "LOGIN":
			login = true
		}
	}

	if !server.TLS && !plain && !login {
		return "", nil, errors.New("gomail: unencrypted connection")
	}

	if !plain && login {
		a.login = true
		return "LOGIN", nil, nil
	}

	return "PLAIN", []byte("\x00" + a.username + "\x00" + a.password), nil
}

func (a *plainAuth) Next(fromServer []byte, more bool) ([]byte, error) {
	if !a.login {
		if more {
			return nil, errors.New("gomail: unexpected server challenge")
		}
		return nil, nil
	}

	if !more {
		return nil, nil
	}

	switch {
	case bytes.Equal(fromServer, []byte("Username:")):
		return []byte(a.username), nil
	case bytes.Equal(fromServer, []byte("Password:")):
		return []byte(a.password), nil
	default:
		return nil, fmt.Errorf("gomail: unexpected server challenge: %s", fromServer)
	}
}