| 1 |
package smtp |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/smtp" |
| 5 |
|
| 6 |
ginkgo "github.com/onsi/ginkgo/v2" |
| 7 |
gomega "github.com/onsi/gomega" |
| 8 |
) |
| 9 |
|
| 10 |
var _ = ginkgo.Describe("oauth2Auth", func() { |
| 11 |
ginkgo.Describe("newOAuth2Auth", func() { |
| 12 |
ginkgo.It("should store the username, access token, and host", func() { |
| 13 |
got := newOAuth2Auth("alice", "ya29.token", "smtp.gmail.com") |
| 14 |
auth, ok := got.(*oauth2Auth) |
| 15 |
gomega.Expect(ok).To(gomega.BeTrue()) |
| 16 |
gomega.Expect(auth.username).To(gomega.Equal("alice")) |
| 17 |
gomega.Expect(auth.accessToken).To(gomega.Equal("ya29.token")) |
| 18 |
gomega.Expect(auth.host).To(gomega.Equal("smtp.gmail.com")) |
| 19 |
}) |
| 20 |
}) |
| 21 |
|
| 22 |
ginkgo.Describe("Start", func() { |
| 23 |
ginkgo.It("should send XOAUTH2 credentials as the initial response", func() { |
| 24 |
auth := newOAuth2Auth("alice", "ya29.token", "smtp.gmail.com") |
| 25 |
mech, resp, err := auth.Start(&smtp.ServerInfo{Name: "smtp.gmail.com", TLS: true}) |
| 26 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 27 |
gomega.Expect(mech).To(gomega.Equal("XOAUTH2")) |
| 28 |
gomega.Expect(string(resp)).To(gomega.Equal("user=alice\x01auth=Bearer ya29.token\x01\x01")) |
| 29 |
}) |
| 30 |
|
| 31 |
ginkgo.It("should accept localhost without TLS", func() { |
| 32 |
auth := newOAuth2Auth("alice", "ya29.token", "localhost") |
| 33 |
_, _, err := auth.Start(&smtp.ServerInfo{Name: "localhost", TLS: false}) |
| 34 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 35 |
}) |
| 36 |
|
| 37 |
ginkgo.It("should accept ::1 without TLS", func() { |
| 38 |
auth := newOAuth2Auth("alice", "ya29.token", "::1") |
| 39 |
_, _, err := auth.Start(&smtp.ServerInfo{Name: "::1", TLS: false}) |
| 40 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 41 |
}) |
| 42 |
|
| 43 |
ginkgo.It("should reject an unencrypted remote server without leaking the token", func() { |
| 44 |
auth := newOAuth2Auth("alice", "ya29.token", "smtp.gmail.com") |
| 45 |
mech, resp, err := auth.Start(&smtp.ServerInfo{Name: "smtp.gmail.com", TLS: false}) |
| 46 |
gomega.Expect(err).To(gomega.MatchError(errUnencryptedConnection)) |
| 47 |
gomega.Expect(mech).To(gomega.BeEmpty()) |
| 48 |
gomega.Expect(resp).To(gomega.BeNil()) |
| 49 |
gomega.Expect(err.Error()).NotTo(gomega.ContainSubstring("ya29.token")) |
| 50 |
}) |
| 51 |
|
| 52 |
ginkgo.It("should reject a mismatched host without leaking the token", func() { |
| 53 |
auth := newOAuth2Auth("alice", "ya29.token", "smtp.gmail.com") |
| 54 |
_, _, err := auth.Start(&smtp.ServerInfo{Name: "evil.example.com", TLS: true}) |
| 55 |
gomega.Expect(err).To(gomega.MatchError(errWrongHostName)) |
| 56 |
gomega.Expect(err.Error()).NotTo(gomega.ContainSubstring("ya29.token")) |
| 57 |
}) |
| 58 |
}) |
| 59 |
|
| 60 |
ginkgo.Describe("Next", func() { |
| 61 |
ginkgo.It("should send an empty continuation when the server issues a 334", func() { |
| 62 |
auth := newOAuth2Auth("alice", "ya29.token", "smtp.gmail.com").(*oauth2Auth) |
| 63 |
resp, err := auth.Next([]byte(`{"status":"400"}`), true) |
| 64 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 65 |
gomega.Expect(resp).To(gomega.Equal([]byte{})) |
| 66 |
gomega.Expect(resp).NotTo(gomega.BeNil()) |
| 67 |
}) |
| 68 |
|
| 69 |
ginkgo.It("should not produce a further client response after a 235", func() { |
| 70 |
auth := newOAuth2Auth("alice", "ya29.token", "smtp.gmail.com").(*oauth2Auth) |
| 71 |
resp, err := auth.Next(nil, false) |
| 72 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 73 |
gomega.Expect(resp).To(gomega.BeNil()) |
| 74 |
}) |
| 75 |
}) |
| 76 |
}) |