@@ -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,16 @@ 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 (provider string , credentialsBytes []byte , scopes []string , subject string ) (* jwt.Config , error ) {
165166 switch provider {
166167 case "google" , "googleads" , "googleanalytics" ,
167168 "googledevelopers" , "googlemybusiness" , "googleworkspace" ,
168169 "youtube" , "googleadmin" :
170+ if scopes == nil {
171+ scopes = []string {
172+ "https://www.googleapis.com/auth/cloud-platform" ,
173+ }
174+ }
169175 rv , err := google .JWTConfigFromJSON (credentialsBytes , scopes ... )
170176 if err != nil {
171177 return nil , err
@@ -179,7 +185,31 @@ func getJWTConfig(provider string, credentialsBytes []byte, scopes []string, sub
179185 }
180186}
181187
182- func oauthServiceAccount (
188+ func getGenericClientCredentialsConfig (authCtx * dto.AuthCtx , scopes []string ) (* clientcredentials.Config , error ) {
189+ clientID , clientIDErr := authCtx .GetClientID ()
190+ if clientIDErr != nil {
191+ return nil , clientIDErr
192+ }
193+ clientSecret , secretErr := authCtx .GetClientSecret ()
194+ if secretErr != nil {
195+ return nil , secretErr
196+ }
197+ rv := & clientcredentials.Config {
198+ ClientID : clientID ,
199+ ClientSecret : clientSecret ,
200+ Scopes : scopes ,
201+ TokenURL : authCtx .GetTokenURL (),
202+ }
203+ if len (authCtx .GetValues ()) > 0 {
204+ rv .EndpointParams = authCtx .GetValues ()
205+ }
206+ if authCtx .GetAuthStyle () > 0 {
207+ rv .AuthStyle = oauth2 .AuthStyle (authCtx .GetAuthStyle ())
208+ }
209+ return rv , nil
210+ }
211+
212+ func googleOauthServiceAccount (
183213 provider string ,
184214 authCtx * dto.AuthCtx ,
185215 scopes []string ,
@@ -189,14 +219,27 @@ func oauthServiceAccount(
189219 if err != nil {
190220 return nil , fmt .Errorf ("service account credentials error: %w" , err )
191221 }
192- config , errToken := getJWTConfig (provider , b , scopes , authCtx .Subject )
222+ config , errToken := getGoogleJWTConfig (provider , b , scopes , authCtx .Subject )
193223 if errToken != nil {
194224 return nil , errToken
195225 }
196226 activateAuth (authCtx , "" , dto .AuthServiceAccountStr )
197227 httpClient := netutils .GetHTTPClient (runtimeCtx , http .DefaultClient )
198- //nolint:staticcheck // TODO: fix this
199- return config .Client (context .WithValue (oauth2 .NoContext , oauth2 .HTTPClient , httpClient )), nil
228+ return config .Client (context .WithValue (context .Background (), oauth2 .HTTPClient , httpClient )), nil
229+ }
230+
231+ func genericOauthClientCredentials (
232+ authCtx * dto.AuthCtx ,
233+ scopes []string ,
234+ runtimeCtx dto.RuntimeCtx ,
235+ ) (* http.Client , error ) {
236+ config , errToken := getGenericClientCredentialsConfig (authCtx , scopes )
237+ if errToken != nil {
238+ return nil , errToken
239+ }
240+ activateAuth (authCtx , "" , dto .ClientCredentialsStr )
241+ httpClient := netutils .GetHTTPClient (runtimeCtx , http .DefaultClient )
242+ return config .Client (context .WithValue (context .Background (), oauth2 .HTTPClient , httpClient )), nil
200243}
201244
202245func apiTokenAuth (authCtx * dto.AuthCtx , runtimeCtx dto.RuntimeCtx , enforceBearer bool ) (* http.Client , error ) {
0 commit comments