2023-09-09 01:38:10 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2023-09-24 18:24:16 +01:00
|
|
|
"fmt"
|
2023-09-09 01:38:10 +01:00
|
|
|
"github.com/1f349/tulip/database"
|
|
|
|
"github.com/1f349/tulip/pages"
|
2023-09-24 18:24:16 +01:00
|
|
|
"github.com/emersion/go-message/mail"
|
2023-09-09 01:38:10 +01:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
2023-09-24 18:24:16 +01:00
|
|
|
"log"
|
2023-09-09 01:38:10 +01:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2023-09-24 18:24:16 +01:00
|
|
|
"time"
|
2023-09-09 01:38:10 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func (h *HttpServer) LoginGet(rw http.ResponseWriter, req *http.Request, _ httprouter.Params, auth UserAuth) {
|
|
|
|
if !auth.IsGuest() {
|
|
|
|
h.SafeRedirect(rw, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rw.Header().Set("Content-Type", "text/html")
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
pages.RenderPageTemplate(rw, "login", map[string]any{
|
2023-09-15 13:06:31 +01:00
|
|
|
"ServiceName": h.serviceName,
|
|
|
|
"Redirect": req.URL.Query().Get("redirect"),
|
2023-09-24 18:24:16 +01:00
|
|
|
"Mismatch": req.URL.Query().Get("mismatch"),
|
2023-09-09 01:38:10 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HttpServer) LoginPost(rw http.ResponseWriter, req *http.Request, _ httprouter.Params, auth UserAuth) {
|
|
|
|
un := req.FormValue("username")
|
|
|
|
pw := req.FormValue("password")
|
2023-09-24 18:24:16 +01:00
|
|
|
|
|
|
|
// flags returned from database call
|
|
|
|
var userInfo *database.User
|
|
|
|
var loginMismatch byte
|
2023-09-09 01:38:10 +01:00
|
|
|
var hasOtp bool
|
2023-09-24 18:24:16 +01:00
|
|
|
|
2023-09-09 01:38:10 +01:00
|
|
|
if h.DbTx(rw, func(tx *database.Tx) error {
|
2023-09-24 18:24:16 +01:00
|
|
|
loginUser, hasOtpRaw, hasVerifiedEmail, err := tx.CheckLogin(un, pw)
|
2023-09-09 01:38:10 +01:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, sql.ErrNoRows) || errors.Is(err, bcrypt.ErrMismatchedHashAndPassword) {
|
2023-09-24 18:24:16 +01:00
|
|
|
loginMismatch = 1
|
2023-09-09 01:38:10 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
http.Error(rw, "Internal server error", http.StatusInternalServerError)
|
|
|
|
return err
|
|
|
|
}
|
2023-09-24 18:24:16 +01:00
|
|
|
|
|
|
|
userInfo = loginUser
|
2023-09-09 01:38:10 +01:00
|
|
|
hasOtp = hasOtpRaw
|
2023-09-24 18:24:16 +01:00
|
|
|
if !hasVerifiedEmail {
|
|
|
|
loginMismatch = 2
|
|
|
|
}
|
2023-09-09 01:38:10 +01:00
|
|
|
return nil
|
|
|
|
}) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-24 18:24:16 +01:00
|
|
|
if loginMismatch != 0 {
|
|
|
|
originUrl, err := url.Parse(req.FormValue("redirect"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, "400 Bad Request: Invalid redirect URL", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// send verify email
|
|
|
|
if loginMismatch == 2 {
|
|
|
|
// parse email for headers
|
|
|
|
address, err := mail.ParseAddress(userInfo.Email)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, "500 Internal Server Error: Failed to parse user email address", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
u := uuid.New()
|
|
|
|
h.mailLinkCache.Set(mailLinkKey{mailLinkVerifyEmail, u}, userInfo.Sub, time.Now().Add(10*time.Minute))
|
|
|
|
|
|
|
|
// try to send email
|
|
|
|
err = h.mailer.SendEmailTemplate("mail-verify", "Verify Email", userInfo.Name, address, map[string]any{
|
|
|
|
"VerifyUrl": h.domain + "/mail/verify/" + u.String(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Println("[Tulip] Login: Failed to send verification email:", err)
|
|
|
|
http.Error(rw, "500 Internal Server Error: Failed to send verification email", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// send email successfully, hope the user actually receives it
|
|
|
|
}
|
|
|
|
|
|
|
|
redirectUrl := PrepareRedirectUrl(fmt.Sprintf("/login?mismatch=%d", loginMismatch), originUrl)
|
|
|
|
http.Redirect(rw, req, redirectUrl.String(), http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-09 01:38:10 +01:00
|
|
|
// only continues if the above tx succeeds
|
|
|
|
auth.Data = SessionData{
|
2023-09-24 18:24:16 +01:00
|
|
|
ID: userInfo.Sub,
|
2023-09-09 01:38:10 +01:00
|
|
|
NeedOtp: hasOtp,
|
|
|
|
}
|
|
|
|
if auth.SaveSessionData() != nil {
|
|
|
|
http.Error(rw, "Failed to save session", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasOtp {
|
|
|
|
originUrl, err := url.Parse(req.FormValue("redirect"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, "400 Bad Request: Invalid redirect URL", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
redirectUrl := PrepareRedirectUrl("/login/otp", originUrl)
|
|
|
|
http.Redirect(rw, req, redirectUrl.String(), http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
h.SafeRedirect(rw, req)
|
|
|
|
}
|
2023-09-29 16:37:23 +01:00
|
|
|
|
|
|
|
func (h *HttpServer) LoginResetPasswordPost(rw http.ResponseWriter, req *http.Request, params httprouter.Params) {
|
|
|
|
email := req.PostFormValue("email")
|
|
|
|
address, err := mail.ParseAddress(email)
|
|
|
|
if err != nil || address.Name != "" {
|
|
|
|
http.Error(rw, "Invalid email address format", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var emailExists bool
|
|
|
|
if h.DbTx(rw, func(tx *database.Tx) (err error) {
|
|
|
|
emailExists, err = tx.UserEmailExists(email)
|
|
|
|
return err
|
|
|
|
}) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go h.possiblySendPasswordResetEmail(email, emailExists)
|
|
|
|
|
|
|
|
http.Error(rw, "An email will be send to your inbox if an account with that email address is found", http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HttpServer) possiblySendPasswordResetEmail(email string, exists bool) {
|
|
|
|
// TODO(Melon): Send reset password email template
|
|
|
|
}
|