@@ -15,6 +15,7 @@ import (
1515 "regexp"
1616
1717 "golang.org/x/oauth2"
18+ "golang.org/x/oauth2/clientcredentials"
1819 "golang.org/x/oauth2/google"
1920 "golang.org/x/oauth2/jwt"
2021)
@@ -161,11 +162,21 @@ func parseServiceAccountFile(ac *dto.AuthCtx) (serviceAccount, error) {
161162 return c , json .Unmarshal (b , & c )
162163}
163164
164- func getJWTConfig (provider string , credentialsBytes []byte , scopes []string , subject string ) (* jwt.Config , error ) {
165+ func getGoogleJWTConfig (
166+ provider string ,
167+ credentialsBytes []byte ,
168+ scopes []string ,
169+ subject string ,
170+ ) (* jwt.Config , error ) {
165171 switch provider {
166172 case "google" , "googleads" , "googleanalytics" ,
167173 "googledevelopers" , "googlemybusiness" , "googleworkspace" ,
168174 "youtube" , "googleadmin" :
175+ if scopes == nil {
176+ scopes = []string {
177+ "https://www.googleapis.com/auth/cloud-platform" ,
178+ }
179+ }
169180 rv , err := google .JWTConfigFromJSON (credentialsBytes , scopes ... )
170181 if err != nil {
171182 return nil , err
@@ -179,7 +190,31 @@ func getJWTConfig(provider string, credentialsBytes []byte, scopes []string, sub
179190 }
180191}
181192
182- func oauthServiceAccount (
193+ func getGenericClientCredentialsConfig (authCtx * dto.AuthCtx , scopes []string ) (* clientcredentials.Config , error ) {
194+ clientID , clientIDErr := authCtx .GetClientID ()
195+ if clientIDErr != nil {
196+ return nil , clientIDErr
197+ }
198+ clientSecret , secretErr := authCtx .GetClientSecret ()
199+ if secretErr != nil {
200+ return nil , secretErr
201+ }
202+ rv := & clientcredentials.Config {
203+ ClientID : clientID ,
204+ ClientSecret : clientSecret ,
205+ Scopes : scopes ,
206+ TokenURL : authCtx .GetTokenURL (),
207+ }
208+ if len (authCtx .GetValues ()) > 0 {
209+ rv .EndpointParams = authCtx .GetValues ()
210+ }
211+ if authCtx .GetAuthStyle () > 0 {
212+ rv .AuthStyle = oauth2 .AuthStyle (authCtx .GetAuthStyle ())
213+ }
214+ return rv , nil
215+ }
216+
217+ func googleOauthServiceAccount (
183218 provider string ,
184219 authCtx * dto.AuthCtx ,
185220 scopes []string ,
@@ -189,14 +224,27 @@ func oauthServiceAccount(
189224 if err != nil {
190225 return nil , fmt .Errorf ("service account credentials error: %w" , err )
191226 }
192- config , errToken := getJWTConfig (provider , b , scopes , authCtx .Subject )
227+ config , errToken := getGoogleJWTConfig (provider , b , scopes , authCtx .Subject )
193228 if errToken != nil {
194229 return nil , errToken
195230 }
196231 activateAuth (authCtx , "" , dto .AuthServiceAccountStr )
197232 httpClient := netutils .GetHTTPClient (runtimeCtx , http .DefaultClient )
198- //nolint:staticcheck // TODO: fix this
199- return config .Client (context .WithValue (oauth2 .NoContext , oauth2 .HTTPClient , httpClient )), nil
233+ return config .Client (context .WithValue (context .Background (), oauth2 .HTTPClient , httpClient )), nil
234+ }
235+
236+ func genericOauthClientCredentials (
237+ authCtx * dto.AuthCtx ,
238+ scopes []string ,
239+ runtimeCtx dto.RuntimeCtx ,
240+ ) (* http.Client , error ) {
241+ config , errToken := getGenericClientCredentialsConfig (authCtx , scopes )
242+ if errToken != nil {
243+ return nil , errToken
244+ }
245+ activateAuth (authCtx , "" , dto .ClientCredentialsStr )
246+ httpClient := netutils .GetHTTPClient (runtimeCtx , http .DefaultClient )
247+ return config .Client (context .WithValue (context .Background (), oauth2 .HTTPClient , httpClient )), nil
200248}
201249
202250func apiTokenAuth (authCtx * dto.AuthCtx , runtimeCtx dto.RuntimeCtx , enforceBearer bool ) (* http.Client , error ) {
0 commit comments