Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new email auth type: LOGIN #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package email

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

func LoginAuth(username, password, host string) smtp.Auth {
return &loginAuth{username, password, host}
}

// loginAuth is an smtp.Auth that implements the LOGIN authentication mechanism.
type loginAuth struct {
username string
password string
host string
}

func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
if !server.TLS {
advertised := false
for _, mechanism := range server.Auth {
if mechanism == "LOGIN" {
advertised = true
break
}
}
if !advertised {
return "", nil, errors.New("gomail: unencrypted connection")
}
}
if server.Name != a.host {
return "", nil, errors.New("gomail: wrong host name")
}
Comment on lines +22 to +36
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel I've seen this implementation before (perhaps when perusing gomail when I've needed to implement this myself) but it does not make sense to me. I'd expect something more like:

Suggested change
if !server.TLS {
advertised := false
for _, mechanism := range server.Auth {
if mechanism == "LOGIN" {
advertised = true
break
}
}
if !advertised {
return "", nil, errors.New("gomail: unencrypted connection")
}
}
if server.Name != a.host {
return "", nil, errors.New("gomail: wrong host name")
}
if !server.TLS {
return "", nil, errors.New("unencrypted connection")
}
if server.Name != a.Host {
return "", nil, errors.New("wrong host name")
}
advertised := false
for _, mechanism := range server.Auth {
if mechanism == "LOGIN" {
advertised = true
break
}
}
if !advertised {
return "", nil, errors.New("login auth not supported")
}

I don't see how whether it advertises LOGIN has anything to do with an unencrypted connection. In a comment in the net/smtp source it even mentions:

Note: If TLS is not true, then we can't trust ANYTHING in ServerInfo.

And they use something similar to what I propose above for the smtp.PlainAuth implementation.

Unless I'm missing something?

return "LOGIN", nil, nil
}

func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
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)
}
}
100 changes: 100 additions & 0 deletions auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package email

import (
"net/smtp"
"testing"
)

const (
testUser = "user"
testPwd = "pwd"
testHost = "smtp.example.com"
)

type authTest struct {
auths []string
challenges []string
tls bool
wantData []string
wantError bool
}

func TestNoAdvertisement(t *testing.T) {
testLoginAuth(t, &authTest{
auths: []string{},
tls: false,
wantError: true,
})
}

func TestNoAdvertisementTLS(t *testing.T) {
testLoginAuth(t, &authTest{
auths: []string{},
challenges: []string{"Username:", "Password:"},
tls: true,
wantData: []string{"", testUser, testPwd},
})
}

func TestLogin(t *testing.T) {
testLoginAuth(t, &authTest{
auths: []string{"PLAIN", "LOGIN"},
challenges: []string{"Username:", "Password:"},
tls: false,
wantData: []string{"", testUser, testPwd},
})
}

func TestLoginTLS(t *testing.T) {
testLoginAuth(t, &authTest{
auths: []string{"LOGIN"},
challenges: []string{"Username:", "Password:"},
tls: true,
wantData: []string{"", testUser, testPwd},
})
}

func testLoginAuth(t *testing.T, test *authTest) {
auth := &loginAuth{
username: testUser,
password: testPwd,
host: testHost,
}
server := &smtp.ServerInfo{
Name: testHost,
TLS: test.tls,
Auth: test.auths,
}
proto, toServer, err := auth.Start(server)
if err != nil && !test.wantError {
t.Fatalf("loginAuth.Start(): %v", err)
}
if err != nil && test.wantError {
return
}
if proto != "LOGIN" {
t.Errorf("invalid protocol, got %q, want LOGIN", proto)
}

i := 0
got := string(toServer)
if got != test.wantData[i] {
t.Errorf("Invalid response, got %q, want %q", got, test.wantData[i])
}

for _, challenge := range test.challenges {
i++
if i >= len(test.wantData) {
t.Fatalf("unexpected challenge: %q", challenge)
}

toServer, err = auth.Next([]byte(challenge), true)
if err != nil {
t.Fatalf("loginAuth.Auth(): %v", err)
}
got = string(toServer)
if got != test.wantData[i] {
t.Errorf("Invalid response, got %q, want %q", got, test.wantData[i])
}
}
}