Refactoring

This commit is contained in:
Chris Mann 2023-07-26 13:03:45 +02:00
parent 666b34150b
commit 7f3383944b
4 changed files with 50 additions and 50 deletions

View file

@ -64,7 +64,7 @@ func passwordLost(user User, config *ConfigFile, ldapConn *ldap.Conn) error {
// Préparation du courriel à envoyer
user.Password = suggestPassword()
code := b64.URLEncoding.EncodeToString([]byte(user.UID + ";" + user.Password))
user.DN = "uid=" + searchRes.Entries[0].GetAttributeValue("cn") + ",ou=invitations,dc=resdigita,dc=org"
user.DN = "uid=" + searchRes.Entries[0].GetAttributeValue("cn") + "," + config.InvitationBaseDN
user.UID = searchRes.Entries[0].GetAttributeValue("cn")
user.CN = searchRes.Entries[0].GetAttributeValue("cn")
user.Mail = searchRes.Entries[0].GetAttributeValue("mail")
@ -81,11 +81,11 @@ func passwordLost(user User, config *ConfigFile, ldapConn *ldap.Conn) error {
if len(searchRes.Entries) == 0 {
/* Add the invitation */
addReq := ldap.NewAddRequest(
user.DN,
"uid="+searchRes.Entries[0].GetAttributeValue("cn")+","+config.InvitationBaseDN,
nil)
addReq.Attribute("objectClass", []string{"top", "account", "simpleSecurityObject"})
addReq.Attribute("uid", []string{user.UID})
addReq.Attribute("userPassword", []string{"absdefghi"})
addReq.Attribute("uid", []string{searchRes.Entries[0].GetAttributeValue("cn")})
addReq.Attribute("userPassword", []string{suggestPassword()})
addReq.Attribute("seeAlso", []string{config.UserNameAttr + "=" + user.UID + "," + config.UserBaseDN})
err = ldapConn.Add(addReq)
if err != nil {

View file

@ -43,6 +43,7 @@ func openLdap(config *ConfigFile) (*ldap.Conn, error) {
// }
}
// Suggesting a 12 char password with some excentrics
func suggestPassword() string {
password := ""
chars := "abcdfghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%&*+_-="

View file

@ -56,52 +56,6 @@ func openNewUserLdap(config *ConfigFile) (*ldap.Conn, error) {
return l, err
}
func handleLostPassword(w http.ResponseWriter, r *http.Request) {
templateLostPasswordPage := getTemplate("passwd/lost.html")
if checkLogin(w, r) != nil {
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
}
data := PasswordLostData{
Common: NestedCommonTplData{
CanAdmin: false,
LoggedIn: false},
}
if r.Method == "POST" {
r.ParseForm()
data.Username = strings.TrimSpace(strings.Join(r.Form["username"], ""))
data.Mail = strings.TrimSpace(strings.Join(r.Form["mail"], ""))
data.OtherMailbox = strings.TrimSpace(strings.Join(r.Form["othermailbox"], ""))
user := User{
CN: strings.TrimSpace(strings.Join(r.Form["username"], "")),
UID: strings.TrimSpace(strings.Join(r.Form["username"], "")),
Mail: strings.TrimSpace(strings.Join(r.Form["mail"], "")),
OtherMailbox: strings.TrimSpace(strings.Join(r.Form["othermailbox"], "")),
}
ldapConn, err := openNewUserLdap(config)
if err != nil {
log.Printf(fmt.Sprintf("handleLostPassword 99 : %v %v", err, ldapConn))
data.Common.ErrorMessage = err.Error()
}
err = passwordLost(user, config, ldapConn)
if err != nil {
log.Printf(fmt.Sprintf("handleLostPassword 104 : %v %v", err, ldapConn))
data.Common.ErrorMessage = err.Error()
} else {
err = ldapConn.Bind(config.NewUserDN, config.NewUserPassword)
if err != nil {
log.Printf(fmt.Sprintf("handleLostPassword 109 : %v %v", err, ldapConn))
data.Common.ErrorMessage = err.Error()
} else {
data.Common.Success = true
}
}
}
data.Common.CanAdmin = false
templateLostPasswordPage.Execute(w, data)
}
func handleInviteNewAccount(w http.ResponseWriter, r *http.Request) {
l, err := ldapOpen(w)
if err != nil {

View file

@ -10,6 +10,51 @@ import (
"github.com/gorilla/mux"
)
func handleLostPassword(w http.ResponseWriter, r *http.Request) {
templateLostPasswordPage := getTemplate("passwd/lost.html")
if checkLogin(w, r) != nil {
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
}
data := PasswordLostData{
Common: NestedCommonTplData{
CanAdmin: false,
LoggedIn: false},
}
if r.Method == "POST" {
r.ParseForm()
data.Username = strings.TrimSpace(strings.Join(r.Form["username"], ""))
data.Mail = strings.TrimSpace(strings.Join(r.Form["mail"], ""))
data.OtherMailbox = strings.TrimSpace(strings.Join(r.Form["othermailbox"], ""))
user := User{
CN: strings.TrimSpace(strings.Join(r.Form["username"], "")),
UID: strings.TrimSpace(strings.Join(r.Form["username"], "")),
Mail: strings.TrimSpace(strings.Join(r.Form["mail"], "")),
OtherMailbox: strings.TrimSpace(strings.Join(r.Form["othermailbox"], "")),
}
ldapConn, err := openNewUserLdap(config)
if err != nil {
log.Printf(fmt.Sprintf("handleLostPassword 99 : %v %v", err, ldapConn))
data.Common.ErrorMessage = err.Error()
}
if err != nil {
log.Printf(fmt.Sprintf("handleLostPassword 104 : %v %v", err, ldapConn))
data.Common.ErrorMessage = err.Error()
} else {
err = ldapConn.Bind(config.NewUserDN, config.NewUserPassword)
if err != nil {
log.Printf(fmt.Sprintf("handleLostPassword 109 : %v %v", err, ldapConn))
data.Common.ErrorMessage = err.Error()
} else {
data.Common.Success = true
}
}
}
data.Common.CanAdmin = false
templateLostPasswordPage.Execute(w, data)
}
func handleFoundPassword(w http.ResponseWriter, r *http.Request) {
templateFoundPasswordPage := getTemplate("passwd.html")
data := PasswdTplData{