-
Notifications
You must be signed in to change notification settings - Fork 323
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
exfly
wants to merge
1
commit into
jordan-wright:master
Choose a base branch
from
exfly:fix/smtp_no_auth_login
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
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:
And they use something similar to what I propose above for the smtp.PlainAuth implementation.
Unless I'm missing something?