/
auth.go
32 lines (27 loc) · 973 Bytes
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package auth
import (
"context"
"net/http"
"github.com/SAP/sap-btp-service-operator/internal/httputil"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
)
// HTTPClient interface
//
//go:generate counterfeiter . HTTPClient
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
func NewAuthClient(ccConfig *clientcredentials.Config, sslDisabled bool) HTTPClient {
httpClient := httputil.BuildHTTPClient(sslDisabled)
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, httpClient)
return oauth2.NewClient(ctx, ccConfig.TokenSource(ctx))
}
func NewAuthClientWithTLS(ccConfig *clientcredentials.Config, tlsCertKey, tlsPrivateKey string) (HTTPClient, error) {
httpClient, err := httputil.BuildHTTPClientTLS(tlsCertKey, tlsPrivateKey)
if err != nil {
return nil, err
}
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, httpClient)
return oauth2.NewClient(ctx, ccConfig.TokenSource(ctx)), nil
}