github.com/Jeffail/benthos/v3@v3.65.0/lib/util/http/auth/oauth2.go (about) 1 package auth 2 3 import ( 4 "context" 5 "net/http" 6 7 "golang.org/x/oauth2/clientcredentials" 8 ) 9 10 //------------------------------------------------------------------------------ 11 12 // OAuth2Config holds the configuration parameters for an OAuth2 exchange. 13 type OAuth2Config struct { 14 Enabled bool `json:"enabled" yaml:"enabled"` 15 ClientKey string `json:"client_key" yaml:"client_key"` 16 ClientSecret string `json:"client_secret" yaml:"client_secret"` 17 TokenURL string `json:"token_url" yaml:"token_url"` 18 Scopes []string `json:"scopes" yaml:"scopes"` 19 } 20 21 // NewOAuth2Config returns a new OAuth2Config with default values. 22 func NewOAuth2Config() OAuth2Config { 23 return OAuth2Config{ 24 Enabled: false, 25 ClientKey: "", 26 ClientSecret: "", 27 TokenURL: "", 28 Scopes: []string{}, 29 } 30 } 31 32 //------------------------------------------------------------------------------ 33 34 // Client returns an http.Client with OAuth2 configured. 35 func (oauth OAuth2Config) Client(ctx context.Context) *http.Client { 36 if !oauth.Enabled { 37 var client http.Client 38 return &client 39 } 40 41 conf := &clientcredentials.Config{ 42 ClientID: oauth.ClientKey, 43 ClientSecret: oauth.ClientSecret, 44 TokenURL: oauth.TokenURL, 45 Scopes: oauth.Scopes, 46 } 47 48 return conf.Client(ctx) 49 }