rawlinp commented on a change in pull request #4077: updated SMTP to use Login 
Auth
URL: https://github.com/apache/trafficcontrol/pull/4077#discussion_r343328352
 
 

 ##########
 File path: traffic_ops/traffic_ops_golang/api/api.go
 ##########
 @@ -747,3 +750,44 @@ func AddUserToReq(r *http.Request, u auth.CurrentUser) {
        ctx = context.WithValue(ctx, auth.CurrentUserKey, u)
        *r = *r.WithContext(ctx)
 }
+
+type loginAuth struct {
+       identity, username, password, host string
+}
+
+func LoginAuth(identity, username, password, host string) smtp.Auth {
+       return &loginAuth{identity, username, password, host}
+}
+
+func isLocalhost(name string) bool {
+       return name == "localhost" || name == "127.0.0.1" || name == "::1"
+}
+
+func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
+       if !server.TLS && !isLocalhost(server.Name) {
+               return "", nil, errors.New("unencrypted connection")
+       }
+       if server.Name != a.host {
+               return "", nil, errors.New("wrong host name")
+       }
+       resp := []byte(a.identity + "\x00" + a.username + "\x00" + a.password)
+       return "LOGIN", resp, nil
+}
+
+func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
+       command := string(fromServer)
+       command = strings.TrimSpace(command)
+       command = strings.TrimSuffix(command, ":")
+       command = strings.ToLower(command)
+
+       if more {
+               if command == "username" {
+                       return []byte(fmt.Sprintf("%s", a.username)), nil
 
 Review comment:
   Since `a.username` and `a.password` are just strings, but is the 
`fmt.Sprintf()` necessary? Can it just be like `[]byte(a.username)` for both?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to