Implementing Activate User

This commit is contained in:
Chris Mann 2023-07-21 10:16:51 +02:00
parent a95d8e1213
commit 76b39c0236
2 changed files with 50 additions and 43 deletions

View file

@ -22,6 +22,7 @@ import (
var EMAIL_REGEXP = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$") var EMAIL_REGEXP = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
func checkInviterLogin(w http.ResponseWriter, r *http.Request) *LoginStatus { func checkInviterLogin(w http.ResponseWriter, r *http.Request) *LoginStatus {
login := checkLogin(w, r) login := checkLogin(w, r)
if login == nil { if login == nil {
return nil return nil

View file

@ -209,54 +209,60 @@ func handleLogin(w http.ResponseWriter, r *http.Request) *LoginInfo {
username := strings.Join(r.Form["username"], "") username := strings.Join(r.Form["username"], "")
password := strings.Join(r.Form["password"], "") password := strings.Join(r.Form["password"], "")
user_dn := fmt.Sprintf("%s=%s,%s", config.UserNameAttr, username, config.UserBaseDN) user_dn := fmt.Sprintf("%s=%s,%s", config.UserNameAttr, username, config.UserBaseDN)
if strings.EqualFold(username, config.AdminAccount) { if strings.EqualFold(username, config.AdminAccount) {
user_dn = username user_dn = username
} }
return doLogin(w, r, username, user_dn, password)
l := ldapOpen(w)
if l == nil {
return nil
}
err := l.Bind(user_dn, password)
if err != nil {
data := &LoginFormData{
Username: username,
}
if ldap.IsErrorWithCode(err, ldap.LDAPResultInvalidCredentials) {
data.WrongPass = true
} else if ldap.IsErrorWithCode(err, ldap.LDAPResultNoSuchObject) {
data.WrongUser = true
} else {
data.ErrorMessage = err.Error()
}
templateLogin.Execute(w, data)
return nil
}
// Successfully logged in, save it to session
session, err := store.Get(r, SESSION_NAME)
if err != nil {
session, _ = store.New(r, SESSION_NAME)
}
session.Values["login_username"] = username
session.Values["login_password"] = password
session.Values["login_dn"] = user_dn
err = session.Save(r, w)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return nil
}
return &LoginInfo{
DN: user_dn,
Username: username,
Password: password,
}
} else { } else {
http.Error(w, "Unsupported method", http.StatusBadRequest) http.Error(w, "Unsupported method", http.StatusBadRequest)
return nil return nil
} }
} }
func doLogin(w http.ResponseWriter, r *http.Request, username string, user_dn string, password string) *LoginInfo {
l := ldapOpen(w)
if l == nil {
return nil
}
err := l.Bind(user_dn, password)
if err == nil {
templateLogin := getTemplate("login.html")
data := &LoginFormData{
Username: username,
}
if ldap.IsErrorWithCode(err, ldap.LDAPResultInvalidCredentials) {
data.WrongPass = true
} else if ldap.IsErrorWithCode(err, ldap.LDAPResultNoSuchObject) {
data.WrongUser = true
} else {
data.ErrorMessage = err.Error()
}
templateLogin.Execute(w, data)
return nil
}
// Successfully logged in, save it to session
session, err := store.Get(r, SESSION_NAME)
if err != nil {
session, _ = store.New(r, SESSION_NAME)
}
session.Values["login_username"] = username
session.Values["login_password"] = password
session.Values["login_dn"] = user_dn
err = session.Save(r, w)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return nil
}
return &LoginInfo{
DN: user_dn,
Username: username,
Password: password,
}
}