-
Notifications
You must be signed in to change notification settings - Fork 6
fix: improve authentication flow and session handling #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -18,7 +18,7 @@ var templateFS embed.FS | |||||||||||||||||||
| type AuthRouter struct { | ||||||||||||||||||||
| passwordHash []string | ||||||||||||||||||||
| providers []Provider | ||||||||||||||||||||
| template *template.Template | ||||||||||||||||||||
| loginTemplate *template.Template | ||||||||||||||||||||
| unauthorizedTemplate *template.Template | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -36,7 +36,7 @@ func NewAuthRouter(passwordHash []string, providers ...Provider) (*AuthRouter, e | |||||||||||||||||||
| return &AuthRouter{ | ||||||||||||||||||||
| passwordHash: passwordHash, | ||||||||||||||||||||
| providers: providers, | ||||||||||||||||||||
| template: tmpl, | ||||||||||||||||||||
| loginTemplate: tmpl, | ||||||||||||||||||||
| unauthorizedTemplate: unauthorizedTmpl, | ||||||||||||||||||||
| }, nil | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
@@ -82,11 +82,28 @@ func (a *AuthRouter) SetupRoutes(router gin.IRouter) { | |||||||||||||||||||
| c.Error(err) | ||||||||||||||||||||
| return | ||||||||||||||||||||
| } | ||||||||||||||||||||
| ok, err := provider.Authorization(userID) | ||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||
| c.Error(err) | ||||||||||||||||||||
| return | ||||||||||||||||||||
| } | ||||||||||||||||||||
| if !ok { | ||||||||||||||||||||
| a.renderUnauthorized(c, userID, provider.Name()) | ||||||||||||||||||||
| return | ||||||||||||||||||||
| } | ||||||||||||||||||||
| session.Set(SessionKeyProvider, provider.Name()) | ||||||||||||||||||||
| session.Set(SessionKeyUserID, userID) | ||||||||||||||||||||
| session.Save() | ||||||||||||||||||||
| redirectURL := session.Get(SessionKeyRedirectURL) | ||||||||||||||||||||
| c.Redirect(http.StatusFound, redirectURL.(string)) | ||||||||||||||||||||
| if redirectURL != nil { | ||||||||||||||||||||
| session.Delete(SessionKeyRedirectURL) | ||||||||||||||||||||
| } | ||||||||||||||||||||
| session.Save() | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if redirectURL == nil { | ||||||||||||||||||||
| c.Redirect(http.StatusFound, "/") | ||||||||||||||||||||
| } else { | ||||||||||||||||||||
| c.Redirect(http.StatusFound, redirectURL.(string)) | ||||||||||||||||||||
| } | ||||||||||||||||||||
| }) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| router.GET(provider.AuthURL(), func(c *gin.Context) { | ||||||||||||||||||||
|
|
@@ -118,29 +135,12 @@ func (a *AuthRouter) getProvider(name string) Provider { | |||||||||||||||||||
| return nil | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| type templateData struct { | ||||||||||||||||||||
| Providers []Provider | ||||||||||||||||||||
| HasPassword bool | ||||||||||||||||||||
| PasswordError string | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| func (a *AuthRouter) handleLogin(c *gin.Context) { | ||||||||||||||||||||
| if c.Request.Method == "POST" { | ||||||||||||||||||||
| a.handleLoginPost(c) | ||||||||||||||||||||
| return | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| data := templateData{ | ||||||||||||||||||||
| Providers: a.providers, | ||||||||||||||||||||
| HasPassword: len(a.passwordHash) > 0, | ||||||||||||||||||||
| PasswordError: "", | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| c.Header("Content-Type", "text/html; charset=utf-8") | ||||||||||||||||||||
| if err := a.template.Execute(c.Writer, data); err != nil { | ||||||||||||||||||||
| c.AbortWithError(http.StatusInternalServerError, err) | ||||||||||||||||||||
| return | ||||||||||||||||||||
| } | ||||||||||||||||||||
| a.renderLogin(c, "") | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| func (a *AuthRouter) handleLoginPost(c *gin.Context) { | ||||||||||||||||||||
|
|
@@ -165,39 +165,32 @@ func (a *AuthRouter) handleLoginPost(c *gin.Context) { | |||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if errorMessage != "" { | ||||||||||||||||||||
| data := templateData{ | ||||||||||||||||||||
| Providers: a.providers, | ||||||||||||||||||||
| HasPassword: len(a.passwordHash) > 0, | ||||||||||||||||||||
| PasswordError: errorMessage, | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| c.Header("Content-Type", "text/html; charset=utf-8") | ||||||||||||||||||||
| c.Status(http.StatusBadRequest) | ||||||||||||||||||||
| if err := a.template.Execute(c.Writer, data); err != nil { | ||||||||||||||||||||
| c.AbortWithError(http.StatusInternalServerError, err) | ||||||||||||||||||||
| return | ||||||||||||||||||||
| } | ||||||||||||||||||||
| a.renderLogin(c, errorMessage) | ||||||||||||||||||||
| return | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| session := sessions.Default(c) | ||||||||||||||||||||
| session.Set(SessionKeyProvider, PasswordProvider) | ||||||||||||||||||||
| session.Set(SessionKeyUserID, PasswordUserID) | ||||||||||||||||||||
|
||||||||||||||||||||
| session.Set(SessionKeyUserID, PasswordUserID) | |
| session.Set(SessionKeyUserID, PasswordUserID) | |
| a.handleRedirectAfterLogin(c, session) | |
| } | |
| // handleRedirectAfterLogin handles redirecting the user after successful login, | |
| // using the redirect URL stored in the session if present. | |
| func (a *AuthRouter) handleRedirectAfterLogin(c *gin.Context, session sessions.Session) { |
Copilot
AI
Aug 20, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same issue as in the OAuth callback: the redirect URL is deleted and session saved before the redirect logic. If session.Save() fails, the redirect URL would be lost but the redirect might not happen correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The redirect URL is deleted from the session before checking if it's nil, but the check happens after the session is saved. This could cause issues if the session save fails. Consider moving the session.Save() call after the redirect logic.