Reworked the login flow

It is looking neat, but I still have to make sure it behaves how
I want it to behave
This commit is contained in:
2024-06-10 22:27:46 +01:00
parent fd8f3720fb
commit 0c1dd26df4
8 changed files with 106 additions and 80 deletions
+74 -45
View File
@@ -7,6 +7,7 @@ import (
"net/http"
"thermalFax/models"
"thermalFax/scache"
"thermalFax/views"
"time"
"github.com/golang-jwt/jwt/v4"
@@ -71,7 +72,63 @@ func createSessionCookie(token string) *http.Cookie {
return &sessionCookie
}
func createNewSession(w http.ResponseWriter, loginForm models.LoginForm) {
authCookie, err := createAuthCookie(loginForm)
if err != nil {
log.Printf("Error: %s", err)
return
}
http.SetCookie(w, authCookie)
sessionCookie := createSessionCookie(authCookie.Value)
http.SetCookie(w, sessionCookie)
// Store the auth token in the database
err = models.StoreAuthToken(authCookie.Value, loginForm.Username, authCookie.Expires.Unix())
if err != nil {
log.Println("Failed to store auth token: ", err)
return
}
// Store the session token in the cache
err = models.StoreSessionToken(authCookie.Value)
if err != nil {
log.Println("Failed to store session token: ", err)
return
}
log.Printf(`| {"user": %s, "time": %d}`, loginForm.Username, time.Now().Unix())
w.Header().Add("HX-Redirect", "/editor")
}
func updateSession(w http.ResponseWriter, loginForm models.LoginForm,
sessionCookie *http.Cookie) {
localUserSession := models.GetSession(sessionCookie.Value)
if localUserSession == nil {
// Just create a new session in this case
log.Println("| User has session key not present locally")
createNewSession(w, loginForm)
return
}
expirationTime := time.Now().Add(SESSION_INTERVAL)
cookie := http.Cookie{
Name: "token",
Value: sessionCookie.Value,
Expires: expirationTime,
}
http.SetCookie(w, &cookie)
log.Println("| User is being sent to ", localUserSession.Wants)
w.Header().Set("HX-Redirect", localUserSession.Wants)
}
func Authenticate(w http.ResponseWriter, r *http.Request) {
// DELETE THIS AFTER TESTING AND SO ON
time.Sleep(1 * time.Second)
// first we have to verify if this request brings any login data
r.ParseForm()
loginForm := models.LoginForm{
@@ -81,8 +138,7 @@ func Authenticate(w http.ResponseWriter, r *http.Request) {
if !models.ValidateCredentials(&loginForm) {
log.Println("| Invalid credentials.")
w.Header().Set("Content-Type", "text/json")
w.Write([]byte("{\"state\":\"wrong credentials\"}"))
w.Header().Add("HX-Trigger", `{"wrongCredentials":"Wrong Credentials"}`)
return
}
@@ -96,53 +152,14 @@ func Authenticate(w http.ResponseWriter, r *http.Request) {
// If there is an active session we update the current session
if userReqSession {
// Update current session
localUserSession := models.GetSession(sessionCookie.Value)
if localUserSession == nil {
log.Panicln("| But what happened should not have happened. Blame the user")
}
expirationTime := time.Now().Add(SESSION_INTERVAL)
cookie := http.Cookie{
Name: "token",
Value: sessionCookie.Value,
Expires: expirationTime,
}
http.SetCookie(w, &cookie)
w.Header().Set("HX-Redirect", localUserSession.Wants)
updateSession(w, loginForm, sessionCookie)
} else {
// Create a new session
authCookie, err := createAuthCookie(loginForm)
if err != nil {
log.Printf("Error: %s", err)
return
}
http.SetCookie(w, authCookie)
sessionCookie := createSessionCookie(authCookie.Value)
http.SetCookie(w, sessionCookie)
// Store the auth token in the database
err = models.StoreAuthToken(authCookie.Value, loginForm.Username, authCookie.Expires.Unix())
if err != nil {
log.Println("Failed to store auth token: ", err)
return
}
// Store the session token in the cache
err = models.StoreSessionToken(authCookie.Value)
if err != nil {
log.Println("Failed to store session token: ", err)
return
}
log.Printf(`| {"user": %s, "time": %d}`, loginForm.Username, time.Now().Unix())
w.Header().Add("HX-Redirect", "/editor")
createNewSession(w, loginForm)
}
}
func reauth(w http.ResponseWriter, r *http.Request) {
func saveSessionState(r *http.Request) {
cache := scache.GetInstance()
cookie := getToken(r, "fax-session")
if cookie == nil {
@@ -158,7 +175,19 @@ func reauth(w http.ResponseWriter, r *http.Request) {
}
cache.Data[tok].Wants = r.RequestURI
}
log.Println("| User wants to go to ", cache.Data[tok].Wants)
}
}
func reauth(w http.ResponseWriter, r *http.Request) {
log.Println("| User wants to request something but authentication has expired")
saveSessionState(r)
w.Header().Set("HX-Redirect", "/login")
w.Header().Set("HX-Push-Url", "true")
}
func relogin(w http.ResponseWriter, r *http.Request) {
saveSessionState(r)
views.LoginPage(w, r)
}