mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-27 10:47:06 +02:00
fix(oidc): apply rate limit to fix oocasional oauth state error due to race condition
This commit is contained in:
@@ -18,6 +18,7 @@ import (
|
|||||||
"github.com/yusing/go-proxy/internal/net/gphttp"
|
"github.com/yusing/go-proxy/internal/net/gphttp"
|
||||||
"github.com/yusing/go-proxy/internal/utils"
|
"github.com/yusing/go-proxy/internal/utils"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
|
"golang.org/x/time/rate"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@@ -162,6 +163,8 @@ func (auth *OIDCProvider) HandleAuth(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var rateLimit = rate.NewLimiter(rate.Every(time.Second), 1)
|
||||||
|
|
||||||
func (auth *OIDCProvider) LoginHandler(w http.ResponseWriter, r *http.Request) {
|
func (auth *OIDCProvider) LoginHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
// check for session token
|
// check for session token
|
||||||
sessionToken, err := r.Cookie(CookieOauthSessionToken)
|
sessionToken, err := r.Cookie(CookieOauthSessionToken)
|
||||||
@@ -182,6 +185,11 @@ func (auth *OIDCProvider) LoginHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !rateLimit.Allow() {
|
||||||
|
http.Error(w, "auth rate limit exceeded", http.StatusTooManyRequests)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
state := generateState()
|
state := generateState()
|
||||||
SetTokenCookie(w, r, CookieOauthState, state, 300*time.Second)
|
SetTokenCookie(w, r, CookieOauthState, state, 300*time.Second)
|
||||||
// redirect user to Idp
|
// redirect user to Idp
|
||||||
|
|||||||
Reference in New Issue
Block a user