From 077641beaa58871c14e51ad0b2853643d6f8f414 Mon Sep 17 00:00:00 2001 From: yusing Date: Tue, 22 Apr 2025 16:49:12 +0800 Subject: [PATCH] refactor(oidc): simplify exchange method --- internal/api/v1/auth/oidc.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/internal/api/v1/auth/oidc.go b/internal/api/v1/auth/oidc.go index 8baa1f0..0fda97f 100644 --- a/internal/api/v1/auth/oidc.go +++ b/internal/api/v1/auth/oidc.go @@ -182,13 +182,18 @@ func (auth *OIDCProvider) RedirectLoginPage(w http.ResponseWriter, r *http.Reque http.Redirect(w, r, redirURL, http.StatusTemporaryRedirect) } +func (auth *OIDCProvider) cloneConfig() *oauth2.Config { + cfg := *auth.oauthConfig + return &cfg +} + func (auth *OIDCProvider) exchange(r *http.Request) (*oauth2.Token, error) { + var cfg *oauth2.Config if auth.isMiddleware { - cfg := *auth.oauthConfig + cfg = auth.cloneConfig() cfg.RedirectURL = "https://" + r.Host + OIDCMiddlewareCallbackPath - return cfg.Exchange(r.Context(), r.URL.Query().Get("code")) } - return auth.oauthConfig.Exchange(r.Context(), r.URL.Query().Get("code")) + return cfg.Exchange(r.Context(), r.URL.Query().Get("code")) } // OIDCCallbackHandler handles the OIDC callback.