-
Notifications
You must be signed in to change notification settings - Fork 581
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the LOGIN authentication mechanism support
Closes #16.
- Loading branch information
1 parent
d729406
commit de623e6
Showing
3 changed files
with
130 additions
and
0 deletions.
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
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,54 @@ | ||
package gomail | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/smtp" | ||
"strings" | ||
) | ||
|
||
type loginAuth struct { | ||
username string | ||
password string | ||
host string | ||
} | ||
|
||
// LoginAuth returns an Auth that implements the LOGIN authentication mechanism. | ||
func LoginAuth(username, password, host string) smtp.Auth { | ||
return &loginAuth{username, password, host} | ||
} | ||
|
||
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 | ||
} | ||
|
||
command := strings.ToLower(strings.TrimSuffix(string(fromServer), ":")) | ||
switch command { | ||
case "username": | ||
return []byte(fmt.Sprintf("%s", a.username)), nil | ||
case "password": | ||
return []byte(fmt.Sprintf("%s", a.password)), nil | ||
default: | ||
return nil, fmt.Errorf("gomail: unexpected server challenge: %s", command) | ||
} | ||
} |
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,66 @@ | ||
package gomail | ||
|
||
import ( | ||
"net/smtp" | ||
"testing" | ||
) | ||
|
||
type output struct { | ||
proto string | ||
data []string | ||
err error | ||
} | ||
|
||
const ( | ||
testUser = "user" | ||
testPwd = "pwd" | ||
) | ||
|
||
func TestPlainAuth(t *testing.T) { | ||
tests := []struct { | ||
serverProtos []string | ||
serverChallenges []string | ||
proto string | ||
data []string | ||
}{ | ||
{ | ||
serverProtos: []string{"LOGIN"}, | ||
serverChallenges: []string{"Username:", "Password:"}, | ||
proto: "LOGIN", | ||
data: []string{"", testUser, testPwd}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
auth := LoginAuth(testUser, testPwd, testHost) | ||
server := &smtp.ServerInfo{ | ||
Name: testHost, | ||
TLS: true, | ||
Auth: test.serverProtos, | ||
} | ||
proto, toServer, err := auth.Start(server) | ||
if err != nil { | ||
t.Fatalf("Start error: %v", err) | ||
} | ||
if proto != test.proto { | ||
t.Errorf("Invalid protocol, got %q, want %q", proto, test.proto) | ||
} | ||
|
||
i := 0 | ||
got := string(toServer) | ||
if got != test.data[i] { | ||
t.Errorf("Invalid response, got %q, want %q", got, test.data[i]) | ||
} | ||
for _, challenge := range test.serverChallenges { | ||
toServer, err = auth.Next([]byte(challenge), true) | ||
if err != nil { | ||
t.Fatalf("Auth error: %v", err) | ||
} | ||
i++ | ||
got = string(toServer) | ||
if got != test.data[i] { | ||
t.Errorf("Invalid response, got %q, want %q", got, test.data[i]) | ||
} | ||
} | ||
} | ||
} |
de623e6
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.
Please approve this asap, thanks!!! Thanks :)
de623e6
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.
Done.