refactor: separate SMTP config from LDAP struct
The LDAP struct was mixing LDAP connection settings with unrelated mail settings. Extract mail fields into a dedicated SMTPConfig struct with its own global (mySMTP), keeping concerns cleanly separated. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
4a68d0700d
commit
3e6b95bf40
3 changed files with 30 additions and 19 deletions
13
ldap.go
13
ldap.go
|
|
@ -21,11 +21,14 @@ type LDAP struct {
|
|||
BaseDN string
|
||||
ServiceDN string
|
||||
ServicePassword string
|
||||
MailHost string
|
||||
MailPort int
|
||||
MailUser string
|
||||
MailPassword string
|
||||
MailFrom string
|
||||
}
|
||||
|
||||
type SMTPConfig struct {
|
||||
MailHost string
|
||||
MailPort int
|
||||
MailUser string
|
||||
MailPassword string
|
||||
MailFrom string
|
||||
}
|
||||
|
||||
func (l LDAP) Connect() (*LDAPConn, error) {
|
||||
|
|
|
|||
6
lost.go
6
lost.go
|
|
@ -167,14 +167,14 @@ func lostPassword(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
// Send the email
|
||||
m := gomail.NewMessage()
|
||||
m.SetHeader("From", myLDAP.MailFrom)
|
||||
m.SetHeader("From", mySMTP.MailFrom)
|
||||
m.SetHeader("To", email)
|
||||
m.SetHeader("Subject", "SSO nemunai.re: password recovery")
|
||||
m.SetBody("text/plain", "Hello "+cn+"!\n\nSomeone, and we hope it's you, requested to reset your account password. \nIn order to continue, go to:\n"+myPublicURL+"/reset?l="+r.PostFormValue("login")+"&t="+token+"\n\nThis link expires in 1 hour and can only be used once.\n\nBest regards,\n-- \nnemunai.re SSO")
|
||||
|
||||
var s gomail.Sender
|
||||
if myLDAP.MailHost != "" {
|
||||
d := gomail.NewDialer(myLDAP.MailHost, myLDAP.MailPort, myLDAP.MailUser, myLDAP.MailPassword)
|
||||
if mySMTP.MailHost != "" {
|
||||
d := gomail.NewDialer(mySMTP.MailHost, mySMTP.MailPort, mySMTP.MailUser, mySMTP.MailPassword)
|
||||
s, err = d.Dial()
|
||||
if err != nil {
|
||||
log.Println("Unable to connect to email server: " + err.Error())
|
||||
|
|
|
|||
30
main.go
30
main.go
|
|
@ -31,9 +31,12 @@ var dockerRegistrySecret string
|
|||
var allowedAliasDomains []string
|
||||
|
||||
var myLDAP = LDAP{
|
||||
Host: "localhost",
|
||||
Port: 389,
|
||||
BaseDN: "dc=example,dc=com",
|
||||
Host: "localhost",
|
||||
Port: 389,
|
||||
BaseDN: "dc=example,dc=com",
|
||||
}
|
||||
|
||||
var mySMTP = SMTPConfig{
|
||||
MailPort: 587,
|
||||
MailFrom: "noreply@example.com",
|
||||
}
|
||||
|
|
@ -115,8 +118,13 @@ func main() {
|
|||
log.Fatal(err)
|
||||
} else if cnt, err := io.ReadAll(fd); err != nil {
|
||||
log.Fatal(err)
|
||||
} else if err := json.Unmarshal(cnt, &myLDAP); err != nil {
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
if err := json.Unmarshal(cnt, &myLDAP); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := json.Unmarshal(cnt, &mySMTP); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -156,17 +164,17 @@ func main() {
|
|||
}
|
||||
|
||||
if val, ok := os.LookupEnv("SMTP_HOST"); ok {
|
||||
myLDAP.MailHost = val
|
||||
mySMTP.MailHost = val
|
||||
}
|
||||
if val, ok := os.LookupEnv("SMTP_PORT"); ok {
|
||||
if port, err := strconv.Atoi(val); err == nil {
|
||||
myLDAP.MailPort = port
|
||||
mySMTP.MailPort = port
|
||||
} else {
|
||||
log.Println("Invalid value for SMTP_PORT:", val)
|
||||
}
|
||||
}
|
||||
if val, ok := os.LookupEnv("SMTP_USER"); ok {
|
||||
myLDAP.MailUser = val
|
||||
mySMTP.MailUser = val
|
||||
}
|
||||
if val, ok := os.LookupEnv("SMTP_PASSWORD_FILE"); ok {
|
||||
if fd, err := os.Open(val); err != nil {
|
||||
|
|
@ -176,13 +184,13 @@ func main() {
|
|||
log.Fatal(err)
|
||||
} else {
|
||||
fd.Close()
|
||||
myLDAP.MailPassword = string(cnt)
|
||||
mySMTP.MailPassword = string(cnt)
|
||||
}
|
||||
} else if val, ok := os.LookupEnv("SMTP_PASSWORD"); ok {
|
||||
myLDAP.MailPassword = val
|
||||
mySMTP.MailPassword = val
|
||||
}
|
||||
if val, ok := os.LookupEnv("SMTP_FROM"); ok {
|
||||
myLDAP.MailFrom = val
|
||||
mySMTP.MailFrom = val
|
||||
}
|
||||
if val, ok := os.LookupEnv("PUBLIC_URL"); ok {
|
||||
myPublicURL = val
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue