refactor: remove redirect URL from session cookie

This commit is contained in:
Stavros
2025-04-14 19:42:52 +03:00
parent 476a455329
commit 0761c2f5c1
5 changed files with 14 additions and 14 deletions

View File

@@ -95,6 +95,8 @@ var rootCmd = &cobra.Command{
DisableContinue: config.DisableContinue, DisableContinue: config.DisableContinue,
Title: config.Title, Title: config.Title,
GenericName: config.GenericName, GenericName: config.GenericName,
CookieSecure: config.CookieSecure,
Domain: domain,
} }
// Create api config // Create api config

View File

@@ -178,7 +178,6 @@ func (auth *Auth) CreateSessionCookie(c *gin.Context, data *types.SessionCookie)
session.Values["provider"] = data.Provider session.Values["provider"] = data.Provider
session.Values["expiry"] = time.Now().Add(time.Duration(sessionExpiry) * time.Second).Unix() session.Values["expiry"] = time.Now().Add(time.Duration(sessionExpiry) * time.Second).Unix()
session.Values["totpPending"] = data.TotpPending session.Values["totpPending"] = data.TotpPending
session.Values["redirectURI"] = data.RedirectURI
// Save session // Save session
err = session.Save(c.Request, c.Writer) err = session.Save(c.Request, c.Writer)
@@ -230,11 +229,10 @@ func (auth *Auth) GetSessionCookie(c *gin.Context) (types.SessionCookie, error)
// Get data from session // Get data from session
username, usernameOk := session.Values["username"].(string) username, usernameOk := session.Values["username"].(string)
provider, providerOK := session.Values["provider"].(string) provider, providerOK := session.Values["provider"].(string)
redirectURI, redirectOK := session.Values["redirectURI"].(string)
expiry, expiryOk := session.Values["expiry"].(int64) expiry, expiryOk := session.Values["expiry"].(int64)
totpPending, totpPendingOk := session.Values["totpPending"].(bool) totpPending, totpPendingOk := session.Values["totpPending"].(bool)
if !usernameOk || !providerOK || !expiryOk || !redirectOK || !totpPendingOk { if !usernameOk || !providerOK || !expiryOk || !totpPendingOk {
log.Warn().Msg("Session cookie is missing data") log.Warn().Msg("Session cookie is missing data")
return types.SessionCookie{}, nil return types.SessionCookie{}, nil
} }
@@ -257,7 +255,6 @@ func (auth *Auth) GetSessionCookie(c *gin.Context) (types.SessionCookie, error)
Username: username, Username: username,
Provider: provider, Provider: provider,
TotpPending: totpPending, TotpPending: totpPending,
RedirectURI: redirectURI,
}, nil }, nil
} }

View File

@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"strings" "strings"
"time"
"tinyauth/internal/auth" "tinyauth/internal/auth"
"tinyauth/internal/docker" "tinyauth/internal/docker"
"tinyauth/internal/hooks" "tinyauth/internal/hooks"
@@ -525,9 +526,7 @@ func (h *Handlers) OauthUrlHandler(c *gin.Context) {
// Set redirect cookie if redirect URI is provided // Set redirect cookie if redirect URI is provided
if redirectURI != "" { if redirectURI != "" {
log.Debug().Str("redirectURI", redirectURI).Msg("Setting redirect cookie") log.Debug().Str("redirectURI", redirectURI).Msg("Setting redirect cookie")
h.Auth.CreateSessionCookie(c, &types.SessionCookie{ c.SetCookie("tinyauth-redirect", redirectURI, int(time.Hour.Seconds()), "/", "", h.Config.CookieSecure, true)
RedirectURI: redirectURI,
})
} }
// Return auth URL // Return auth URL
@@ -623,25 +622,26 @@ func (h *Handlers) OauthCallbackHandler(c *gin.Context) {
log.Debug().Msg("Email whitelisted") log.Debug().Msg("Email whitelisted")
// Get redirect URI
cookie, err := h.Auth.GetSessionCookie(c)
// Create session cookie (also cleans up redirect cookie) // Create session cookie (also cleans up redirect cookie)
h.Auth.CreateSessionCookie(c, &types.SessionCookie{ h.Auth.CreateSessionCookie(c, &types.SessionCookie{
Username: email, Username: email,
Provider: providerName.Provider, Provider: providerName.Provider,
}) })
// If it is empty it means that no redirect_uri was provided to the login screen so we just log in // Check if we have a redirect URI
redirectCookie, err := c.Cookie("tinyauth-redirect")
if err != nil { if err != nil {
log.Debug().Msg("No redirect cookie")
c.Redirect(http.StatusPermanentRedirect, h.Config.AppURL) c.Redirect(http.StatusPermanentRedirect, h.Config.AppURL)
return
} }
log.Debug().Str("redirectURI", cookie.RedirectURI).Msg("Got redirect URI") log.Debug().Str("redirectURI", redirectCookie).Msg("Got redirect URI")
// Build query // Build query
queries, err := query.Values(types.LoginQuery{ queries, err := query.Values(types.LoginQuery{
RedirectURI: cookie.RedirectURI, RedirectURI: redirectCookie,
}) })
log.Debug().Msg("Got redirect query") log.Debug().Msg("Got redirect query")

View File

@@ -37,6 +37,8 @@ type Config struct {
// Server configuration // Server configuration
type HandlersConfig struct { type HandlersConfig struct {
AppURL string AppURL string
Domain string
CookieSecure bool
DisableContinue bool DisableContinue bool
GenericName string GenericName string
Title string Title string

View File

@@ -27,7 +27,6 @@ type SessionCookie struct {
Username string Username string
Provider string Provider string
TotpPending bool TotpPending bool
RedirectURI string
} }
// TinyauthLabels is the labels for the tinyauth container // TinyauthLabels is the labels for the tinyauth container