-
Notifications
You must be signed in to change notification settings - Fork 408
/
oauthcertstrategy.go
56 lines (46 loc) · 1.91 KB
/
oauthcertstrategy.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package authorization
import (
"fmt"
"net/http"
"go.uber.org/zap"
"github.com/kyma-project/kyma/components/central-application-gateway/pkg/httpconsts"
"github.com/kyma-project/kyma/components/central-application-gateway/pkg/apperrors"
"github.com/kyma-project/kyma/components/central-application-gateway/pkg/authorization/clientcert"
)
type oauthWithCertStrategy struct {
oauthClient OAuthClient
clientId string
clientSecret string
certificate []byte
privateKey []byte
url string
requestParameters *RequestParameters
tokenRequestSkipVerify bool
}
func newOAuthWithCertStrategy(oauthClient OAuthClient, clientId string, clientSecret string, certificate, privateKey []byte, url string, requestParameters *RequestParameters) oauthWithCertStrategy {
return oauthWithCertStrategy{
oauthClient: oauthClient,
clientId: clientId,
clientSecret: clientSecret,
certificate: certificate,
privateKey: privateKey,
url: url,
requestParameters: requestParameters,
}
}
func (o oauthWithCertStrategy) AddAuthorization(r *http.Request, _ clientcert.SetClientCertificateFunc, skipTLSVerification bool) apperrors.AppError {
zap.L().Info("passing skipTLSVerification to GetTokenMTLS",
zap.Bool("skipTLSVerification", skipTLSVerification))
headers, queryParameters := o.requestParameters.unpack()
token, err := o.oauthClient.GetTokenMTLS(o.clientId, o.url, o.certificate, o.privateKey, headers, queryParameters, skipTLSVerification)
if err != nil {
zap.L().Error("failed to get token",
zap.Error(err))
return apperrors.Internal("Failed to get token: %s", err.Error())
}
r.Header.Set(httpconsts.HeaderAuthorization, fmt.Sprintf("Bearer %s", token))
return nil
}
func (o oauthWithCertStrategy) Invalidate() {
o.oauthClient.InvalidateTokenCacheMTLS(o.clientId, o.url, o.certificate, o.privateKey)
}