1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00
wakapi/routes/public.go

181 lines
4.9 KiB
Go
Raw Normal View History

package routes
import (
2020-05-24 17:34:32 +03:00
"fmt"
"github.com/gorilla/schema"
2020-10-09 22:37:16 +03:00
conf "github.com/muety/wakapi/config"
"github.com/muety/wakapi/middlewares"
"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"
"time"
)
type IndexHandler struct {
2020-10-09 22:37:16 +03:00
config *conf.Config
userSrvc *services.UserService
keyValueSrvc *services.KeyValueService
}
var loginDecoder = schema.NewDecoder()
2020-05-24 17:34:32 +03:00
var signupDecoder = schema.NewDecoder()
func NewIndexHandler(userService *services.UserService, keyValueService *services.KeyValueService) *IndexHandler {
return &IndexHandler{
2020-10-09 22:37:16 +03:00
config: conf.Get(),
userSrvc: userService,
keyValueSrvc: keyValueService,
}
}
func (h *IndexHandler) GetIndex(w http.ResponseWriter, r *http.Request) {
if h.config.IsDev() {
loadTemplates()
}
if cookie, err := r.Cookie(models.AuthCookieKey); err == nil && cookie.Value != "" {
http.Redirect(w, r, fmt.Sprintf("%s/summary", h.config.Server.BasePath), http.StatusFound)
return
}
2020-05-24 17:34:32 +03:00
if handleAlerts(w, r, "") {
return
}
2020-10-09 22:37:16 +03:00
templates[conf.IndexTemplate].Execute(w, nil)
}
func (h *IndexHandler) GetImprint(w http.ResponseWriter, r *http.Request) {
if h.config.IsDev() {
loadTemplates()
}
text := "failed to load content"
if data, err := h.keyValueSrvc.GetString(models.ImprintKey); err == nil {
text = data.Value
}
2020-10-09 22:37:16 +03:00
templates[conf.ImprintTemplate].Execute(w, &struct {
HtmlText string
}{HtmlText: text})
}
func (h *IndexHandler) PostLogin(w http.ResponseWriter, r *http.Request) {
if h.config.IsDev() {
loadTemplates()
}
2020-05-24 17:34:32 +03:00
if cookie, err := r.Cookie(models.AuthCookieKey); err == nil && cookie.Value != "" {
http.Redirect(w, r, fmt.Sprintf("%s/summary", h.config.Server.BasePath), http.StatusFound)
2020-05-24 17:34:32 +03:00
return
}
var login models.Login
if err := r.ParseForm(); err != nil {
2020-05-24 17:34:32 +03:00
respondAlert(w, "missing parameters", "", "", http.StatusBadRequest)
return
}
if err := loginDecoder.Decode(&login, r.PostForm); err != nil {
2020-05-24 17:34:32 +03:00
respondAlert(w, "missing parameters", "", "", http.StatusBadRequest)
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)
return
}
// TODO: depending on middleware package here is a hack
if !middlewares.CheckAndMigratePassword(user, &login, h.config.Security.PasswordSalt, h.userSrvc) {
2020-05-24 17:34:32 +03:00
respondAlert(w, "invalid credentials", "", "", http.StatusUnauthorized)
return
}
encoded, err := h.config.Security.SecureCookie.Encode(models.AuthCookieKey, login)
if err != nil {
2020-05-24 17:34:32 +03:00
respondAlert(w, "internal server error", "", "", http.StatusInternalServerError)
return
}
user.LastLoggedInAt = models.CustomTime(time.Now())
h.userSrvc.Update(user)
cookie := &http.Cookie{
Name: models.AuthCookieKey,
Value: encoded,
Path: "/",
Secure: !h.config.Security.InsecureCookies,
HttpOnly: true,
}
http.SetCookie(w, cookie)
http.Redirect(w, r, fmt.Sprintf("%s/summary", h.config.Server.BasePath), http.StatusFound)
}
func (h *IndexHandler) PostLogout(w http.ResponseWriter, r *http.Request) {
if h.config.IsDev() {
loadTemplates()
}
utils.ClearCookie(w, models.AuthCookieKey, !h.config.Security.InsecureCookies)
http.Redirect(w, r, fmt.Sprintf("%s/", h.config.Server.BasePath), http.StatusFound)
}
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 != "" {
http.Redirect(w, r, fmt.Sprintf("%s/summary", h.config.Server.BasePath), http.StatusFound)
2020-05-24 17:34:32 +03:00
return
}
2020-10-09 22:37:16 +03:00
if handleAlerts(w, r, conf.SignupTemplate) {
2020-05-24 17:34:32 +03:00
return
}
2020-10-09 22:37:16 +03:00
templates[conf.SignupTemplate].Execute(w, nil)
2020-05-24 17:34: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 != "" {
http.Redirect(w, r, fmt.Sprintf("%s/summary", h.config.Server.BasePath), http.StatusFound)
2020-05-24 17:34:32 +03:00
return
}
var signup models.Signup
if err := r.ParseForm(); err != nil {
2020-10-09 22:37:16 +03:00
respondAlert(w, "missing parameters", "", conf.SignupTemplate, http.StatusBadRequest)
2020-05-24 17:34:32 +03:00
return
}
if err := signupDecoder.Decode(&signup, r.PostForm); err != nil {
2020-10-09 22:37:16 +03:00
respondAlert(w, "missing parameters", "", conf.SignupTemplate, http.StatusBadRequest)
2020-05-24 17:34:32 +03:00
return
}
2020-05-24 22:42:15 +03:00
if !signup.IsValid() {
2020-10-09 22:37:16 +03:00
respondAlert(w, "invalid parameters", "", conf.SignupTemplate, http.StatusBadRequest)
2020-05-24 17:34:32 +03:00
return
}
_, created, err := h.userSrvc.CreateOrGet(&signup)
if err != nil {
2020-10-09 22:37:16 +03:00
respondAlert(w, "failed to create new user", "", conf.SignupTemplate, http.StatusInternalServerError)
2020-05-24 17:34:32 +03:00
return
}
if !created {
2020-10-09 22:37:16 +03:00
respondAlert(w, "user already existing", "", conf.SignupTemplate, http.StatusConflict)
2020-05-24 17:34:32 +03:00
return
}
msg := url.QueryEscape("account created successfully")
http.Redirect(w, r, fmt.Sprintf("%s/?success=%s", h.config.Server.BasePath, msg), http.StatusFound)
2020-05-24 17:34:32 +03:00
}