From 0c347eda8e9b759b0564decd9a8524d348678fd0 Mon Sep 17 00:00:00 2001 From: Steve Date: Thu, 12 Nov 2020 14:33:01 -0500 Subject: [PATCH] better routes, standardize error messages part 1 --- main.go | 12 +-- reset.go | 12 +-- session.go | 9 +- templates/error.html | 7 +- templates/index.html | 2 +- templates/minecraft_error.html | 8 -- templates/minecraft_success.html | 6 -- templates/reset_error.html | 8 -- templates/reset_password_back.html | 2 +- templates/reset_password_front.html | 2 +- .../{reset_success.html => success.html} | 4 +- web.go | 93 ++++++++++--------- 12 files changed, 74 insertions(+), 91 deletions(-) delete mode 100644 templates/minecraft_error.html delete mode 100644 templates/minecraft_success.html delete mode 100644 templates/reset_error.html rename templates/{reset_success.html => success.html} (56%) diff --git a/main.go b/main.go index c4893c4..9a70ebd 100644 --- a/main.go +++ b/main.go @@ -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") diff --git a/reset.go b/reset.go index 4e7174c..21f8674 100644 --- a/reset.go +++ b/reset.go @@ -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 } diff --git a/session.go b/session.go index fd2a608..8d9db32 100644 --- a/session.go +++ b/session.go @@ -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) diff --git a/templates/error.html b/templates/error.html index a3be7e4..ae78847 100644 --- a/templates/error.html +++ b/templates/error.html @@ -1,5 +1,8 @@ -{{ define "error" }} +{{ define "generic_error" }} {{ template "header" .}} -An error occured. Please let the admin know. +

Unable to {{.Action}} due to the following error:

+

{{.Error}}

+

Please try again or let the admin know.

+

Return to homepage

{{template "footer" .}} {{ end }} diff --git a/templates/index.html b/templates/index.html index 8b295c9..45c88c6 100644 --- a/templates/index.html +++ b/templates/index.html @@ -6,7 +6,7 @@

Minecraft Account Status

{{else}}

Register

-

Reset Password

+

Reset Password

{{end}} {{template "footer" .}} {{ end }} diff --git a/templates/minecraft_error.html b/templates/minecraft_error.html deleted file mode 100644 index bb47697..0000000 --- a/templates/minecraft_error.html +++ /dev/null @@ -1,8 +0,0 @@ -{{ define "minecraft_error" }} -{{ template "header" .}} -

Unable to link your Minecraft account due to the following error:

-

{{.Error}}

-

Please try again or let the admin know.

-

Return to homepage

-{{template "footer" .}} -{{ end }} diff --git a/templates/minecraft_success.html b/templates/minecraft_success.html deleted file mode 100644 index d774db1..0000000 --- a/templates/minecraft_success.html +++ /dev/null @@ -1,6 +0,0 @@ -{{ define "minecraft_success" }} -{{ template "header" .}} -

Your Minecraft account has been linked

-

Return to homepage

-{{template "footer" .}} -{{ end }} diff --git a/templates/reset_error.html b/templates/reset_error.html deleted file mode 100644 index 5065baf..0000000 --- a/templates/reset_error.html +++ /dev/null @@ -1,8 +0,0 @@ -{{ define "reset_error" }} -{{ template "header" .}} -

Unable to reset your password due to the following error:

-

{{.Error}}

-

Please try again or let the admin know.

-

Return to homepage

-{{template "footer" .}} -{{ end }} diff --git a/templates/reset_password_back.html b/templates/reset_password_back.html index 3bffc9d..a700039 100644 --- a/templates/reset_password_back.html +++ b/templates/reset_password_back.html @@ -3,7 +3,7 @@

Password Recovery

You should receive a Password Recovery token momentarily in your email inbox

Please enter it, and your new password, into the form below to reset your password

-
+
diff --git a/templates/reset_password_front.html b/templates/reset_password_front.html index dbfceea..c192b91 100644 --- a/templates/reset_password_front.html +++ b/templates/reset_password_front.html @@ -2,7 +2,7 @@ {{ template "header" .}}

Lookup By Email

Please enter your email address to reset your password

- +
diff --git a/templates/reset_success.html b/templates/success.html similarity index 56% rename from templates/reset_success.html rename to templates/success.html index f049bca..ec99356 100644 --- a/templates/reset_success.html +++ b/templates/success.html @@ -1,6 +1,6 @@ -{{ define "reset_success" }} +{{ define "generic_success" }} {{ template "header" .}} -

Your password has been succesfully reset!

+

{{.Action}}

Return to homepage

{{template "footer" .}} {{ end }} diff --git a/web.go b/web.go index d002df9..98c8e03 100644 --- a/web.go +++ b/web.go @@ -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 +}