mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-22 13:50:49 +01:00
Refactor error handling in SMTP authentication
Centralized error definitions in `smtp/auth.go` and updated references in `auth_login.go` and `auth_plain.go`. This improves code maintainability and error consistency across the package.
This commit is contained in:
parent
a41639ec07
commit
580981b158
3 changed files with 18 additions and 13 deletions
13
smtp/auth.go
13
smtp/auth.go
|
@ -13,6 +13,19 @@
|
||||||
|
|
||||||
package smtp
|
package smtp
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrUnencrypted is an error indicating that the connection is not encrypted.
|
||||||
|
ErrUnencrypted = errors.New("unencrypted connection")
|
||||||
|
// ErrUnexpectedServerChallange is an error indicating that the server issued an unexpected challenge.
|
||||||
|
ErrUnexpectedServerChallange = errors.New("unexpected server challenge")
|
||||||
|
// ErrUnexpectedServerResponse is an error indicating that the server issued an unexpected response.
|
||||||
|
ErrUnexpectedServerResponse = errors.New("unexpected server response")
|
||||||
|
// ErrWrongHostname is an error indicating that the provided hostname does not match the expected value.
|
||||||
|
ErrWrongHostname = errors.New("wrong host name")
|
||||||
|
)
|
||||||
|
|
||||||
// Auth is implemented by an SMTP authentication mechanism.
|
// Auth is implemented by an SMTP authentication mechanism.
|
||||||
type Auth interface {
|
type Auth interface {
|
||||||
// Start begins an authentication with a server.
|
// Start begins an authentication with a server.
|
||||||
|
|
|
@ -5,13 +5,9 @@
|
||||||
package smtp
|
package smtp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrUnencrypted is an error indicating that the connection is not encrypted.
|
|
||||||
var ErrUnencrypted = errors.New("unencrypted connection")
|
|
||||||
|
|
||||||
// loginAuth is the type that satisfies the Auth interface for the "SMTP LOGIN" auth
|
// loginAuth is the type that satisfies the Auth interface for the "SMTP LOGIN" auth
|
||||||
type loginAuth struct {
|
type loginAuth struct {
|
||||||
username, password string
|
username, password string
|
||||||
|
@ -55,7 +51,7 @@ func (a *loginAuth) Start(server *ServerInfo) (string, []byte, error) {
|
||||||
return "", nil, ErrUnencrypted
|
return "", nil, ErrUnencrypted
|
||||||
}
|
}
|
||||||
if server.Name != a.host {
|
if server.Name != a.host {
|
||||||
return "", nil, errors.New("wrong host name")
|
return "", nil, ErrWrongHostname
|
||||||
}
|
}
|
||||||
a.respStep = 0
|
a.respStep = 0
|
||||||
return "LOGIN", nil, nil
|
return "LOGIN", nil, nil
|
||||||
|
@ -73,7 +69,7 @@ func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
|
||||||
a.respStep++
|
a.respStep++
|
||||||
return []byte(a.password), nil
|
return []byte(a.password), nil
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unexpected server response: %s", string(fromServer))
|
return nil, fmt.Errorf("%w: %s", ErrUnexpectedServerResponse, string(fromServer))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
|
|
@ -13,10 +13,6 @@
|
||||||
|
|
||||||
package smtp
|
package smtp
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
// plainAuth is the type that satisfies the Auth interface for the "SMTP PLAIN" auth
|
// plainAuth is the type that satisfies the Auth interface for the "SMTP PLAIN" auth
|
||||||
type plainAuth struct {
|
type plainAuth struct {
|
||||||
identity, username, password string
|
identity, username, password string
|
||||||
|
@ -42,10 +38,10 @@ func (a *plainAuth) Start(server *ServerInfo) (string, []byte, error) {
|
||||||
// That might just be the attacker saying
|
// That might just be the attacker saying
|
||||||
// "it's ok, you can trust me with your password."
|
// "it's ok, you can trust me with your password."
|
||||||
if !server.TLS && !isLocalhost(server.Name) {
|
if !server.TLS && !isLocalhost(server.Name) {
|
||||||
return "", nil, errors.New("unencrypted connection")
|
return "", nil, ErrUnencrypted
|
||||||
}
|
}
|
||||||
if server.Name != a.host {
|
if server.Name != a.host {
|
||||||
return "", nil, errors.New("wrong host name")
|
return "", nil, ErrWrongHostname
|
||||||
}
|
}
|
||||||
resp := []byte(a.identity + "\x00" + a.username + "\x00" + a.password)
|
resp := []byte(a.identity + "\x00" + a.username + "\x00" + a.password)
|
||||||
return "PLAIN", resp, nil
|
return "PLAIN", resp, nil
|
||||||
|
@ -54,7 +50,7 @@ func (a *plainAuth) Start(server *ServerInfo) (string, []byte, error) {
|
||||||
func (a *plainAuth) Next(_ []byte, more bool) ([]byte, error) {
|
func (a *plainAuth) Next(_ []byte, more bool) ([]byte, error) {
|
||||||
if more {
|
if more {
|
||||||
// We've already sent everything.
|
// We've already sent everything.
|
||||||
return nil, errors.New("unexpected server challenge")
|
return nil, ErrUnexpectedServerChallange
|
||||||
}
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue