guichet/utils.go

72 lines
1.9 KiB
Go
Raw Normal View History

2023-07-20 09:20:46 +00:00
package main
import (
2023-07-26 08:35:11 +00:00
"bytes"
2023-07-23 11:02:09 +00:00
"crypto/tls"
2023-07-23 12:07:08 +00:00
"log"
2023-07-23 11:02:09 +00:00
"net"
2023-07-26 08:43:22 +00:00
"net/smtp"
2023-07-20 09:56:11 +00:00
2023-07-20 18:02:54 +00:00
"math/rand"
2023-07-21 04:23:03 +00:00
2023-07-26 08:35:11 +00:00
"html/template"
2023-07-21 04:23:03 +00:00
"github.com/go-ldap/ldap/v3"
// "golang.org/x/text/encoding/unicode"
2023-07-20 09:20:46 +00:00
)
2023-07-23 11:02:09 +00:00
func openLdap(config *ConfigFile) (*ldap.Conn, error) {
2023-07-23 12:07:08 +00:00
var ldapConn *ldap.Conn
var err error
2023-07-23 11:02:09 +00:00
if config.LdapTLS {
tlsConf := &tls.Config{
ServerName: config.LdapServerAddr,
InsecureSkipVerify: true,
}
2023-07-23 12:07:08 +00:00
ldapConn, err = ldap.DialTLS("tcp", net.JoinHostPort(config.LdapServerAddr, "636"), tlsConf)
2023-07-20 09:56:11 +00:00
} else {
2023-07-23 12:07:08 +00:00
ldapConn, err = ldap.DialURL("ldap://" + config.LdapServerAddr)
2023-07-20 09:56:11 +00:00
}
2023-07-23 12:07:08 +00:00
if err != nil {
log.Printf("openLDAP %v", err)
log.Printf("openLDAP %v", config.LdapServerAddr)
}
return ldapConn, err
2023-07-23 11:02:09 +00:00
// l, err := ldap.DialURL(config.LdapServerAddr)
// if err != nil {
// log.Printf(fmt.Sprint("Erreur connect LDAP %v", err))
// log.Printf(fmt.Sprint("Erreur connect LDAP %v", config.LdapServerAddr))
// return nil
// } else {
// return l
// }
2023-07-20 09:20:46 +00:00
}
2023-07-20 17:55:00 +00:00
func suggestPassword() string {
2023-07-20 18:02:54 +00:00
password := ""
chars := "abcdfghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%&*+_-="
for i := 0; i < 12; i++ {
password += string([]rune(chars)[rand.Intn(len(chars))])
2023-07-20 17:55:00 +00:00
}
2023-07-20 18:02:54 +00:00
return password
2023-07-20 15:52:16 +00:00
}
2023-07-26 08:35:11 +00:00
// Sends an email according to the enclosed information
func sendMail(sendMailTplData SendMailTplData) error {
2023-07-26 08:48:07 +00:00
log.Printf("sendMail")
2023-07-26 08:35:11 +00:00
templateMail := template.Must(template.ParseFiles(templatePath + "/" + sendMailTplData.RelTemplatePath))
buf := bytes.NewBuffer([]byte{})
err := templateMail.Execute(buf, sendMailTplData)
2023-07-26 08:43:22 +00:00
message := buf.Bytes()
auth := smtp.PlainAuth("", config.SMTPUsername, config.SMTPPassword, config.SMTPServer)
log.Printf("auth: %v", auth)
err = smtp.SendMail(config.SMTPServer+":587", auth, config.SMTPUsername, []string{user.OtherMailbox}, message)
if err != nil {
log.Printf("email send error %v", err)
return err
}
log.Printf("Mail sent.")
2023-07-26 08:35:11 +00:00
return err
}