@@ -18,6 +18,7 @@ import (
1818 "context"
1919 "crypto/x509"
2020 "fmt"
21+ "log"
2122 "net/http"
2223 "os"
2324 "strings"
@@ -439,16 +440,80 @@ func (b *CommandConfigOauth) GetServerConfig() *Server {
439440 return & server
440441}
441442
443+ // GetAccessToken returns the OAuth2 token source for the given configuration.
444+ func (b * CommandConfigOauth ) GetAccessToken () (oauth2.TokenSource , error ) {
445+ if b == nil {
446+ return nil , fmt .Errorf ("CommandConfigOauth is nil" )
447+ }
448+
449+ b .ValidateAuthConfig ()
450+
451+ if b .AccessToken != "" && (b .ClientID == "" || b .ClientSecret == "" || b .TokenURL == "" ) {
452+ log .Printf ("[DEBUG] Access token is explicitly set, and no client credentials are provided. Using static token source." )
453+ return oauth2 .StaticTokenSource (
454+ & oauth2.Token {
455+ AccessToken : b .AccessToken ,
456+ TokenType : DefaultTokenPrefix ,
457+ Expiry : b .Expiry ,
458+ },
459+ ), nil
460+ }
461+
462+ log .Printf ("[DEBUG] Getting OAuth2 token source for client ID: %s" , b .ClientID )
463+ if b .ClientID == "" || b .ClientSecret == "" || b .TokenURL == "" {
464+ return nil , fmt .Errorf ("client ID, client secret, and token URL must be provided" )
465+ }
466+
467+ config := & clientcredentials.Config {
468+ ClientID : b .ClientID ,
469+ ClientSecret : b .ClientSecret ,
470+ TokenURL : b .TokenURL ,
471+ Scopes : b .Scopes ,
472+ }
473+
474+ if b .Audience != "" {
475+ log .Printf ("[DEBUG] Setting audience for OAuth2 token source: %s" , b .Audience )
476+ config .EndpointParams = map [string ][]string {
477+ "audience" : {b .Audience },
478+ }
479+ }
480+
481+ ctx := context .Background ()
482+ log .Printf ("[DEBUG] Returning call config.TokenSource() for client ID: %s" , b .ClientID )
483+ tokenSource := config .TokenSource (ctx )
484+ if tokenSource == nil {
485+ return nil , fmt .Errorf ("failed to create token source for client ID: %s" , b .ClientID )
486+ }
487+ token , tErr := tokenSource .Token ()
488+ if tErr != nil {
489+ return nil , fmt .Errorf ("failed to retrieve token for client ID %s: %w" , b .ClientID , tErr )
490+ }
491+ if token == nil || token .AccessToken == "" {
492+ return nil , fmt .Errorf ("received empty OAuth token for client ID: %s" , b .ClientID )
493+ }
494+
495+ return tokenSource , nil
496+ }
497+
442498// RoundTrip executes a single HTTP transaction, adding the OAuth2 token to the request
443499func (t * oauth2Transport ) RoundTrip (req * http.Request ) (* http.Response , error ) {
500+ log .Printf ("[DEBUG] Attempting to get oAuth token from: %s %s" , req .Method , req .URL )
444501 token , err := t .src .Token ()
445502 if err != nil {
503+
446504 return nil , fmt .Errorf ("failed to retrieve OAuth token: %w" , err )
447505 }
448506
507+ if token == nil || token .AccessToken == "" {
508+ return nil , fmt .Errorf ("received empty OAuth token" )
509+ }
510+
449511 // Clone the request to avoid mutating the original
512+ log .Printf ("[DEBUG] Adding oAuth token to request: %s %s" , req .Method , req .URL )
450513 reqCopy := req .Clone (req .Context ())
451514 token .SetAuthHeader (reqCopy )
515+ requestCurlStr , _ := RequestToCurl (reqCopy )
516+ log .Printf ("[TRACE] curl command: %s" , requestCurlStr )
452517
453518 return t .base .RoundTrip (reqCopy )
454519}
0 commit comments