Skip to content

Commit f5b3be9

Browse files
refactor: auth model create
1 parent 926ed16 commit f5b3be9

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

config/config.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,15 @@ func Parse(raw Raw) (Config, error) {
130130
MinAge: 5 * time.Minute,
131131
}
132132

133-
authSMTP := models.Auth{
134-
Username: raw.SMTP.Username,
135-
Password: raw.SMTP.Password,
136-
}
133+
authSMTP := models.NewAuth(
134+
raw.SMTP.Username,
135+
raw.SMTP.Password,
136+
)
137137

138-
authHTTP := models.Auth{
139-
Username: raw.HTTP.Username,
140-
Password: raw.HTTP.Password,
141-
}
138+
authHTTP := models.NewAuth(
139+
raw.HTTP.Username,
140+
raw.HTTP.Password,
141+
)
142142

143143
return Config{
144144
DatabasePath: databasePath,

internal/models/auth.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
package models
22

33
type Auth struct {
4-
Username string
5-
Password string
4+
Anonymous bool
5+
Username string
6+
Password string
7+
}
8+
9+
func NewAuth(username, password string) Auth {
10+
anonymous := false
11+
if username == "" && password == "" {
12+
anonymous = true
13+
}
14+
15+
return Auth{
16+
Anonymous: anonymous,
17+
Username: username,
18+
Password: password,
19+
}
620
}

internal/procs/auth.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
var ErrorLogin = fmt.Errorf("login invalid")
1111

1212
func AuthHTTPAnonymous(cc core.Context) bool {
13-
return cc.Config.AuthHTTP.Username == "" && cc.Config.AuthHTTP.Password == ""
13+
return cc.Config.AuthHTTP.Anonymous
1414
}
1515

1616
func AuthHTTPLogin(cc core.Context, username, password string) error {
@@ -29,7 +29,7 @@ func AuthHTTPLogin(cc core.Context, username, password string) error {
2929
return nil
3030
}
3131

32-
func SMTPLogin(cc core.Context, username, password string) error {
32+
func AuthSMTPLogin(cc core.Context, username, password string) error {
3333
if cc.Config.AuthSMTP.Username == "" && cc.Config.AuthSMTP.Password == "" {
3434
return nil
3535
}

smtp/backend.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func newSession(ctx core.Context, remoteAddr net.Addr) *session {
4040
}
4141

4242
func (s *session) AuthPlain(username, password string) error {
43-
return procs.SMTPLogin(s.cc, username, password)
43+
return procs.AuthSMTPLogin(s.cc, username, password)
4444
}
4545

4646
func (s *session) Mail(from string, opts *smtp.MailOptions) error {

0 commit comments

Comments
 (0)