diff --git a/src/main/main.go b/src/main/main.go index 59cee5f..3c51f45 100644 --- a/src/main/main.go +++ b/src/main/main.go @@ -5,7 +5,6 @@ import ( "html/template" "log" "net/http" - "net/url" "strings" "sync" @@ -259,22 +258,6 @@ func main() { Email: serverConfig.EmailConfig.Email, }, serverConfig.EmailConfig.SMTPURL, serverConfig.EmailConfig.SMTPPort) - funcs := template.FuncMap{ - "avatar": func(username string) string { - return serverConfig.WebserverConfig.BaseURL + "/avatar?user=" + url.QueryEscape(username) - }, - } - - data := map[string]any{ - "Username": "gawells", - } - - email_template, err := email.RenderTemplate("./data/email-templates/expired-password.html", data, funcs) - if err != nil { - logging.Errorf("Failed to load email template: %s", err.Error()) - } - noReplyEmail.SendEmail([]string{"gawells@astraltech.xyz"}, "Test", email_template) - ldapServer = ldap.LDAPServer{ URL: serverConfig.LDAPConfig.LDAPURL, StartTLS: serverConfig.LDAPConfig.Security == "tls", @@ -294,6 +277,8 @@ func main() { logging.Fatal("Failed to connect to LDAP server") } + InitPasswordExpiry() + helpers.HandleFunc("/favicon.ico", faviconHandler) helpers.HandleFunc("/logo", logoHandler) diff --git a/src/main/password_expiry.go b/src/main/password_expiry.go new file mode 100644 index 0000000..cd897ce --- /dev/null +++ b/src/main/password_expiry.go @@ -0,0 +1,53 @@ +package main + +import ( + "fmt" + "time" + + "astraltech.xyz/accountmanager/src/email" + "astraltech.xyz/accountmanager/src/logging" + "astraltech.xyz/accountmanager/src/worker" +) + +func InitPasswordExpiry() { + go func() { + CheckPasswordExpriy() + }() + worker.CreateWorker(time.Hour*12, CheckPasswordExpriy) +} + +func CheckPasswordExpriy() { + now := time.Now().UTC() + formatted := now.Format("20060102150405Z") + + search, err := ldapServer.SerchServer(serverConfig.LDAPConfig.BindDN, serverConfig.LDAPConfig.BindPassword, serverConfig.LDAPConfig.BaseDN, fmt.Sprintf("(&(objectclass=person)(krbPasswordExpiration<=%s))", formatted), []string{"uid", "cn", "mail", "krbPasswordExpiration"}) + if err != nil { + logging.Warn(err.Error()) + } + + for i := range search.EntryCount() { + emailAddr := search.GetEntry(i).GetAttributeValue("mail") + if len(emailAddr) <= 0 { + continue + } + + t, err := time.Parse("20060102150405Z", search.GetEntry(i).GetAttributeValue("krbPasswordExpiration")) + if err != nil { + panic(err) + } + formatted := t.Format("January 2, 2006 at 3:04 PM MST") + + data := map[string]any{ + "Username": search.GetEntry(i).GetAttributeValue("cn"), + "ExpiredAt": formatted, + "ResetURL": "https://example.com/reset?token=abc123", + "ServiceName": "Astral Tech", + } + + email_template, err := email.RenderTemplate("./data/email-templates/expired-password.html", data, nil) + if err != nil { + logging.Errorf("Failed to load email template: %s", err.Error()) + } + noReplyEmail.SendEmail([]string{emailAddr}, "Password expired", email_template) + } +}