forked from kyma-project/kyma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.go
72 lines (58 loc) · 2.36 KB
/
factory.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package authorization
import (
"net/http"
"github.com/kyma-project/kyma/components/application-gateway/internal/apperrors"
"github.com/kyma-project/kyma/components/application-gateway/internal/authorization/oauth"
"github.com/kyma-project/kyma/components/application-gateway/internal/authorization/oauth/tokencache"
metadatamodel "github.com/kyma-project/kyma/components/application-gateway/internal/metadata/model"
)
type Strategy interface {
// Adds Authorization header to the request
AddAuthorization(r *http.Request, setter TransportSetter) apperrors.AppError
// Invalidates internal state
Invalidate()
}
type TransportSetter func(transport *http.Transport)
type StrategyFactory interface {
// Creates strategy for credentials provided
Create(credentials *metadatamodel.Credentials) Strategy
}
type OAuthClient interface {
// GetToken obtains OAuth token
GetToken(clientID string, clientSecret string, authURL string) (string, apperrors.AppError)
// InvalidateTokenCache resets internal token cache
InvalidateTokenCache(clientID string)
}
type authorizationStrategyFactory struct {
oauthClient OAuthClient
}
// Create creates strategy for credentials provided
func (asf authorizationStrategyFactory) Create(c *metadatamodel.Credentials) Strategy {
var strategy Strategy
if c != nil && c.OAuth != nil {
strategy = newOAuthStrategy(asf.oauthClient, c.OAuth.ClientID, c.OAuth.ClientSecret, c.OAuth.URL)
} else if c != nil && c.BasicAuth != nil {
strategy = newBasicAuthStrategy(c.BasicAuth.Username, c.BasicAuth.Password)
} else if c != nil && c.CertificateGen != nil {
strategy = newCertificateGenStrategy(c.CertificateGen.Certificate, c.CertificateGen.PrivateKey)
} else {
strategy = newNoAuthStrategy()
}
var headers *map[string][]string
var queryParams *map[string][]string
if c != nil {
headers = c.Headers
queryParams = c.QueryParameters
}
return newExternalTokenStrategy(strategy, headers, queryParams)
}
// FactoryConfiguration holds factory configuration options
type FactoryConfiguration struct {
OAuthClientTimeout int
}
// NewStrategyFactory creates factory for instantiating Strategy implementations
func NewStrategyFactory(config FactoryConfiguration) StrategyFactory {
cache := tokencache.NewTokenCache()
oauthClient := oauth.NewOauthClient(config.OAuthClientTimeout, cache)
return authorizationStrategyFactory{oauthClient: oauthClient}
}