diff --git a/src/main/login.go b/src/main/login.go new file mode 100644 index 0000000..8e4afa5 --- /dev/null +++ b/src/main/login.go @@ -0,0 +1,58 @@ +package main + +import ( + "html/template" + "net/http" + "strings" + + "astraltech.xyz/accountmanager/src/logging" +) + +type LoginPageData struct { + IsHiddenClassList string +} + +func loginHandler(w http.ResponseWriter, r *http.Request) { + logging.Info("Handing login page") + + w.Header().Set("Content-Type", "text/html; charset=utf-8") + tmpl := template.Must(template.ParseFiles("src/pages/login_page.html")) + if r.Method == http.MethodGet { + logging.Info("Rending login page") + tmpl.Execute(w, LoginPageData{IsHiddenClassList: "hidden"}) + return + } + + if r.Method == http.MethodPost { + username := r.FormValue("username") + if strings.Contains(username, "/") { + tmpl.Execute(w, LoginPageData{IsHiddenClassList: ""}) + } + password := r.FormValue("password") + + logging.Infof("New Login request for %s\n", username) + newUserData, err := authenticateUser(username, password) + userDataMutex.Lock() + userData[username] = newUserData + userDataMutex.Unlock() + if err == ErrPasswordExpired { + http.Redirect(w, r, "/reset-password?token=this_is_the_only_token_that_works", http.StatusFound) + } else if err != nil { + logging.Error(err.Error()) + tmpl.Execute(w, LoginPageData{IsHiddenClassList: ""}) + } else { + if newUserData.isAuth == true { + cookie, err := sessionManager.CreateSession(username) + if err != nil { + logging.Error(err.Error()) + http.Error(w, "Session error", http.StatusInternalServerError) + return + } + http.SetCookie(w, cookie) + http.Redirect(w, r, "/profile", http.StatusFound) + } else { + tmpl.Execute(w, LoginPageData{IsHiddenClassList: ""}) + } + } + } +} diff --git a/src/main/main.go b/src/main/main.go index 933b825..280e7b9 100644 --- a/src/main/main.go +++ b/src/main/main.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "html/template" "log" @@ -34,6 +35,8 @@ var ( userDataMutex sync.RWMutex ) +var ErrPasswordExpired = errors.New("Password expired") + func authenticateUser(username, password string) (*UserData, error) { logging.Event(logging.AuthenticateUser, username) userDN := fmt.Sprintf("uid=%s,cn=users,cn=accounts,%s", username, serverConfig.LDAPConfig.BaseDN) @@ -41,7 +44,7 @@ func authenticateUser(username, password string) (*UserData, error) { connected, err := ldapServer.AuthenticateUser(userDN, password) if err != nil { if strings.Contains(err.Error(), "Password is expired") { - return nil, fmt.Errorf("Password expired for %s\n", username) + return nil, ErrPasswordExpired } return nil, err } @@ -75,53 +78,6 @@ func authenticateUser(username, password string) (*UserData, error) { return &user, nil } -type LoginPageData struct { - IsHiddenClassList string -} - -func loginHandler(w http.ResponseWriter, r *http.Request) { - logging.Info("Handing login page") - - w.Header().Set("Content-Type", "text/html; charset=utf-8") - tmpl := template.Must(template.ParseFiles("src/pages/login_page.html")) - if r.Method == http.MethodGet { - logging.Info("Rending login page") - tmpl.Execute(w, LoginPageData{IsHiddenClassList: "hidden"}) - return - } - - if r.Method == http.MethodPost { - username := r.FormValue("username") - if strings.Contains(username, "/") { - tmpl.Execute(w, LoginPageData{IsHiddenClassList: ""}) - } - password := r.FormValue("password") - - logging.Infof("New Login request for %s\n", username) - newUserData, err := authenticateUser(username, password) - userDataMutex.Lock() - userData[username] = newUserData - userDataMutex.Unlock() - if err != nil { - logging.Error(err.Error()) - tmpl.Execute(w, LoginPageData{IsHiddenClassList: ""}) - } else { - if newUserData.isAuth == true { - cookie, err := sessionManager.CreateSession(username) - if err != nil { - logging.Error(err.Error()) - http.Error(w, "Session error", http.StatusInternalServerError) - return - } - http.SetCookie(w, cookie) - http.Redirect(w, r, "/profile", http.StatusFound) - } else { - tmpl.Execute(w, LoginPageData{IsHiddenClassList: ""}) - } - } - } -} - type ProfileData struct { Username string Email string @@ -288,6 +244,7 @@ func main() { helpers.HandleFunc("/login", loginHandler) helpers.HandleFunc("/profile", profileHandler) helpers.HandleFunc("/logout", logoutHandler) + helpers.HandleFunc("/reset-password", resetPasswordHandler) helpers.HandleFunc("/avatar", components.AvatarHandler) helpers.HandleFunc("/change-photo", components.UploadPhotoHandler) diff --git a/src/main/reset_password.go b/src/main/reset_password.go new file mode 100644 index 0000000..3c1caf9 --- /dev/null +++ b/src/main/reset_password.go @@ -0,0 +1,21 @@ +package main + +import ( + "html/template" + "net/http" + + "astraltech.xyz/accountmanager/src/logging" +) + +func resetPasswordHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/html; charset=utf-8") + + token := r.URL.Query().Get("token") + logging.Infof("Token: %s\n", token) + + tmpl := template.Must(template.ParseFiles("src/pages/reset_password.html")) + if r.Method == http.MethodGet { + tmpl.Execute(w, nil) + return + } +} diff --git a/src/pages/reset_password.html b/src/pages/reset_password.html new file mode 100644 index 0000000..09dadd6 --- /dev/null +++ b/src/pages/reset_password.html @@ -0,0 +1,10 @@ + +Astral Tech - Reset Password + + + + +