Skip to content

Commit 8be6da3

Browse files
YT315yangtan_winsilverwind
authoredMay 2, 2023
Add ntlm authentication support for mail (#23811)
Add ntlm authentication support for mail use "github.com/Azure/go-ntlmssp" --------- Co-authored-by: yangtan_win <YangTan@Fitsco.com.cn> Co-authored-by: silverwind <me@silverwind.io>
1 parent bcdd3c3 commit 8be6da3

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed
 

‎go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
gitea.com/lunny/dingtalk_webhook v0.0.0-20171025031554-e3534c89ef96
1515
gitea.com/lunny/levelqueue v0.4.2-0.20220729054728-f020868cc2f7
1616
github.com/42wim/sshsig v0.0.0-20211121163825-841cf5bbc121
17+
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358
1718
github.com/NYTimes/gziphandler v1.1.1
1819
github.com/PuerkitoBio/goquery v1.8.0
1920
github.com/alecthomas/chroma/v2 v2.5.0
@@ -127,7 +128,6 @@ require (
127128
cloud.google.com/go/compute v1.18.0 // indirect
128129
cloud.google.com/go/compute/metadata v0.2.3 // indirect
129130
git.sr.ht/~mariusor/go-xsd-duration v0.0.0-20220703122237-02e73435a078 // indirect
130-
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 // indirect
131131
github.com/Masterminds/goutils v1.1.1 // indirect
132132
github.com/Masterminds/semver/v3 v3.2.0 // indirect
133133
github.com/Masterminds/sprig/v3 v3.2.3 // indirect

‎services/mailer/mailer.go

+32
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"code.gitea.io/gitea/modules/setting"
2727
"code.gitea.io/gitea/modules/templates"
2828

29+
ntlmssp "github.com/Azure/go-ntlmssp"
2930
"github.com/jaytaylor/html2text"
3031
"gopkg.in/gomail.v2"
3132
)
@@ -145,6 +146,35 @@ func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
145146
return nil, nil
146147
}
147148

149+
type ntlmAuth struct {
150+
username, password, domain string
151+
domainNeeded bool
152+
}
153+
154+
// NtlmAuth SMTP AUTH NTLM Auth Handler
155+
func NtlmAuth(username, password string) smtp.Auth {
156+
user, domain, domainNeeded := ntlmssp.GetDomain(username)
157+
return &ntlmAuth{user, password, domain, domainNeeded}
158+
}
159+
160+
// Start starts SMTP NTLM Auth
161+
func (a *ntlmAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
162+
negotiateMessage, err := ntlmssp.NewNegotiateMessage(a.domain, "")
163+
return "NTLM", negotiateMessage, err
164+
}
165+
166+
// Next next step of SMTP ntlm auth
167+
func (a *ntlmAuth) Next(fromServer []byte, more bool) ([]byte, error) {
168+
if more {
169+
if len(fromServer) == 0 {
170+
return nil, fmt.Errorf("ntlm ChallengeMessage is empty")
171+
}
172+
authenticateMessage, err := ntlmssp.ProcessChallenge(fromServer, a.username, a.password, a.domainNeeded)
173+
return authenticateMessage, err
174+
}
175+
return nil, nil
176+
}
177+
148178
// Sender SMTP mail sender
149179
type smtpSender struct{}
150180

@@ -237,6 +267,8 @@ func (s *smtpSender) Send(from string, to []string, msg io.WriterTo) error {
237267
} else if strings.Contains(options, "LOGIN") {
238268
// Patch for AUTH LOGIN
239269
auth = LoginAuth(opts.User, opts.Passwd)
270+
} else if strings.Contains(options, "NTLM") {
271+
auth = NtlmAuth(opts.User, opts.Passwd)
240272
}
241273

242274
if auth != nil {

0 commit comments

Comments
 (0)
Please sign in to comment.