Fixed Change Password Bug

This commit is contained in:
Chris Mann 2023-07-25 14:13:14 +02:00
parent 9ce93ddf8f
commit 0b74e04603
3 changed files with 14 additions and 174 deletions

View file

@ -1,5 +1,5 @@
BIN=guichet
SRC=main.go ssha.go profile.go admin.go invite.go directory.go utils.go picture.go login.go config.go http-utils.go home.go model-user.go gpas.go
SRC=main.go ssha.go profile.go admin.go invite.go directory.go utils.go picture.go login.go config.go http-utils.go home.go model-user.go gpas.go session.go
DOCKER=lxpz/guichet_amd64
all: $(BIN)

12
gpas.go
View file

@ -55,12 +55,12 @@ func passwordLost(user User, config *ConfigFile, ldapConn *ldap.Conn) error {
log.Printf("Il n'y a pas d'utilisateur qui correspond %v", searchReq)
return errors.New("Il n'y a pas d'utilisateur qui correspond")
}
log.Printf(fmt.Sprintf("passwordLost 58 : %v", user))
log.Printf(fmt.Sprintf("passwordLost 59 : %v", searchRes.Entries[0]))
log.Printf(fmt.Sprintf("passwordLost 60 : %v", searchRes.Entries[0].GetAttributeValue("cn")))
log.Printf(fmt.Sprintf("passwordLost 61 : %v", searchRes.Entries[0].GetAttributeValue("uid")))
log.Printf(fmt.Sprintf("passwordLost 62 : %v", searchRes.Entries[0].GetAttributeValue("mail")))
log.Printf(fmt.Sprintf("passwordLost 63 : %v", searchRes.Entries[0].GetAttributeValue("carLicense")))
// log.Printf(fmt.Sprintf("passwordLost 58 : %v", user))
// log.Printf(fmt.Sprintf("passwordLost 59 : %v", searchRes.Entries[0]))
// log.Printf(fmt.Sprintf("passwordLost 60 : %v", searchRes.Entries[0].GetAttributeValue("cn")))
// log.Printf(fmt.Sprintf("passwordLost 61 : %v", searchRes.Entries[0].GetAttributeValue("uid")))
// log.Printf(fmt.Sprintf("passwordLost 62 : %v", searchRes.Entries[0].GetAttributeValue("mail")))
// log.Printf(fmt.Sprintf("passwordLost 63 : %v", searchRes.Entries[0].GetAttributeValue("carLicense")))
// Préparation du courriel à envoyer
user.Password = suggestPassword()
code := b64.URLEncoding.EncodeToString([]byte(user.UID + ";" + user.Password))

174
login.go
View file

@ -38,175 +38,9 @@ func (login *LoginStatus) WelcomeName() string {
return ret
}
func checkLogin(w http.ResponseWriter, r *http.Request) *LoginStatus {
var login_info *LoginInfo
session, err := store.Get(r, SESSION_NAME)
if err == nil {
username, ok := session.Values["login_username"]
password, ok2 := session.Values["login_password"]
user_dn, ok3 := session.Values["login_dn"]
if ok && ok2 && ok3 {
login_info = &LoginInfo{
DN: user_dn.(string),
Username: username.(string),
Password: password.(string),
}
}
}
if login_info == nil {
login_info = handleLogin(w, r)
if login_info == nil {
return nil
}
}
l, err := ldapOpen(w)
if l == nil {
return nil
}
err = bind(User{
DN: login_info.DN,
Password: login_info.Password,
}, config, l)
if err != nil {
delete(session.Values, "login_username")
delete(session.Values, "login_password")
delete(session.Values, "login_dn")
err = session.Save(r, w)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return nil
}
return checkLogin(w, r)
}
ldapUser, err := get(User{
DN: login_info.DN,
CN: login_info.Username,
}, config, l)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return nil
}
userEntry := ldapUser.UserEntry
loginStatus := &LoginStatus{
Info: login_info,
conn: l,
UserEntry: userEntry,
CanAdmin: ldapUser.CanAdmin,
CanInvite: ldapUser.CanInvite,
}
/*
requestKind := "(objectClass=organizationalPerson)"
if strings.EqualFold(login_info.DN, config.AdminAccount) {
requestKind = "(objectclass=*)"
}
searchRequest := ldap.NewSearchRequest(
login_info.DN,
ldap.ScopeBaseObject, ldap.NeverDerefAliases, 0, 0, false,
requestKind,
[]string{
"dn",
"displayname",
"givenname",
"sn",
"mail",
"cn",
"memberof",
"description",
"garage_s3_access_key",
},
nil)
// FIELD_NAME_DIRECTORY_VISIBILITY,
// FIELD_NAME_PROFILE_PICTURE,
sr, err := l.Search(searchRequest)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return nil
}
if len(sr.Entries) != 1 {
http.Error(w, fmt.Sprintf("Unable to find entry for %s", login_info.DN), http.StatusInternalServerError)
return nil
}
loginStatus.UserEntry = sr.Entries[0]
loginStatus.CanAdmin = strings.EqualFold(loginStatus.Info.DN, config.AdminAccount)
loginStatus.CanInvite = false
groups := []EntryName{}
searchRequest = ldap.NewSearchRequest(
config.GroupBaseDN,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
fmt.Sprintf("(&(objectClass=groupOfNames)(member=%s))", login_info.DN),
[]string{"dn", "displayName", "cn", "description"},
nil)
// // log.Printf(fmt.Sprintf("708: %v",searchRequest))
sr, err = l.Search(searchRequest)
// if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
//// log.Printf(fmt.Sprintf("303: %v",sr.Entries))
for _, ent := range sr.Entries {
// log.Printf(fmt.Sprintf("305: %v",ent.DN))
groups = append(groups, EntryName{
DN: ent.DN,
Name: ent.GetAttributeValue("cn"),
})
// log.Printf(fmt.Sprintf("310: %v",config.GroupCanInvite))
if config.GroupCanInvite != "" && strings.EqualFold(ent.DN, config.GroupCanInvite) {
loginStatus.CanInvite = true
}
// log.Printf(fmt.Sprintf("314: %v",config.GroupCanAdmin))
if config.GroupCanAdmin != "" && strings.EqualFold(ent.DN, config.GroupCanAdmin) {
loginStatus.CanAdmin = true
}
}
// for _, attr := range loginStatus.UserEntry.Attributes {
// if strings.EqualFold(attr.Name, "memberof") {
// for _, group := range attr.Values {
// if config.GroupCanInvite != "" && strings.EqualFold(group, config.GroupCanInvite) {
// loginStatus.CanInvite = true
// }
// if config.GroupCanAdmin != "" && strings.EqualFold(group, config.GroupCanAdmin) {
// loginStatus.CanAdmin = true
// }
// }
// }
// }
return loginStatus
*/
return loginStatus
}
func handleLogout(w http.ResponseWriter, r *http.Request) {
session, err := store.Get(r, SESSION_NAME)
if err != nil {
session, _ = store.New(r, SESSION_NAME)
}
delete(session.Values, "login_username")
delete(session.Values, "login_password")
delete(session.Values, "login_dn")
err = session.Save(r, w)
err := logout(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@ -227,6 +61,12 @@ type LoginFormData struct {
func handleLogin(w http.ResponseWriter, r *http.Request) *LoginInfo {
templateLogin := getTemplate("login.html")
loginStatus := checkLogin(w, r)
if loginStatus != nil {
handleHome(w, r)
return loginStatus.Info
}
if r.Method == "GET" {
templateLogin.Execute(w, LoginFormData{CanAdmin: false})
return nil