better routes, standardize error messages part 1

This commit is contained in:
stryan 2020-11-12 14:33:01 -05:00
parent 98e1d7e774
commit 0c347eda8e
12 changed files with 74 additions and 91 deletions

12
main.go
View File

@ -33,12 +33,12 @@ func main() {
router.HandleFunc("/minecraft/link", minecraftLink).Methods("POST")
router.HandleFunc("/minecraft/link/success", minecraftLinkSuccessPage).Methods("GET")
router.HandleFunc("/minecraft/link/error", minecraftLinkErrorPage).Methods("GET")
router.HandleFunc("/passwordreset", resetPageFront).Methods("GET")
router.HandleFunc("/passwordreset", resetLookup).Methods("POST")
router.HandleFunc("/passwordresetform", resetPageBack).Methods("GET")
router.HandleFunc("/passwordresetform", reset).Methods("POST")
router.HandleFunc("/resetsuccess", resetSuccessPage).Methods("GET")
router.HandleFunc("/reseterror", resetErrorPage).Methods("GET")
router.HandleFunc("/reset", resetPageFront).Methods("GET")
router.HandleFunc("/reset", resetLookup).Methods("POST")
router.HandleFunc("/reset/form", resetPageBack).Methods("GET")
router.HandleFunc("/reset/form", reset).Methods("POST")
router.HandleFunc("/reset/success", resetSuccessPage).Methods("GET")
router.HandleFunc("/reset/error", resetErrorPage).Methods("GET")
log.Printf("Registering templates from %v/\n", Conf.TplPath)
tpl = template.Must(template.ParseGlob(Conf.TplPath + "/*"))
log.Println("Performing LDAP checks")

View File

@ -10,12 +10,12 @@ import (
)
func resetLookup(res http.ResponseWriter, req *http.Request) {
log.Println("POST /passwordreset")
log.Println("POST /reset")
email := req.FormValue("email")
uname, err := findLDAPAccountByEmail(email)
if err != nil {
log.Printf("Error while looking up account to email password reset to: %v\n. Account may not exist", err)
http.Redirect(res, req, "/passwordresetform", 303)
http.Redirect(res, req, "/reset/form", 303)
}
if uname == "" {
log.Printf("Error while looking up account to email password reset to: %v\n", err)
@ -38,7 +38,7 @@ func resetLookup(res http.ResponseWriter, req *http.Request) {
}
}()*/
log.Println("Redirecting to next part of password reset")
http.Redirect(res, req, "/passwordresetform", 303)
http.Redirect(res, req, "/reset/form", 303)
}
func reset(res http.ResponseWriter, req *http.Request) {
token := req.FormValue("token")
@ -47,7 +47,7 @@ func reset(res http.ResponseWriter, req *http.Request) {
user, err := validateToken(token)
if err != nil {
log.Printf("Error validing password reset token: %v\n", err)
http.Redirect(res, req, "/reseterror", 302)
http.Redirect(res, req, "/reset/error", 302)
return
}
if user == "" {
@ -59,11 +59,11 @@ func reset(res http.ResponseWriter, req *http.Request) {
err = resetLDAPAccountPassword(user, newPass)
if err == nil {
log.Printf("reset password for %v\n", user)
http.Redirect(res, req, "/resetsuccess", 302)
http.Redirect(res, req, "/reset/success", 302)
return
} else {
log.Printf("failed to reset password for %v:%v\n", user, err)
http.Redirect(res, req, "/reseterror", 302)
http.Redirect(res, req, "/reset/error", 302)
return
}

View File

@ -50,7 +50,8 @@ func signup(res http.ResponseWriter, req *http.Request) {
_, err := validateToken(secret)
if err != nil {
log.Printf("Bad secret entered: %v\n", err)
res.Write([]byte("Get a load of this guy, not knowing the secret code"))
genericErrorPage(res, "User Creation Failure", "Unregistered", false, "Invalid Secret Token.", "to create account")
return
}
}
@ -58,10 +59,10 @@ func signup(res http.ResponseWriter, req *http.Request) {
log.Printf("Attempting to create account for %v", username)
err := createLDAPAccount(username, password, email)
if err == nil {
res.Write([]byte("User created!"))
genericSuccessPage(res, "User Created", "Unregistered", false, "User created")
return
} else {
res.Write([]byte("Failure to create account"))
genericErrorPage(res, "User Creation Failure", "Unregistered", false, err.Error(), "to create account")
return
}
}
@ -73,7 +74,7 @@ func login(res http.ResponseWriter, req *http.Request) {
err := loginLDAPAccount(username, password)
if err != nil {
log.Printf("Error logging in user %v: %v\n", username, err)
res.Write([]byte("Error logging in. Incorrect password?"))
genericErrorPage(res, "Login Failure", "Unregistered", false, err.Error(), "to login")
return
} else {
setSession(username, res)

View File

@ -1,5 +1,8 @@
{{ define "error" }}
{{ define "generic_error" }}
{{ template "header" .}}
An error occured. Please let the admin know.
<p>Unable to {{.Action}} due to the following error:</p>
<p>{{.Error}}</p>
<p>Please try again or let the admin know.</p>
<p><a href="/">Return to homepage</a></p>
{{template "footer" .}}
{{ end }}

View File

@ -6,7 +6,7 @@
<p><a href="/minecraft">Minecraft Account Status</a></p>
{{else}}
<p><a href="/register">Register</a></p>
<p><a href="/passwordreset">Reset Password</a></p>
<p><a href="/reset">Reset Password</a></p>
{{end}}
{{template "footer" .}}
{{ end }}

View File

@ -1,8 +0,0 @@
{{ define "minecraft_error" }}
{{ template "header" .}}
<p>Unable to link your Minecraft account due to the following error:</p>
<p>{{.Error}}</p>
<p>Please try again or let the admin know.</p>
<p><a href="/">Return to homepage</a></p>
{{template "footer" .}}
{{ end }}

View File

@ -1,6 +0,0 @@
{{ define "minecraft_success" }}
{{ template "header" .}}
<p>Your Minecraft account has been linked</p>
<p><a href="/">Return to homepage</a></p>
{{template "footer" .}}
{{ end }}

View File

@ -1,8 +0,0 @@
{{ define "reset_error" }}
{{ template "header" .}}
<p>Unable to reset your password due to the following error:</p>
<p>{{.Error}}</p>
<p>Please try again or let the admin know.</p>
<p><a href="/">Return to homepage</a></p>
{{template "footer" .}}
{{ end }}

View File

@ -3,7 +3,7 @@
<h1> Password Recovery </h1>
<p> You should receive a Password Recovery token momentarily in your email inbox</p>
<p> Please enter it, and your new password, into the form below to reset your password</p>
<form action="/passwordresetform" method="POST" novalidate>
<form action="/reset/form" method="POST" novalidate>
<div>
<label>Password Token:</label>
<input type="text" name="token">

View File

@ -2,7 +2,7 @@
{{ template "header" .}}
<h1> Lookup By Email </h1>
<p> Please enter your email address to reset your password </p>
<form action="/passwordreset" method="POST" novalidate>
<form action="/reset" method="POST" novalidate>
<div>
<label>Email Address:</label>
<input type="text" name="email">

View File

@ -1,6 +1,6 @@
{{ define "reset_success" }}
{{ define "generic_success" }}
{{ template "header" .}}
<p>Your password has been succesfully reset!</p>
<p>{{.Action}}</p>
<p><a href="/">Return to homepage</a></p>
{{template "footer" .}}
{{ end }}

93
web.go
View File

@ -86,36 +86,24 @@ func minecraftPage(res http.ResponseWriter, req *http.Request) {
}
func minecraftLinkSuccessPage(res http.ResponseWriter, req *http.Request) {
log.Println("GET /minecraft/link/success")
data := struct {
Title string
Username string
LoggedIn bool
}{
"Minecraft Link Success",
"",
true,
u := getUserName(req)
if u == "" {
http.Redirect(res, req, "/404", 302)
}
tpl.ExecuteTemplate(res, "minecraft_success", data)
genericSuccessPage(res, "Minecraft Link Success", u, true, "Succesfully linked Minecraft account.")
return
}
func minecraftLinkErrorPage(res http.ResponseWriter, req *http.Request) {
log.Println("GET /minecraft/link/error")
data := struct {
Title string
Username string
LoggedIn bool
Error string
}{
"Minecraft Link Failure",
"",
true,
"Undefined",
u := getUserName(req)
if u == "" {
http.Redirect(res, req, "/404", 302)
}
tpl.ExecuteTemplate(res, "minecraft_error", data)
genericErrorPage(res, "Minecraft Link Failure", u, true, "Undefined", "link Minecraft account.")
return
}
func resetPageFront(res http.ResponseWriter, req *http.Request) {
log.Println("GET /passwordreset")
log.Println("GET /reset")
u := getUserName(req)
if u != "" {
http.Redirect(res, req, "/", 302) //TODO create password change form, direct to that
@ -134,7 +122,7 @@ func resetPageFront(res http.ResponseWriter, req *http.Request) {
}
func resetPageBack(res http.ResponseWriter, req *http.Request) {
log.Println("GET /passwordresetform")
log.Println("GET /reset/form")
u := getUserName(req)
if u != "" {
http.Redirect(res, req, "/", 302) //TODO create password change form, direct to that
@ -152,33 +140,13 @@ func resetPageBack(res http.ResponseWriter, req *http.Request) {
}
}
func resetSuccessPage(res http.ResponseWriter, req *http.Request) {
log.Println("GET /resetsuccess")
data := struct {
Title string
Username string
LoggedIn bool
}{
"Reset Password Success",
"Unregistered",
false,
}
tpl.ExecuteTemplate(res, "reset_success", data)
log.Println("GET /reset/success")
genericSuccessPage(res, "Reset Password Success", "Unregistered", false, "Succesfully Reset Password")
return
}
func resetErrorPage(res http.ResponseWriter, req *http.Request) {
log.Println("GET /reseterror")
data := struct {
Title string
Username string
LoggedIn bool
Error string
}{
"Reset Password Failure",
"Unregistered",
false,
"Undefined",
}
tpl.ExecuteTemplate(res, "reset_error", data)
log.Println("GET /reset/error")
genericErrorPage(res, "Reset Password Failure", "Unregistered", false, "Undefined", "reset password")
return
}
func signupPage(res http.ResponseWriter, req *http.Request) {
@ -273,3 +241,36 @@ func homePage(res http.ResponseWriter, req *http.Request) {
tpl.ExecuteTemplate(res, "index", data)
}
func genericSuccessPage(res http.ResponseWriter, title string, uname string, login bool, action string) {
data := struct {
Title string
Username string
LoggedIn bool
Action string
}{
title,
uname,
login,
action,
}
tpl.ExecuteTemplate(res, "generic_success", data)
return
}
func genericErrorPage(res http.ResponseWriter, title string, uname string, login bool, err string, action string) {
data := struct {
Title string
Username string
LoggedIn bool
Error string
Action string
}{
title,
uname,
login,
err,
action,
}
tpl.ExecuteTemplate(res, "generic_error", data)
return
}