@@ -8,13 +8,13 @@ import (
88 "fmt"
99 "log"
1010 "net/http"
11- "sync"
1211 "time"
1312
1413 "github.com/gin-gonic/gin"
1514 "github.com/gotify/server/v2/auth"
1615 "github.com/gotify/server/v2/config"
1716 "github.com/gotify/server/v2/database"
17+ "github.com/gotify/server/v2/decaymap"
1818 "github.com/gotify/server/v2/model"
1919 "github.com/zitadel/oidc/v3/pkg/client/rp"
2020 httphelper "github.com/zitadel/oidc/v3/pkg/http"
@@ -60,7 +60,7 @@ func NewOIDC(conf *config.Configuration, db *database.GormDatabase, userChangeNo
6060 PasswordStrength : conf .PassStrength ,
6161 SecureCookie : conf .Server .SecureCookie ,
6262 AutoRegister : conf .OIDC .AutoRegister ,
63- pendingSessions : make ( map [string ] * pendingOIDCSession ),
63+ pendingSessions : decaymap . NewDecayMap [string , * pendingOIDCSession ]( time . Now (), pendingSessionMaxAge ),
6464 }
6565}
6666
@@ -81,32 +81,7 @@ type OIDCAPI struct {
8181 PasswordStrength int
8282 SecureCookie bool
8383 AutoRegister bool
84- pendingSessions map [string ]* pendingOIDCSession
85- pendingSessionsMu sync.Mutex
86- }
87-
88- func (a * OIDCAPI ) storePendingSession (state string , session * pendingOIDCSession ) {
89- a .pendingSessionsMu .Lock ()
90- defer a .pendingSessionsMu .Unlock ()
91- for s , sess := range a .pendingSessions {
92- if time .Since (sess .CreatedAt ) > pendingSessionMaxAge {
93- delete (a .pendingSessions , s )
94- }
95- }
96- a .pendingSessions [state ] = session
97- }
98-
99- func (a * OIDCAPI ) popPendingSession (state string ) (* pendingOIDCSession , bool ) {
100- a .pendingSessionsMu .Lock ()
101- session , ok := a .pendingSessions [state ]
102- if ok {
103- delete (a .pendingSessions , state )
104- }
105- a .pendingSessionsMu .Unlock ()
106- if ! ok || time .Since (session .CreatedAt ) > pendingSessionMaxAge {
107- return nil , false
108- }
109- return session , true
84+ pendingSessions * decaymap.DecayMap [string , * pendingOIDCSession ]
11085}
11186
11287// swagger:operation GET /auth/oidc/login oidc oidcLogin
@@ -142,7 +117,7 @@ func (a *OIDCAPI) LoginHandler() gin.HandlerFunc {
142117 http .Error (w , fmt .Sprintf ("failed to generate state: %v" , err ), http .StatusInternalServerError )
143118 return
144119 }
145- a .storePendingSession ( state , & pendingOIDCSession {ClientName : clientName , CreatedAt : time .Now ()})
120+ a .pendingSessions . Set ( time . Now (), state , & pendingOIDCSession {ClientName : clientName , CreatedAt : time .Now ()})
146121 rp .AuthURLHandler (func () string { return state }, a .Provider )(w , r )
147122 })
148123}
@@ -237,7 +212,7 @@ func (a *OIDCAPI) ExternalAuthorizeHandler(ctx *gin.Context) {
237212 ctx .AbortWithError (http .StatusInternalServerError , err )
238213 return
239214 }
240- a .storePendingSession ( state , & pendingOIDCSession {
215+ a .pendingSessions . Set ( time . Now (), state , & pendingOIDCSession {
241216 RedirectURI : req .RedirectURI , ClientName : req .Name , CreatedAt : time .Now (),
242217 })
243218 authOpts := []rp.AuthURLOpt {
@@ -364,3 +339,11 @@ func (a *OIDCAPI) createClient(name string, userID uint) (*model.Client, error)
364339 }
365340 return client , a .DB .CreateClient (client )
366341}
342+
343+ func (a * OIDCAPI ) popPendingSession (key string ) (* pendingOIDCSession , bool ) {
344+ session , ok := a .pendingSessions .Pop (key )
345+ if ok && time .Since (session .CreatedAt ) < pendingSessionMaxAge {
346+ return session , true
347+ }
348+ return nil , false
349+ }
0 commit comments