2020-05-24 14:41:19 +03:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
2020-05-24 17:34:32 +03:00
|
|
|
"fmt"
|
2020-05-24 14:41:19 +03:00
|
|
|
"github.com/gorilla/schema"
|
2020-05-25 23:24:29 +03:00
|
|
|
"github.com/muety/wakapi/middlewares"
|
2020-05-24 14:41:19 +03:00
|
|
|
"github.com/muety/wakapi/models"
|
|
|
|
"github.com/muety/wakapi/services"
|
|
|
|
"github.com/muety/wakapi/utils"
|
|
|
|
"net/http"
|
2020-05-24 17:34:32 +03:00
|
|
|
"net/url"
|
2020-05-30 23:19:05 +03:00
|
|
|
"time"
|
2020-05-24 14:41:19 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type IndexHandler struct {
|
2020-05-30 21:41:27 +03:00
|
|
|
config *models.Config
|
|
|
|
userSrvc *services.UserService
|
|
|
|
keyValueSrvc *services.KeyValueService
|
2020-05-24 14:41:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var loginDecoder = schema.NewDecoder()
|
2020-05-24 17:34:32 +03:00
|
|
|
var signupDecoder = schema.NewDecoder()
|
2020-05-24 14:41:19 +03:00
|
|
|
|
2020-05-30 21:41:27 +03:00
|
|
|
func NewIndexHandler(userService *services.UserService, keyValueService *services.KeyValueService) *IndexHandler {
|
2020-05-24 14:41:19 +03:00
|
|
|
return &IndexHandler{
|
2020-05-30 21:41:27 +03:00
|
|
|
config: models.GetConfig(),
|
|
|
|
userSrvc: userService,
|
|
|
|
keyValueSrvc: keyValueService,
|
2020-05-24 14:41:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-07 20:28:32 +03:00
|
|
|
func (h *IndexHandler) GetIndex(w http.ResponseWriter, r *http.Request) {
|
2020-05-24 14:41:19 +03:00
|
|
|
if h.config.IsDev() {
|
|
|
|
loadTemplates()
|
|
|
|
}
|
|
|
|
|
|
|
|
if cookie, err := r.Cookie(models.AuthCookieKey); err == nil && cookie.Value != "" {
|
2020-05-24 18:08:44 +03:00
|
|
|
http.Redirect(w, r, fmt.Sprintf("%s/summary", h.config.BasePath), http.StatusFound)
|
2020-05-24 14:41:19 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:34:32 +03:00
|
|
|
if handleAlerts(w, r, "") {
|
2020-05-24 14:41:19 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
templates["index.tpl.html"].Execute(w, nil)
|
|
|
|
}
|
|
|
|
|
2020-06-07 20:28:32 +03:00
|
|
|
func (h *IndexHandler) GetImprint(w http.ResponseWriter, r *http.Request) {
|
2020-05-30 21:41:27 +03:00
|
|
|
if h.config.IsDev() {
|
|
|
|
loadTemplates()
|
|
|
|
}
|
|
|
|
|
|
|
|
text := "failed to load content"
|
|
|
|
if data, err := h.keyValueSrvc.GetString(models.ImprintKey); err == nil {
|
|
|
|
text = data.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
templates["imprint.tpl.html"].Execute(w, &struct {
|
|
|
|
HtmlText string
|
|
|
|
}{HtmlText: text})
|
|
|
|
}
|
|
|
|
|
2020-06-07 20:28:32 +03:00
|
|
|
func (h *IndexHandler) PostLogin(w http.ResponseWriter, r *http.Request) {
|
2020-05-24 14:41:19 +03:00
|
|
|
if h.config.IsDev() {
|
|
|
|
loadTemplates()
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:34:32 +03:00
|
|
|
if cookie, err := r.Cookie(models.AuthCookieKey); err == nil && cookie.Value != "" {
|
2020-05-24 18:08:44 +03:00
|
|
|
http.Redirect(w, r, fmt.Sprintf("%s/summary", h.config.BasePath), http.StatusFound)
|
2020-05-24 17:34:32 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-24 14:41:19 +03:00
|
|
|
var login models.Login
|
|
|
|
if err := r.ParseForm(); err != nil {
|
2020-05-24 17:34:32 +03:00
|
|
|
respondAlert(w, "missing parameters", "", "", http.StatusBadRequest)
|
2020-05-24 14:41:19 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := loginDecoder.Decode(&login, r.PostForm); err != nil {
|
2020-05-24 17:34:32 +03:00
|
|
|
respondAlert(w, "missing parameters", "", "", http.StatusBadRequest)
|
2020-05-24 14:41:19 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := h.userSrvc.GetUserById(login.Username)
|
|
|
|
if err != nil {
|
2020-05-24 17:34:32 +03:00
|
|
|
respondAlert(w, "resource not found", "", "", http.StatusNotFound)
|
2020-05-24 14:41:19 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-25 23:24:29 +03:00
|
|
|
// TODO: depending on middleware package here is a hack
|
|
|
|
if !middlewares.CheckAndMigratePassword(user, &login, h.config.PasswordSalt, h.userSrvc) {
|
2020-05-24 17:34:32 +03:00
|
|
|
respondAlert(w, "invalid credentials", "", "", http.StatusUnauthorized)
|
2020-05-24 14:41:19 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
encoded, err := h.config.SecureCookie.Encode(models.AuthCookieKey, login)
|
|
|
|
if err != nil {
|
2020-05-24 17:34:32 +03:00
|
|
|
respondAlert(w, "internal server error", "", "", http.StatusInternalServerError)
|
2020-05-24 14:41:19 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-30 23:19:05 +03:00
|
|
|
user.LastLoggedInAt = models.CustomTime(time.Now())
|
|
|
|
h.userSrvc.Update(user)
|
|
|
|
|
2020-05-24 14:41:19 +03:00
|
|
|
cookie := &http.Cookie{
|
|
|
|
Name: models.AuthCookieKey,
|
|
|
|
Value: encoded,
|
|
|
|
Path: "/",
|
2020-05-30 13:11:21 +03:00
|
|
|
Secure: !h.config.InsecureCookies,
|
2020-05-24 14:41:19 +03:00
|
|
|
HttpOnly: true,
|
|
|
|
}
|
|
|
|
http.SetCookie(w, cookie)
|
2020-05-24 18:08:44 +03:00
|
|
|
http.Redirect(w, r, fmt.Sprintf("%s/summary", h.config.BasePath), http.StatusFound)
|
2020-05-24 14:41:19 +03:00
|
|
|
}
|
|
|
|
|
2020-06-07 20:28:32 +03:00
|
|
|
func (h *IndexHandler) PostLogout(w http.ResponseWriter, r *http.Request) {
|
2020-05-24 14:41:19 +03:00
|
|
|
if h.config.IsDev() {
|
|
|
|
loadTemplates()
|
|
|
|
}
|
|
|
|
|
2020-05-30 13:11:21 +03:00
|
|
|
utils.ClearCookie(w, models.AuthCookieKey, !h.config.InsecureCookies)
|
2020-05-24 18:08:44 +03:00
|
|
|
http.Redirect(w, r, fmt.Sprintf("%s/", h.config.BasePath), http.StatusFound)
|
2020-05-24 14:41:19 +03:00
|
|
|
}
|
|
|
|
|
2020-06-07 20:28:32 +03:00
|
|
|
func (h *IndexHandler) GetSignup(w http.ResponseWriter, r *http.Request) {
|
2020-05-24 17:34:32 +03:00
|
|
|
if h.config.IsDev() {
|
|
|
|
loadTemplates()
|
|
|
|
}
|
|
|
|
|
|
|
|
if cookie, err := r.Cookie(models.AuthCookieKey); err == nil && cookie.Value != "" {
|
2020-05-24 18:08:44 +03:00
|
|
|
http.Redirect(w, r, fmt.Sprintf("%s/summary", h.config.BasePath), http.StatusFound)
|
2020-05-24 17:34:32 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if handleAlerts(w, r, "signup.tpl.html") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
templates["signup.tpl.html"].Execute(w, nil)
|
|
|
|
}
|
|
|
|
|
2020-06-07 20:28:32 +03:00
|
|
|
func (h *IndexHandler) PostSignup(w http.ResponseWriter, r *http.Request) {
|
2020-05-24 17:34:32 +03:00
|
|
|
if h.config.IsDev() {
|
|
|
|
loadTemplates()
|
|
|
|
}
|
|
|
|
|
|
|
|
if cookie, err := r.Cookie(models.AuthCookieKey); err == nil && cookie.Value != "" {
|
2020-05-24 18:08:44 +03:00
|
|
|
http.Redirect(w, r, fmt.Sprintf("%s/summary", h.config.BasePath), http.StatusFound)
|
2020-05-24 17:34:32 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var signup models.Signup
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
respondAlert(w, "missing parameters", "", "signup.tpl.html", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := signupDecoder.Decode(&signup, r.PostForm); err != nil {
|
|
|
|
respondAlert(w, "missing parameters", "", "signup.tpl.html", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-24 22:42:15 +03:00
|
|
|
if !signup.IsValid() {
|
|
|
|
respondAlert(w, "invalid parameters", "", "signup.tpl.html", http.StatusBadRequest)
|
2020-05-24 17:34:32 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, created, err := h.userSrvc.CreateOrGet(&signup)
|
|
|
|
if err != nil {
|
|
|
|
respondAlert(w, "failed to create new user", "", "signup.tpl.html", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !created {
|
|
|
|
respondAlert(w, "user already existing", "", "signup.tpl.html", http.StatusConflict)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := url.QueryEscape("account created successfully")
|
2020-05-24 18:08:44 +03:00
|
|
|
http.Redirect(w, r, fmt.Sprintf("%s/?success=%s", h.config.BasePath, msg), http.StatusFound)
|
2020-05-24 17:34:32 +03:00
|
|
|
}
|