guichet/view-login.go

129 lines
3.1 KiB
Go
Raw Normal View History

2023-07-26 06:31:43 +00:00
/*
login handles login and current-user verification
*/
package main
import (
"fmt"
"log"
"net/http"
"strings"
"github.com/go-ldap/ldap/v3"
)
func (login *LoginStatus) WelcomeName() string {
ret := login.UserEntry.GetAttributeValue("givenName")
if ret == "" {
ret = login.UserEntry.GetAttributeValue("displayName")
}
if ret == "" {
ret = login.Info.Username
}
return ret
}
func handleLogout(w http.ResponseWriter, r *http.Request) {
err := logout(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/", http.StatusFound)
}
func handleLogin(w http.ResponseWriter, r *http.Request) *LoginInfo {
templateLogin := getTemplate("login.html")
if r.Method == "POST" {
// log.Printf("%v", "Parsing Form handleLogin")
r.ParseForm()
username := strings.Join(r.Form["username"], "")
password := strings.Join(r.Form["password"], "")
user_dn := fmt.Sprintf("%s=%s,%s", config.UserNameAttr, username, config.UserBaseDN)
// log.Printf("%v", user_dn)
// log.Printf("%v", username)
if strings.EqualFold(username, config.AdminAccount) {
user_dn = username
}
loginInfo, err := doLogin(w, r, username, user_dn, password)
// log.Printf("%v", loginInfo)
if err != nil {
data := &LoginFormData{
Username: username,
Common: NestedCommonTplData{
CanAdmin: false,
CanInvite: true,
LoggedIn: false,
},
}
if ldap.IsErrorWithCode(err, ldap.LDAPResultInvalidCredentials) {
data.WrongPass = true
} else if ldap.IsErrorWithCode(err, ldap.LDAPResultNoSuchObject) {
data.WrongUser = true
} else {
log.Printf("%v", err)
log.Printf("%v", user_dn)
log.Printf("%v", username)
data.Common.ErrorMessage = err.Error()
}
templateLogin.Execute(w, data)
}
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return loginInfo
} else if r.Method == "GET" {
templateLogin.Execute(w, LoginFormData{
Common: NestedCommonTplData{
CanAdmin: false,
CanInvite: true,
LoggedIn: false}})
return nil
} else {
http.Error(w, "Unsupported method", http.StatusBadRequest)
return nil
}
}
func doLogin(w http.ResponseWriter, r *http.Request, username string, user_dn string, password string) (*LoginInfo, error) {
l, _ := ldapOpen(w)
err := l.Bind(user_dn, password)
if err != nil {
log.Printf("doLogin : %v", err)
log.Printf("doLogin : %v", user_dn)
return nil, err
}
// 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 {
log.Printf("doLogin Session Save: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return nil, err
}
LoginInfo := LoginInfo{
DN: user_dn,
Username: username,
Password: password,
}
return &LoginInfo, nil
}