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

     1  package systemfetcher
     2  
     3  import (
     4  	"net/http"
     5  	"net/url"
     6  	"strings"
     7  
     8  	"github.com/kyma-incubator/compass/components/director/pkg/auth"
     9  	"github.com/kyma-incubator/compass/components/director/pkg/oauth"
    10  )
    11  
    12  type oauthMtlsClient struct {
    13  	clientID     string
    14  	tokenURL     string
    15  	scopesClaim  string
    16  	tenantHeader string
    17  	certCache    auth.CertificateCache
    18  
    19  	c *http.Client
    20  }
    21  
    22  // NewOauthMtlsClient missing docs
    23  func NewOauthMtlsClient(oauthCfg oauth.Config, certCache auth.CertificateCache, client *http.Client) *oauthMtlsClient {
    24  	protocol := oauthCfg.TokenEndpointProtocol + "://"
    25  	tokenParsedURL, err := url.Parse(oauthCfg.TokenBaseURL)
    26  	if err == nil && len(tokenParsedURL.Scheme) != 0 {
    27  		protocol = ""
    28  	}
    29  	return &oauthMtlsClient{
    30  		clientID:     oauthCfg.ClientID,
    31  		certCache:    certCache,
    32  		tokenURL:     protocol + oauthCfg.TokenBaseURL + oauthCfg.TokenPath,
    33  		scopesClaim:  strings.Join(oauthCfg.ScopesClaim, " "),
    34  		tenantHeader: oauthCfg.TenantHeaderName,
    35  		c:            client,
    36  	}
    37  }
    38  
    39  // Do missing docs
    40  func (omc *oauthMtlsClient) Do(req *http.Request, tenant string) (*http.Response, error) {
    41  	req = req.WithContext(auth.SaveToContext(req.Context(), &auth.OAuthMtlsCredentials{
    42  		ClientID:          omc.clientID,
    43  		CertCache:         omc.certCache,
    44  		TokenURL:          omc.tokenURL,
    45  		Scopes:            omc.scopesClaim,
    46  		AdditionalHeaders: map[string]string{omc.tenantHeader: tenant},
    47  	}))
    48  
    49  	return omc.c.Do(req)
    50  }