bunch of API changes

This commit is contained in:
2020-05-05 22:03:34 +02:00
parent e89db822cc
commit e811d0a54f
22 changed files with 12289 additions and 352 deletions

View File

@@ -69,7 +69,7 @@ func (wc *WebController) adminGlobalsForm(td *TemplateData, r *http.Request) (f
}
// Value changed, try to update global setting
if _, err = td.PixelAPI.AdminSetGlobals(v.Name, v.EnteredValue); err != nil {
if err = td.PixelAPI.AdminSetGlobals(v.Name, v.EnteredValue); err != nil {
if apiErr, ok := err.(pixelapi.Error); ok {
f.SubmitMessages = append(f.SubmitMessages, template.HTML(apiErr.Message))
} else {

View File

@@ -130,11 +130,14 @@ func (wc *WebController) serveListViewer(w http.ResponseWriter, r *http.Request,
var templateData = wc.newTemplateData(w, r)
var list, err = templateData.PixelAPI.GetList(p.ByName("id"))
if err != nil {
if err, ok := err.(pixelapi.Error); ok && err.ReqError {
log.Error("API request error occurred: %s", err.Value)
if err, ok := err.(pixelapi.Error); ok && err.Status == http.StatusNotFound {
w.WriteHeader(http.StatusNotFound)
wc.templates.Get().ExecuteTemplate(w, "list_not_found", templateData)
} else {
log.Error("API request error occurred: %s", err)
w.WriteHeader(http.StatusInternalServerError)
wc.templates.Get().ExecuteTemplate(w, "500", templateData)
}
w.WriteHeader(http.StatusNotFound)
wc.templates.Get().ExecuteTemplate(w, "list_not_found", templateData)
return
}

View File

@@ -27,6 +27,7 @@ func (wc *WebController) serveLogout(
}
func (wc *WebController) registerForm(td *TemplateData, r *http.Request) (f Form) {
var err error
// This only runs on the first request
if wc.captchaSiteKey == "" {
capt, err := td.PixelAPI.GetRecaptcha()
@@ -58,7 +59,7 @@ func (wc *WebController) registerForm(td *TemplateData, r *http.Request) (f Form
Separator: true,
Type: FieldTypeUsername,
}, {
Name: "e-mail",
Name: "email",
Label: "E-mail address",
Description: "not required. your e-mail address will only be " +
"used for password resets and important account " +
@@ -66,7 +67,7 @@ func (wc *WebController) registerForm(td *TemplateData, r *http.Request) (f Form
Separator: true,
Type: FieldTypeEmail,
}, {
Name: "password1",
Name: "password",
Label: "Password",
Type: FieldTypeNewPassword,
}, {
@@ -79,10 +80,11 @@ func (wc *WebController) registerForm(td *TemplateData, r *http.Request) (f Form
Type: FieldTypeNewPassword,
}, {
Name: "recaptcha_response",
Label: "Turing test (click the white box)",
Label: "reCaptcha",
Description: "the reCaptcha turing test verifies that you " +
"are not an evil robot that is trying to flood the " +
"website with fake accounts",
"website with fake accounts. Please click the white box " +
"to prove that you're not a robot",
Separator: true,
Type: FieldTypeCaptcha,
CaptchaSiteKey: wc.captchaKey(),
@@ -94,31 +96,21 @@ func (wc *WebController) registerForm(td *TemplateData, r *http.Request) (f Form
}
if f.ReadInput(r) {
if f.FieldVal("password1") != f.FieldVal("password2") {
if f.FieldVal("password") != f.FieldVal("password2") {
f.SubmitMessages = []template.HTML{
"Password verification failed. Please enter the same " +
"password in both password fields"}
return f
}
log.Debug("capt: %s", f.FieldVal("recaptcha_response"))
resp, err := td.PixelAPI.UserRegister(
if err = td.PixelAPI.UserRegister(
f.FieldVal("username"),
f.FieldVal("e-mail"),
f.FieldVal("password1"),
f.FieldVal("email"),
f.FieldVal("password"),
f.FieldVal("recaptcha_response"),
)
if err != nil {
if apiErr, ok := err.(pixelapi.Error); ok {
f.SubmitMessages = []template.HTML{template.HTML(apiErr.Message)}
} else {
log.Error("%s", err)
f.SubmitMessages = []template.HTML{"Internal Server Error"}
}
} else if len(resp.Errors) != 0 {
// Registration errors occurred
for _, rerr := range resp.Errors {
f.SubmitMessages = append(f.SubmitMessages, template.HTML(rerr.Message))
}
); err != nil {
formAPIError(err, &f)
} else {
// Request was a success
f.SubmitSuccess = true
@@ -162,12 +154,7 @@ func (wc *WebController) loginForm(td *TemplateData, r *http.Request) (f Form) {
if f.ReadInput(r) {
loginResp, err := td.PixelAPI.UserLogin(f.FieldVal("username"), f.FieldVal("password"), false)
if err != nil {
if apiErr, ok := err.(pixelapi.Error); ok {
f.SubmitMessages = []template.HTML{template.HTML(apiErr.Message)}
} else {
log.Error("%s", err)
f.SubmitMessages = []template.HTML{"Internal Server Error"}
}
formAPIError(err, &f)
} else {
log.Debug("key %s", loginResp.APIKey)
// Request was a success
@@ -223,13 +210,11 @@ func (wc *WebController) passwordResetForm(td *TemplateData, r *http.Request) (f
}
if f.ReadInput(r) {
if err := td.PixelAPI.UserPasswordReset(f.FieldVal("email"), f.FieldVal("recaptcha_response")); err != nil {
if apiErr, ok := err.(pixelapi.Error); ok {
f.SubmitMessages = []template.HTML{template.HTML(apiErr.Message)}
} else {
log.Error("%s", err)
f.SubmitMessages = []template.HTML{"Internal Server Error"}
}
if err := td.PixelAPI.UserPasswordReset(
f.FieldVal("email"),
f.FieldVal("recaptcha_response"),
); err != nil {
formAPIError(err, &f)
} else {
f.SubmitSuccess = true
f.SubmitMessages = []template.HTML{
@@ -247,12 +232,12 @@ func (wc *WebController) passwordResetConfirmForm(td *TemplateData, r *http.Requ
Title: td.Title,
Fields: []Field{
{
Name: "password1",
Label: "password",
Name: "new_password",
Label: "Password",
Type: FieldTypeNewPassword,
}, {
Name: "password2",
Label: "password again",
Name: "new_password2",
Label: "Password again",
Description: "you need to enter your password twice so we " +
"can verify that no typing errors were made, which would " +
"prevent you from logging into your new account",
@@ -271,23 +256,20 @@ func (wc *WebController) passwordResetConfirmForm(td *TemplateData, r *http.Requ
}
if f.ReadInput(r) {
if f.FieldVal("password1") != f.FieldVal("password2") {
if f.FieldVal("new_password") != f.FieldVal("new_password2") {
f.SubmitMessages = []template.HTML{
"Password verification failed. Please enter the same " +
"password in both password fields"}
return f
}
if err := td.PixelAPI.UserPasswordResetConfirm(resetKey, f.FieldVal("password1")); err != nil {
if err.Error() == "not_found" {
f.SubmitMessages = []template.HTML{template.HTML("Password reset key not found")}
} else {
log.Error("%s", err)
f.SubmitMessages = []template.HTML{"Internal Server Error"}
}
if err := td.PixelAPI.UserPasswordResetConfirm(resetKey, f.FieldVal("new_password")); err != nil {
formAPIError(err, &f)
} else {
f.SubmitSuccess = true
f.SubmitMessages = []template.HTML{"Success! You can now log in with your new password"}
f.SubmitMessages = []template.HTML{
`Success! You can now <a href="/login">log in</a> with your new password`,
}
}
}
return f

View File

@@ -1,6 +1,7 @@
package webcontroller
import (
"fmt"
"html/template"
"net/http"
@@ -9,6 +10,44 @@ import (
"github.com/julienschmidt/httprouter"
)
// formAPIError makes it easier to display errors returned by the pixeldrain
// API. TO make use of this function the form fields should be named exactly the
// same as the API parameters
func formAPIError(err error, f *Form) {
fieldLabel := func(name string) string {
for _, v := range f.Fields {
if v.Name == name {
return v.Label
}
}
return name
}
if err, ok := err.(pixelapi.Error); ok {
if err.StatusCode == "multiple_errors" {
for _, err := range err.Errors {
// Modify the message to make it more user-friendly
if err.StatusCode == "string_out_of_range" {
err.Message = fmt.Sprintf(
"%s is too long or too short. Should be between %v and %v characters. Current length: %v",
fieldLabel(err.Extra["field"].(string)),
err.Extra["min_len"],
err.Extra["max_len"],
err.Extra["len"],
)
}
f.SubmitMessages = append(f.SubmitMessages, template.HTML(err.Message))
}
} else {
f.SubmitMessages = append(f.SubmitMessages, template.HTML(err.Message))
}
} else {
log.Error("Error submitting form: %s", err)
f.SubmitMessages = []template.HTML{"Internal Server Error"}
}
}
func (wc *WebController) serveUserSettings(
w http.ResponseWriter,
r *http.Request,
@@ -44,7 +83,7 @@ func (wc *WebController) passwordForm(td *TemplateData, r *http.Request) (f Form
Label: "Old Password",
Type: FieldTypeCurrentPassword,
}, {
Name: "new_password1",
Name: "new_password",
Label: "New Password",
Type: FieldTypeNewPassword,
}, {
@@ -60,7 +99,7 @@ func (wc *WebController) passwordForm(td *TemplateData, r *http.Request) (f Form
}
if f.ReadInput(r) {
if f.FieldVal("new_password1") != f.FieldVal("new_password2") {
if f.FieldVal("new_password") != f.FieldVal("new_password2") {
f.SubmitMessages = []template.HTML{
"Password verification failed. Please enter the same " +
"password in both new password fields"}
@@ -71,14 +110,9 @@ func (wc *WebController) passwordForm(td *TemplateData, r *http.Request) (f Form
// form
if err := td.PixelAPI.UserPasswordSet(
f.FieldVal("old_password"),
f.FieldVal("new_password1"),
f.FieldVal("new_password"),
); err != nil {
if apiErr, ok := err.(pixelapi.Error); ok {
f.SubmitMessages = []template.HTML{template.HTML(apiErr.Message)}
} else {
log.Error("%s", err)
f.SubmitMessages = []template.HTML{"Internal Server Error"}
}
formAPIError(err, &f)
} else {
// Request was a success
f.SubmitSuccess = true
@@ -111,12 +145,7 @@ func (wc *WebController) emailForm(td *TemplateData, r *http.Request) (f Form) {
f.FieldVal("new_email"),
false,
); err != nil {
if apiErr, ok := err.(pixelapi.Error); ok {
f.SubmitMessages = []template.HTML{template.HTML(apiErr.Message)}
} else {
log.Error("%s", err)
f.SubmitMessages = []template.HTML{"Internal Server Error"}
}
formAPIError(err, &f)
} else {
// Request was a success
f.SubmitSuccess = true
@@ -131,18 +160,18 @@ func (wc *WebController) serveEmailConfirm(
r *http.Request,
p httprouter.Params,
) {
var err error
var status string
if key, err := wc.getAPIKey(r); err == nil {
api := pixelapi.New(wc.apiURLInternal)
api.APIKey = key
err = api.UserEmailResetConfirm(r.FormValue("key"))
if err != nil && err.Error() == "not_found" {
status = "not_found"
} else if err != nil {
status = "internal_error"
} else {
status = "success"
}
api := pixelapi.New(wc.apiURLInternal)
err = api.UserEmailResetConfirm(r.FormValue("key"))
if err != nil && err.Error() == "not_found" {
status = "not_found"
} else if err != nil {
log.Error("E-mail reset fail: %s", err)
status = "internal_error"
} else {
status = "success"
}
td := wc.newTemplateData(w, r)
@@ -171,12 +200,7 @@ func (wc *WebController) usernameForm(td *TemplateData, r *http.Request) (f Form
if f.ReadInput(r) {
if err := td.PixelAPI.UserSetUsername(f.FieldVal("new_username")); err != nil {
if apiErr, ok := err.(pixelapi.Error); ok {
f.SubmitMessages = []template.HTML{template.HTML(apiErr.Message)}
} else {
log.Error("%s", err)
f.SubmitMessages = []template.HTML{"Internal Server Error"}
}
formAPIError(err, &f)
} else {
// Request was a success
f.SubmitSuccess = true