2023-07-24 15:17:15 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2023-07-25 22:48:58 +00:00
|
|
|
"dev.tandem.ws/tandem/camper/pkg/database"
|
2023-07-24 15:17:15 +00:00
|
|
|
"dev.tandem.ws/tandem/camper/pkg/form"
|
|
|
|
httplib "dev.tandem.ws/tandem/camper/pkg/http"
|
|
|
|
"dev.tandem.ws/tandem/camper/pkg/locale"
|
|
|
|
"dev.tandem.ws/tandem/camper/pkg/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
sessionCookie = "camper-session"
|
|
|
|
)
|
|
|
|
|
|
|
|
type loginForm struct {
|
|
|
|
Email *form.Input
|
|
|
|
Password *form.Input
|
|
|
|
Error error
|
|
|
|
}
|
|
|
|
|
|
|
|
func newLoginForm() *loginForm {
|
|
|
|
return &loginForm{
|
|
|
|
Email: &form.Input{
|
|
|
|
Name: "email",
|
|
|
|
},
|
|
|
|
Password: &form.Input{
|
|
|
|
Name: "password",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *loginForm) Parse(r *http.Request) error {
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.Email.FillValue(r)
|
|
|
|
f.Password.FillValue(r)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *loginForm) Valid(l *locale.Locale) bool {
|
|
|
|
v := form.NewValidator(l)
|
|
|
|
if v.CheckRequired(f.Email, l.GettextNoop("Email can not be empty.")) {
|
|
|
|
v.CheckValidEmail(f.Email, l.GettextNoop("This email is not valid. It should be like name@domain.com."))
|
|
|
|
}
|
|
|
|
v.CheckRequired(f.Password, l.GettextNoop("Password can not be empty."))
|
|
|
|
return v.AllOK
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *App) handleGet(w http.ResponseWriter, r *http.Request) {
|
|
|
|
l := h.matchLocale(r)
|
|
|
|
login := newLoginForm()
|
|
|
|
template.MustRender(w, l, "login.gohtml", login)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *App) matchLocale(r *http.Request) *locale.Locale {
|
|
|
|
return locale.Match(r, h.locales, h.defaultLocale, h.languageMatcher)
|
|
|
|
}
|
|
|
|
|
2023-07-25 22:48:58 +00:00
|
|
|
func (h *App) handleLogin(w http.ResponseWriter, r *http.Request, conn *database.Conn) {
|
2023-07-24 15:17:15 +00:00
|
|
|
login := newLoginForm()
|
|
|
|
if err := login.Parse(r); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
l := h.matchLocale(r)
|
|
|
|
if login.Valid(l) {
|
|
|
|
cookie := conn.MustGetText(r.Context(), "select login($1, $2, $3)", login.Email, login.Password, httplib.RemoteAddr(r))
|
|
|
|
if cookie != "" {
|
|
|
|
setSessionCookie(w, cookie)
|
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
login.Error = errors.New(l.Gettext("Invalid user or password."))
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusUnprocessableEntity)
|
|
|
|
}
|
|
|
|
template.MustRender(w, l, "login.gohtml", login)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setSessionCookie(w http.ResponseWriter, cookie string) {
|
|
|
|
http.SetCookie(w, createSessionCookie(cookie, 8766*24*time.Hour))
|
|
|
|
}
|
|
|
|
|
2023-07-25 22:48:58 +00:00
|
|
|
func getSessionCookie(r *http.Request) string {
|
|
|
|
if cookie, err := r.Cookie(sessionCookie); err == nil {
|
|
|
|
return cookie.Value
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2023-07-24 15:17:15 +00:00
|
|
|
func createSessionCookie(value string, duration time.Duration) *http.Cookie {
|
|
|
|
return &http.Cookie{
|
|
|
|
Name: sessionCookie,
|
|
|
|
Value: value,
|
|
|
|
Path: "/",
|
|
|
|
Expires: time.Now().Add(duration),
|
|
|
|
HttpOnly: true,
|
|
|
|
SameSite: http.SameSiteLaxMode,
|
|
|
|
}
|
|
|
|
}
|