refactor: make config stateful
This commit is contained in:
parent
bbe7e8e053
commit
8dd6e83fae
27
api/oauth.go
27
api/oauth.go
|
@ -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 {
|
||||||
|
|
Loading…
Reference in New Issue