reset password page
This commit is contained in:
@@ -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: ""})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
-48
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
@@ -34,6 +35,8 @@ var (
|
|||||||
userDataMutex sync.RWMutex
|
userDataMutex sync.RWMutex
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ErrPasswordExpired = errors.New("Password expired")
|
||||||
|
|
||||||
func authenticateUser(username, password string) (*UserData, error) {
|
func authenticateUser(username, password string) (*UserData, error) {
|
||||||
logging.Event(logging.AuthenticateUser, username)
|
logging.Event(logging.AuthenticateUser, username)
|
||||||
userDN := fmt.Sprintf("uid=%s,cn=users,cn=accounts,%s", username, serverConfig.LDAPConfig.BaseDN)
|
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)
|
connected, err := ldapServer.AuthenticateUser(userDN, password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if strings.Contains(err.Error(), "Password is expired") {
|
if strings.Contains(err.Error(), "Password is expired") {
|
||||||
return nil, fmt.Errorf("Password expired for %s\n", username)
|
return nil, ErrPasswordExpired
|
||||||
}
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -75,53 +78,6 @@ func authenticateUser(username, password string) (*UserData, error) {
|
|||||||
return &user, nil
|
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 {
|
type ProfileData struct {
|
||||||
Username string
|
Username string
|
||||||
Email string
|
Email string
|
||||||
@@ -288,6 +244,7 @@ func main() {
|
|||||||
helpers.HandleFunc("/login", loginHandler)
|
helpers.HandleFunc("/login", loginHandler)
|
||||||
helpers.HandleFunc("/profile", profileHandler)
|
helpers.HandleFunc("/profile", profileHandler)
|
||||||
helpers.HandleFunc("/logout", logoutHandler)
|
helpers.HandleFunc("/logout", logoutHandler)
|
||||||
|
helpers.HandleFunc("/reset-password", resetPasswordHandler)
|
||||||
|
|
||||||
helpers.HandleFunc("/avatar", components.AvatarHandler)
|
helpers.HandleFunc("/avatar", components.AvatarHandler)
|
||||||
helpers.HandleFunc("/change-photo", components.UploadPhotoHandler)
|
helpers.HandleFunc("/change-photo", components.UploadPhotoHandler)
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<title>Astral Tech - Reset Password</title>
|
||||||
|
|
||||||
|
<link
|
||||||
|
href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap"
|
||||||
|
rel="stylesheet"
|
||||||
|
/>
|
||||||
|
<link rel="stylesheet" href="static/style.css" />
|
||||||
|
<link rel="stylesheet" href="static/error/error.css" />
|
||||||
|
<link rel="stylesheet" href="static/card.css" />
|
||||||
Reference in New Issue
Block a user