From f02ada56397c1769332a1a40d0449ba74084f008 Mon Sep 17 00:00:00 2001 From: andig Date: Sun, 16 Nov 2025 15:32:27 +0100 Subject: [PATCH] chore: refactor --- plugin/auth/oauth.go | 56 ++++++++++++------------------------- plugin/auth/oauth_option.go | 21 ++++++++++++++ 2 files changed, 39 insertions(+), 38 deletions(-) create mode 100644 plugin/auth/oauth_option.go diff --git a/plugin/auth/oauth.go b/plugin/auth/oauth.go index f4525ff4c..b7bad8152 100644 --- a/plugin/auth/oauth.go +++ b/plugin/auth/oauth.go @@ -21,41 +21,8 @@ import ( "golang.org/x/oauth2" ) -type OAuth struct { - mu sync.Mutex - log *util.Logger - oc *oauth2.Config - token *oauth2.Token - name string - devices []string - subject string - cv string - ctx context.Context - onlineC chan<- bool - - deviceFlow bool - tokenRetriever func(string, *oauth2.Token) error - tokenStorer func(*oauth2.Token) any -} - -type oauthOption func(*OAuth) - -func WithOauthDeviceFlowOption() func(o *OAuth) { - return func(o *OAuth) { - o.deviceFlow = true - } -} - -func WithTokenStorerOption(ts func(*oauth2.Token) any) func(o *OAuth) { - return func(o *OAuth) { - o.tokenStorer = ts - } -} - -func WithTokenRetrieverOption(tr func(string, *oauth2.Token) error) func(o *OAuth) { - return func(o *OAuth) { - o.tokenRetriever = tr - } +func init() { + registry.AddCtx("oauth", NewOauthFromConfig) } var ( @@ -71,8 +38,21 @@ func addInstance(subject string, identity *OAuth) { identities[subject] = identity } -func init() { - registry.AddCtx("oauth", NewOauthFromConfig) +type OAuth struct { + mu sync.Mutex + log *util.Logger + oc *oauth2.Config + token *oauth2.Token + name string + devices []string + subject string + cv string + ctx context.Context + onlineC chan<- bool + + deviceFlow bool + tokenRetriever func(string, *oauth2.Token) error + tokenStorer func(*oauth2.Token) any } func NewOauthFromConfig(ctx context.Context, other map[string]any) (oauth2.TokenSource, error) { @@ -91,7 +71,7 @@ func NewOauthFromConfig(ctx context.Context, other map[string]any) (oauth2.Token var _ api.AuthProvider = (*OAuth)(nil) var _ oauth2.TokenSource = (*OAuth)(nil) -func NewOauth(ctx context.Context, name, device string, oc *oauth2.Config, opts ...oauthOption) (*OAuth, error) { +func NewOauth(ctx context.Context, name, device string, oc *oauth2.Config, opts ...func(o *OAuth)) (*OAuth, error) { if name == "" { return nil, errors.New("instance name must not be empty") } diff --git a/plugin/auth/oauth_option.go b/plugin/auth/oauth_option.go new file mode 100644 index 000000000..6845248a8 --- /dev/null +++ b/plugin/auth/oauth_option.go @@ -0,0 +1,21 @@ +package auth + +import "golang.org/x/oauth2" + +func WithOauthDeviceFlowOption() func(o *OAuth) { + return func(o *OAuth) { + o.deviceFlow = true + } +} + +func WithTokenStorerOption(ts func(*oauth2.Token) any) func(o *OAuth) { + return func(o *OAuth) { + o.tokenStorer = ts + } +} + +func WithTokenRetrieverOption(tr func(string, *oauth2.Token) error) func(o *OAuth) { + return func(o *OAuth) { + o.tokenRetriever = tr + } +}