2020-06-07 20:28:32 +03:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/gorilla/schema"
|
2020-10-09 22:37:16 +03:00
|
|
|
conf "github.com/muety/wakapi/config"
|
2020-06-07 20:28:32 +03:00
|
|
|
"github.com/muety/wakapi/models"
|
|
|
|
"github.com/muety/wakapi/services"
|
|
|
|
"github.com/muety/wakapi/utils"
|
|
|
|
"net/http"
|
2020-06-07 20:58:06 +03:00
|
|
|
"net/url"
|
2020-06-07 20:28:32 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type SettingsHandler struct {
|
2020-10-09 22:37:16 +03:00
|
|
|
config *conf.Config
|
2020-06-07 20:28:32 +03:00
|
|
|
userSrvc *services.UserService
|
|
|
|
}
|
|
|
|
|
|
|
|
var credentialsDecoder = schema.NewDecoder()
|
|
|
|
|
|
|
|
func NewSettingsHandler(userService *services.UserService) *SettingsHandler {
|
|
|
|
return &SettingsHandler{
|
2020-10-09 22:37:16 +03:00
|
|
|
config: conf.Get(),
|
2020-06-07 20:28:32 +03:00
|
|
|
userSrvc: userService,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *SettingsHandler) GetIndex(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if h.config.IsDev() {
|
|
|
|
loadTemplates()
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:09:23 +03:00
|
|
|
user := r.Context().Value(models.UserKey).(*models.User)
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"User": user,
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: when alerts are present, other data will not be passed to the template
|
2020-10-09 22:37:16 +03:00
|
|
|
if handleAlerts(w, r, conf.SettingsTemplate) {
|
2020-06-07 20:58:06 +03:00
|
|
|
return
|
|
|
|
}
|
2020-10-09 22:37:16 +03:00
|
|
|
templates[conf.SettingsTemplate].Execute(w, data)
|
2020-06-07 20:28:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *SettingsHandler) PostCredentials(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if h.config.IsDev() {
|
|
|
|
loadTemplates()
|
|
|
|
}
|
|
|
|
|
|
|
|
user := r.Context().Value(models.UserKey).(*models.User)
|
|
|
|
|
|
|
|
var credentials models.CredentialsReset
|
|
|
|
if err := r.ParseForm(); err != nil {
|
2020-10-09 22:37:16 +03:00
|
|
|
respondAlert(w, "missing parameters", "", conf.SettingsTemplate, http.StatusBadRequest)
|
2020-06-07 20:28:32 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := credentialsDecoder.Decode(&credentials, r.PostForm); err != nil {
|
2020-10-09 22:37:16 +03:00
|
|
|
respondAlert(w, "missing parameters", "", conf.SettingsTemplate, http.StatusBadRequest)
|
2020-06-07 20:28:32 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-04 11:37:38 +03:00
|
|
|
if !utils.CheckPasswordBcrypt(user, credentials.PasswordOld, h.config.Security.PasswordSalt) {
|
2020-10-09 22:37:16 +03:00
|
|
|
respondAlert(w, "invalid credentials", "", conf.SettingsTemplate, http.StatusUnauthorized)
|
2020-06-07 20:28:32 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !credentials.IsValid() {
|
2020-10-09 22:37:16 +03:00
|
|
|
respondAlert(w, "invalid parameters", "", conf.SettingsTemplate, http.StatusBadRequest)
|
2020-06-07 20:28:32 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user.Password = credentials.PasswordNew
|
2020-10-04 11:37:38 +03:00
|
|
|
if err := utils.HashPassword(user, h.config.Security.PasswordSalt); err != nil {
|
2020-10-09 22:37:16 +03:00
|
|
|
respondAlert(w, "internal server error", "", conf.SettingsTemplate, http.StatusInternalServerError)
|
2020-06-07 20:28:32 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := h.userSrvc.Update(user); err != nil {
|
2020-10-09 22:37:16 +03:00
|
|
|
respondAlert(w, "internal server error", "", conf.SettingsTemplate, http.StatusInternalServerError)
|
2020-06-07 20:28:32 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
login := &models.Login{
|
|
|
|
Username: user.ID,
|
|
|
|
Password: user.Password,
|
|
|
|
}
|
2020-10-04 11:37:38 +03:00
|
|
|
encoded, err := h.config.Security.SecureCookie.Encode(models.AuthCookieKey, login)
|
2020-06-07 20:28:32 +03:00
|
|
|
if err != nil {
|
2020-10-09 22:37:16 +03:00
|
|
|
respondAlert(w, "internal server error", "", conf.SettingsTemplate, http.StatusInternalServerError)
|
2020-06-07 20:28:32 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cookie := &http.Cookie{
|
|
|
|
Name: models.AuthCookieKey,
|
|
|
|
Value: encoded,
|
|
|
|
Path: "/",
|
2020-10-04 11:37:38 +03:00
|
|
|
Secure: !h.config.Security.InsecureCookies,
|
2020-06-07 20:28:32 +03:00
|
|
|
HttpOnly: true,
|
|
|
|
}
|
|
|
|
http.SetCookie(w, cookie)
|
2020-06-07 20:58:06 +03:00
|
|
|
|
|
|
|
msg := url.QueryEscape("password was updated successfully")
|
2020-10-04 11:37:38 +03:00
|
|
|
http.Redirect(w, r, fmt.Sprintf("%s/settings?success=%s", h.config.Server.BasePath, msg), http.StatusFound)
|
2020-06-07 20:28:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *SettingsHandler) PostResetApiKey(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if h.config.IsDev() {
|
|
|
|
loadTemplates()
|
|
|
|
}
|
2020-06-07 20:58:06 +03:00
|
|
|
|
|
|
|
user := r.Context().Value(models.UserKey).(*models.User)
|
|
|
|
if _, err := h.userSrvc.ResetApiKey(user); err != nil {
|
2020-10-09 22:37:16 +03:00
|
|
|
respondAlert(w, "internal server error", "", conf.SettingsTemplate, http.StatusInternalServerError)
|
2020-06-07 20:58:06 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := url.QueryEscape(fmt.Sprintf("your new api key is: %s", user.ApiKey))
|
2020-10-04 11:37:38 +03:00
|
|
|
http.Redirect(w, r, fmt.Sprintf("%s/settings?success=%s", h.config.Server.BasePath, msg), http.StatusFound)
|
2020-06-07 20:28:32 +03:00
|
|
|
}
|
2020-09-12 17:09:23 +03:00
|
|
|
|
|
|
|
func (h *SettingsHandler) PostToggleBadges(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if h.config.IsDev() {
|
|
|
|
loadTemplates()
|
|
|
|
}
|
|
|
|
|
|
|
|
user := r.Context().Value(models.UserKey).(*models.User)
|
|
|
|
|
|
|
|
if _, err := h.userSrvc.ToggleBadges(user); err != nil {
|
2020-10-09 22:37:16 +03:00
|
|
|
respondAlert(w, "internal server error", "", conf.SettingsTemplate, http.StatusInternalServerError)
|
2020-09-12 17:09:23 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-04 11:37:38 +03:00
|
|
|
http.Redirect(w, r, fmt.Sprintf("%s/settings", h.config.Server.BasePath), http.StatusFound)
|
2020-09-12 17:09:23 +03:00
|
|
|
}
|