refactor: make config stateful

This commit is contained in:
Derrick Hammer 2024-02-11 16:33:56 -05:00
parent bbe7e8e053
commit 8dd6e83fae
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 15 additions and 12 deletions

View File

@ -18,6 +18,7 @@ type Oauth struct {
token *oauth2.Token token *oauth2.Token
refresher oauth2.TokenSource refresher oauth2.TokenSource
keepAliveRunning bool keepAliveRunning bool
oauthCfg *oauth2.Config
} }
func NewOauth(lc fx.Lifecycle, cfg *config.Config, logger *zap.Logger) *Oauth { 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 { func (o Oauth) config() *oauth2.Config {
cfg := &oauth2.Config{ if o.oauthCfg == nil {
ClientID: o.cfg.Oauth.ClientId, o.oauthCfg = &oauth2.Config{
ClientSecret: o.cfg.Oauth.ClientSecret, ClientID: o.cfg.Oauth.ClientId,
Scopes: []string{"admin"}, ClientSecret: o.cfg.Oauth.ClientSecret,
RedirectURL: fmt.Sprintf("https://%s/setup/callback", o.cfg.Domain), Scopes: []string{"admin"},
Endpoint: oauth2.Endpoint{ RedirectURL: fmt.Sprintf("https://%s/setup/callback", o.cfg.Domain),
TokenURL: fmt.Sprintf("%s/login/Oauth/access_token", o.cfg.GiteaUrl), Endpoint: oauth2.Endpoint{
AuthURL: fmt.Sprintf("%s/login/Oauth/authorize", o.cfg.GiteaUrl), 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() o.keepAlive()
return cfg return o.oauthCfg
} }
func (o Oauth) authUrl() string { func (o Oauth) authUrl() string {