| 1 |
package smtp |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/smtp" |
| 5 |
) |
| 6 |
|
| 7 |
// newAuth returns the SMTP authentication mechanism for config. |
| 8 |
// |
| 9 |
// Auth [AuthNone] returns a nil mechanism and a nil error so the session |
| 10 |
// continues without AUTH. |
| 11 |
// |
| 12 |
// Parameters: |
| 13 |
// - config: SMTP configuration providing Auth, Username, Password, and Host. |
| 14 |
// |
| 15 |
// Returns: |
| 16 |
// - An [smtp.Auth] implementation for the selected method, or nil when Auth is [AuthNone]. |
| 17 |
// - An error if Auth is [AuthUnknown] or otherwise unsupported. |
| 18 |
// |
| 19 |
//nolint:exhaustive // false positive: switch covers all AuthTypes, linter confuses local authType with net/smtp.authType |
| 20 |
func newAuth(config *Config) (smtp.Auth, error) { |
| 21 |
switch config.Auth { |
| 22 |
case AuthTypes.None: |
| 23 |
return nil, nil //nolint:nilnil // AuthNone means skip AUTH; a nil mechanism is the success path. |
| 24 |
case AuthTypes.Plain: |
| 25 |
return smtp.PlainAuth("", config.Username, config.Password, config.Host), nil |
| 26 |
case AuthTypes.CRAMMD5: |
| 27 |
return smtp.CRAMMD5Auth(config.Username, config.Password), nil |
| 28 |
case AuthTypes.OAuth2: |
| 29 |
return newOAuth2Auth(config.Username, config.Password, config.Host), nil |
| 30 |
case AuthTypes.Login: |
| 31 |
return newLoginAuth(config.Username, config.Password, config.Host), nil |
| 32 |
case AuthTypes.Unknown: |
| 33 |
return nil, fail(FailAuthType, nil, config.Auth.String()) |
| 34 |
default: |
| 35 |
return nil, fail(FailAuthType, nil, config.Auth.String()) |
| 36 |
} |
| 37 |
} |