chore: refactor
This commit is contained in:
parent
daa26bf726
commit
f02ada5639
2 changed files with 39 additions and 38 deletions
|
|
@ -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")
|
||||
}
|
||||
|
|
|
|||
21
plugin/auth/oauth_option.go
Normal file
21
plugin/auth/oauth_option.go
Normal file
|
|
@ -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
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue