gitea源码

smtp_auth.go 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package sender
  4. import (
  5. "errors"
  6. "fmt"
  7. "github.com/Azure/go-ntlmssp"
  8. "github.com/wneessen/go-mail/smtp"
  9. )
  10. type loginAuth struct {
  11. username, password string
  12. }
  13. // LoginAuth SMTP AUTH LOGIN Auth Handler
  14. func LoginAuth(username, password string) smtp.Auth {
  15. return &loginAuth{username, password}
  16. }
  17. // Start start SMTP login auth
  18. func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
  19. return "LOGIN", []byte{}, nil
  20. }
  21. // Next next step of SMTP login auth
  22. func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
  23. if more {
  24. switch string(fromServer) {
  25. case "Username:":
  26. return []byte(a.username), nil
  27. case "Password:":
  28. return []byte(a.password), nil
  29. default:
  30. return nil, fmt.Errorf("unknown fromServer: %s", string(fromServer))
  31. }
  32. }
  33. return nil, nil
  34. }
  35. type ntlmAuth struct {
  36. username, password, domain string
  37. domainNeeded bool
  38. }
  39. // NtlmAuth SMTP AUTH NTLM Auth Handler
  40. func NtlmAuth(username, password string) smtp.Auth {
  41. user, domain, domainNeeded := ntlmssp.GetDomain(username)
  42. return &ntlmAuth{user, password, domain, domainNeeded}
  43. }
  44. // Start starts SMTP NTLM Auth
  45. func (a *ntlmAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
  46. negotiateMessage, err := ntlmssp.NewNegotiateMessage(a.domain, "")
  47. return "NTLM", negotiateMessage, err
  48. }
  49. // Next next step of SMTP ntlm auth
  50. func (a *ntlmAuth) Next(fromServer []byte, more bool) ([]byte, error) {
  51. if more {
  52. if len(fromServer) == 0 {
  53. return nil, errors.New("ntlm ChallengeMessage is empty")
  54. }
  55. authenticateMessage, err := ntlmssp.ProcessChallenge(fromServer, a.username, a.password, a.domainNeeded)
  56. return authenticateMessage, err
  57. }
  58. return nil, nil
  59. }