From 8dd6e83faeb8408db64cdd9501064ca11748f74c Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sun, 11 Feb 2024 16:33:56 -0500 Subject: [PATCH] refactor: make config stateful --- api/oauth.go | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/api/oauth.go b/api/oauth.go index 420f5a0..22abd5e 100644 --- a/api/oauth.go +++ b/api/oauth.go @@ -18,6 +18,7 @@ type Oauth struct { token *oauth2.Token refresher oauth2.TokenSource keepAliveRunning bool + oauthCfg *oauth2.Config } func NewOauth(lc fx.Lifecycle, cfg *config.Config, logger *zap.Logger) *Oauth { @@ -35,21 +36,23 @@ func NewOauth(lc fx.Lifecycle, cfg *config.Config, logger *zap.Logger) *Oauth { } func (o Oauth) config() *oauth2.Config { - cfg := &oauth2.Config{ - ClientID: o.cfg.Oauth.ClientId, - ClientSecret: o.cfg.Oauth.ClientSecret, - Scopes: []string{"admin"}, - RedirectURL: fmt.Sprintf("https://%s/setup/callback", o.cfg.Domain), - Endpoint: oauth2.Endpoint{ - TokenURL: fmt.Sprintf("%s/login/Oauth/access_token", o.cfg.GiteaUrl), - AuthURL: fmt.Sprintf("%s/login/Oauth/authorize", o.cfg.GiteaUrl), - }, - } + if o.oauthCfg == nil { + o.oauthCfg = &oauth2.Config{ + ClientID: o.cfg.Oauth.ClientId, + ClientSecret: o.cfg.Oauth.ClientSecret, + Scopes: []string{"admin"}, + RedirectURL: fmt.Sprintf("https://%s/setup/callback", o.cfg.Domain), + Endpoint: oauth2.Endpoint{ + TokenURL: fmt.Sprintf("%s/login/Oauth/access_token", o.cfg.GiteaUrl), + AuthURL: fmt.Sprintf("%s/login/Oauth/authorize", o.cfg.GiteaUrl), + }, + } - o.loadToken(cfg) + } + o.loadToken(o.oauthCfg) o.keepAlive() - return cfg + return o.oauthCfg } func (o Oauth) authUrl() string {