2018-06-20 23:47:47 +02:00
|
|
|
package webcontroller
|
2018-06-23 21:17:53 +02:00
|
|
|
|
|
|
|
import (
|
2021-09-23 22:21:27 +02:00
|
|
|
"fmt"
|
2019-02-22 00:04:57 +01:00
|
|
|
"html/template"
|
2018-06-23 21:17:53 +02:00
|
|
|
"net/http"
|
|
|
|
|
2021-11-16 15:20:15 +01:00
|
|
|
"fornaxian.tech/log"
|
2021-09-23 22:21:27 +02:00
|
|
|
"fornaxian.tech/pixeldrain_api_client/pixelapi"
|
2018-06-23 21:17:53 +02:00
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
)
|
|
|
|
|
2021-09-23 22:21:27 +02:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-09-14 14:29:05 +02:00
|
|
|
if apierr, ok := err.(pixelapi.Error); ok {
|
|
|
|
if apierr.StatusCode == "multiple_errors" {
|
|
|
|
for _, err := range apierr.Errors {
|
2021-09-23 22:21:27 +02:00
|
|
|
// 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"],
|
|
|
|
)
|
|
|
|
} else if err.StatusCode == "field_contains_illegal_character" {
|
|
|
|
err.Message = fmt.Sprintf(
|
|
|
|
"Character '%v' is not allowed in %s",
|
|
|
|
err.Extra["char"],
|
|
|
|
fieldLabel(err.Extra["field"].(string)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
f.SubmitMessages = append(f.SubmitMessages, template.HTML(err.Message))
|
|
|
|
}
|
|
|
|
} else {
|
2023-09-14 14:29:05 +02:00
|
|
|
f.SubmitMessages = append(f.SubmitMessages, template.HTML(apierr.Message))
|
2021-09-23 22:21:27 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Error("Error submitting form: %s", err)
|
|
|
|
f.SubmitMessages = []template.HTML{"Internal Server Error"}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-23 21:17:53 +02:00
|
|
|
func (wc *WebController) serveLogout(
|
|
|
|
w http.ResponseWriter,
|
|
|
|
r *http.Request,
|
|
|
|
p httprouter.Params,
|
|
|
|
) {
|
|
|
|
if key, err := wc.getAPIKey(r); err == nil {
|
2021-01-05 00:00:46 +01:00
|
|
|
var api = wc.api.Login(key)
|
2021-03-10 20:13:32 +01:00
|
|
|
if err = api.DeleteUserSession(key); err != nil {
|
2019-12-17 19:28:30 +01:00
|
|
|
log.Warn("logout failed for session '%s': %s", key, err)
|
2018-06-23 21:17:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 14:47:51 +02:00
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
2018-07-08 14:40:20 +02:00
|
|
|
}
|
2019-02-22 00:04:57 +01:00
|
|
|
|
2019-12-23 23:56:57 +01:00
|
|
|
func (wc *WebController) passwordResetForm(td *TemplateData, r *http.Request) (f Form) {
|
|
|
|
f = Form{
|
2019-12-17 19:28:30 +01:00
|
|
|
Name: "password_reset",
|
2020-07-31 21:21:14 +02:00
|
|
|
Title: "Recover lost password",
|
2019-12-23 23:56:57 +01:00
|
|
|
Fields: []Field{
|
2019-02-25 22:53:09 +01:00
|
|
|
{
|
2019-12-17 19:28:30 +01:00
|
|
|
Name: "email",
|
|
|
|
Label: "E-mail address",
|
2020-07-21 12:01:02 +02:00
|
|
|
Description: `we will send a password reset link to this e-mail
|
2020-08-03 15:36:51 +02:00
|
|
|
address`,
|
2022-08-04 20:19:30 +02:00
|
|
|
Type: FieldTypeEmail,
|
2019-02-25 22:53:09 +01:00
|
|
|
}, {
|
2019-12-17 19:28:30 +01:00
|
|
|
Name: "recaptcha_response",
|
|
|
|
Label: "Turing test (click the white box)",
|
|
|
|
Description: "the reCaptcha turing test verifies that you " +
|
|
|
|
"are not an evil robot that is trying hijack accounts",
|
2019-12-23 23:56:57 +01:00
|
|
|
Type: FieldTypeCaptcha,
|
2019-12-17 19:28:30 +01:00
|
|
|
CaptchaSiteKey: wc.captchaKey(),
|
2019-02-25 22:53:09 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
SubmitLabel: "Submit",
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.ReadInput(r) {
|
2021-03-10 20:13:32 +01:00
|
|
|
if err := td.PixelAPI.PutUserPasswordReset(
|
2020-05-05 22:03:34 +02:00
|
|
|
f.FieldVal("email"),
|
|
|
|
f.FieldVal("recaptcha_response"),
|
|
|
|
); err != nil {
|
|
|
|
formAPIError(err, &f)
|
2019-02-25 22:53:09 +01:00
|
|
|
} else {
|
|
|
|
f.SubmitSuccess = true
|
2019-12-23 23:56:57 +01:00
|
|
|
f.SubmitMessages = []template.HTML{
|
|
|
|
"Success! Check your inbox for instructions to reset your password",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wc *WebController) passwordResetConfirmForm(td *TemplateData, r *http.Request) (f Form) {
|
|
|
|
f = Form{
|
|
|
|
Name: "password_reset_confirm",
|
2020-07-31 21:21:14 +02:00
|
|
|
Title: "Reset lost password",
|
2019-12-23 23:56:57 +01:00
|
|
|
Fields: []Field{
|
|
|
|
{
|
2020-05-05 22:03:34 +02:00
|
|
|
Name: "new_password",
|
|
|
|
Label: "Password",
|
2019-12-23 23:56:57 +01:00
|
|
|
Type: FieldTypeNewPassword,
|
|
|
|
}, {
|
2020-05-05 22:03:34 +02:00
|
|
|
Name: "new_password2",
|
|
|
|
Label: "Password again",
|
2019-12-23 23:56:57 +01:00
|
|
|
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",
|
2022-08-04 20:19:30 +02:00
|
|
|
Type: FieldTypeNewPassword,
|
2019-12-23 23:56:57 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
SubmitLabel: "Submit",
|
|
|
|
}
|
|
|
|
|
|
|
|
var resetKey = r.FormValue("key")
|
|
|
|
if resetKey == "" {
|
|
|
|
f.SubmitSuccess = false
|
|
|
|
f.SubmitMessages = []template.HTML{"Password reset key required"}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.ReadInput(r) {
|
2020-05-05 22:03:34 +02:00
|
|
|
if f.FieldVal("new_password") != f.FieldVal("new_password2") {
|
2019-12-23 23:56:57 +01:00
|
|
|
f.SubmitMessages = []template.HTML{
|
|
|
|
"Password verification failed. Please enter the same " +
|
|
|
|
"password in both password fields"}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2021-03-10 20:13:32 +01:00
|
|
|
if err := td.PixelAPI.PutUserPasswordResetConfirm(resetKey, f.FieldVal("new_password")); err != nil {
|
2020-05-05 22:03:34 +02:00
|
|
|
formAPIError(err, &f)
|
2019-12-23 23:56:57 +01:00
|
|
|
} else {
|
|
|
|
f.SubmitSuccess = true
|
2020-05-05 22:03:34 +02:00
|
|
|
f.SubmitMessages = []template.HTML{
|
|
|
|
`Success! You can now <a href="/login">log in</a> with your new password`,
|
|
|
|
}
|
2019-02-25 22:53:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return f
|
2019-02-22 00:04:57 +01:00
|
|
|
}
|
2021-09-23 22:21:27 +02:00
|
|
|
|
|
|
|
func (wc *WebController) serveEmailConfirm(
|
|
|
|
w http.ResponseWriter,
|
|
|
|
r *http.Request,
|
|
|
|
p httprouter.Params,
|
|
|
|
) {
|
|
|
|
var err error
|
|
|
|
var status string
|
|
|
|
|
|
|
|
err = wc.api.PutUserEmailResetConfirm(r.FormValue("key"))
|
|
|
|
if err != nil && err.Error() == "not_found" {
|
|
|
|
status = "not_found"
|
|
|
|
} else if err != nil {
|
2022-01-11 18:53:01 +01:00
|
|
|
log.Debug("E-mail reset fail: %s", err)
|
2021-09-23 22:21:27 +02:00
|
|
|
status = "internal_error"
|
|
|
|
} else {
|
|
|
|
status = "success"
|
|
|
|
}
|
|
|
|
|
|
|
|
td := wc.newTemplateData(w, r)
|
|
|
|
td.Other = status
|
|
|
|
|
2024-09-24 00:12:41 +02:00
|
|
|
wc.templates.Run(w, r, "email_confirm", td)
|
2021-09-23 22:21:27 +02:00
|
|
|
}
|