github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/systemfetcher/oauth_client.go (about)

     1  package systemfetcher
     2  
     3  import (
     4  	"net/http"
     5  	"strings"
     6  
     7  	"github.com/kyma-incubator/compass/components/director/pkg/auth"
     8  	"github.com/kyma-incubator/compass/components/director/pkg/oauth"
     9  )
    10  
    11  type oauthClient struct {
    12  	clientID     string
    13  	tokenURL     string
    14  	scopesClaim  string
    15  	tenantHeader string
    16  
    17  	c *http.Client
    18  }
    19  
    20  // NewOauthClient missing docs
    21  func NewOauthClient(oauthCfg oauth.Config, client *http.Client) *oauthClient {
    22  	return &oauthClient{
    23  		clientID:     oauthCfg.ClientID,
    24  		tokenURL:     oauthCfg.TokenEndpointProtocol + "://" + oauthCfg.TokenBaseURL + oauthCfg.TokenPath,
    25  		scopesClaim:  strings.Join(oauthCfg.ScopesClaim, " "),
    26  		tenantHeader: oauthCfg.TenantHeaderName,
    27  		c:            client,
    28  	}
    29  }
    30  
    31  // Do missing docs
    32  func (oc *oauthClient) Do(req *http.Request, tenant string) (*http.Response, error) {
    33  	req = req.WithContext(auth.SaveToContext(req.Context(), &auth.OAuthCredentials{
    34  		ClientID:          oc.clientID,
    35  		TokenURL:          oc.tokenURL,
    36  		Scopes:            oc.scopesClaim,
    37  		AdditionalHeaders: map[string]string{oc.tenantHeader: tenant},
    38  	}))
    39  
    40  	return oc.c.Do(req)
    41  }