@@ -20,32 +20,32 @@ import (
20
20
"golang.org/x/oauth2/google"
21
21
)
22
22
23
- // An OAuth2Provider represents a supported oauth provider.
24
- type OAuth2Provider string
23
+ // An oauth2Provider represents a supported oauth provider.
24
+ type oauth2Provider string
25
25
26
26
const (
27
- OAuth2ProviderDefault OAuth2Provider = "" // OAuth2ProviderDefault means no specific provider is set.
28
- OAuth2ProviderAzure OAuth2Provider = "azure" // OAuth2ProviderAzure AzureAD.
29
- OAuth2ProviderGoogle OAuth2Provider = "google" // OAuth2ProviderGoogle Google.
27
+ oauth2ProviderDefault oauth2Provider = "" // OAuth2ProviderDefault means no specific provider is set.
28
+ oauth2ProviderAzure oauth2Provider = "azure" // OAuth2ProviderAzure AzureAD.
29
+ oauth2ProviderGoogle oauth2Provider = "google" // OAuth2ProviderGoogle Google.
30
30
)
31
31
32
- func (p * OAuth2Provider ) Unpack (in string ) error {
33
- * p = OAuth2Provider (in )
32
+ func (p * oauth2Provider ) Unpack (in string ) error {
33
+ * p = oauth2Provider (in )
34
34
return nil
35
35
}
36
36
37
- func (p OAuth2Provider ) canonical () OAuth2Provider {
38
- return OAuth2Provider (strings .ToLower (string (p )))
37
+ func (p oauth2Provider ) canonical () oauth2Provider {
38
+ return oauth2Provider (strings .ToLower (string (p )))
39
39
}
40
40
41
- // OAuth2 contains information about oauth2 authentication settings.
42
- type OAuth2 struct {
41
+ // oauth2Config contains information about oauth2 authentication settings.
42
+ type oauth2Config struct {
43
43
// common oauth fields
44
44
ClientID string `config:"client.id"`
45
45
ClientSecret string `config:"client.secret"`
46
46
Enabled * bool `config:"enabled"`
47
47
EndpointParams map [string ][]string `config:"endpoint_params"`
48
- Provider OAuth2Provider `config:"provider"`
48
+ Provider oauth2Provider `config:"provider"`
49
49
Scopes []string `config:"scopes"`
50
50
TokenURL string `config:"token_url"`
51
51
@@ -61,25 +61,26 @@ type OAuth2 struct {
61
61
}
62
62
63
63
// IsEnabled returns true if the `enable` field is set to true in the yaml.
64
- func (o * OAuth2 ) IsEnabled () bool {
64
+ func (o * oauth2Config ) isEnabled () bool {
65
65
return o != nil && (o .Enabled == nil || * o .Enabled )
66
66
}
67
67
68
68
// Client wraps the given http.Client and returns a new one that will use the oauth authentication.
69
- func (o * OAuth2 ) Client (ctx context.Context , client * http.Client ) (* http.Client , error ) {
70
- ctx = context .WithValue (ctx , oauth2 .HTTPClient , client )
69
+ func (o * oauth2Config ) client (ctx context.Context , client * http.Client ) (* http.Client , error ) {
70
+ // only required to let oauth2 library to find our custom client in the context
71
+ ctx = context .WithValue (context .Background (), oauth2 .HTTPClient , client )
71
72
72
- switch o .GetProvider () {
73
- case OAuth2ProviderAzure , OAuth2ProviderDefault :
73
+ switch o .getProvider () {
74
+ case oauth2ProviderAzure , oauth2ProviderDefault :
74
75
creds := clientcredentials.Config {
75
76
ClientID : o .ClientID ,
76
77
ClientSecret : o .ClientSecret ,
77
- TokenURL : o .GetTokenURL (),
78
+ TokenURL : o .getTokenURL (),
78
79
Scopes : o .Scopes ,
79
- EndpointParams : o .GetEndpointParams (),
80
+ EndpointParams : o .getEndpointParams (),
80
81
}
81
82
return creds .Client (ctx ), nil
82
- case OAuth2ProviderGoogle :
83
+ case oauth2ProviderGoogle :
83
84
if o .GoogleJWTFile != "" {
84
85
cfg , err := google .JWTConfigFromJSON (o .GoogleCredentialsJSON , o .Scopes ... )
85
86
if err != nil {
@@ -100,9 +101,9 @@ func (o *OAuth2) Client(ctx context.Context, client *http.Client) (*http.Client,
100
101
}
101
102
102
103
// GetTokenURL returns the TokenURL.
103
- func (o * OAuth2 ) GetTokenURL () string {
104
- switch o .GetProvider () {
105
- case OAuth2ProviderAzure :
104
+ func (o * oauth2Config ) getTokenURL () string {
105
+ switch o .getProvider () {
106
+ case oauth2ProviderAzure :
106
107
if o .TokenURL == "" {
107
108
return endpoints .AzureAD (o .AzureTenantID ).TokenURL
108
109
}
@@ -112,14 +113,14 @@ func (o *OAuth2) GetTokenURL() string {
112
113
}
113
114
114
115
// GetProvider returns provider in its canonical form.
115
- func (o OAuth2 ) GetProvider () OAuth2Provider {
116
+ func (o oauth2Config ) getProvider () oauth2Provider {
116
117
return o .Provider .canonical ()
117
118
}
118
119
119
120
// GetEndpointParams returns endpoint params with any provider ones combined.
120
- func (o OAuth2 ) GetEndpointParams () map [string ][]string {
121
- switch o .GetProvider () {
122
- case OAuth2ProviderAzure :
121
+ func (o oauth2Config ) getEndpointParams () map [string ][]string {
122
+ switch o .getProvider () {
123
+ case oauth2ProviderAzure :
123
124
if o .AzureResource != "" {
124
125
if o .EndpointParams == nil {
125
126
o .EndpointParams = map [string ][]string {}
@@ -132,26 +133,26 @@ func (o OAuth2) GetEndpointParams() map[string][]string {
132
133
}
133
134
134
135
// Validate checks if oauth2 config is valid.
135
- func (o * OAuth2 ) Validate () error {
136
- switch o .GetProvider () {
137
- case OAuth2ProviderAzure :
136
+ func (o * oauth2Config ) Validate () error {
137
+ switch o .getProvider () {
138
+ case oauth2ProviderAzure :
138
139
return o .validateAzureProvider ()
139
- case OAuth2ProviderGoogle :
140
+ case oauth2ProviderGoogle :
140
141
return o .validateGoogleProvider ()
141
- case OAuth2ProviderDefault :
142
+ case oauth2ProviderDefault :
142
143
if o .TokenURL == "" || o .ClientID == "" || o .ClientSecret == "" {
143
144
return errors .New ("invalid configuration: both token_url and client credentials must be provided" )
144
145
}
145
146
default :
146
- return fmt .Errorf ("invalid configuration: unknown provider %q" , o .GetProvider ())
147
+ return fmt .Errorf ("invalid configuration: unknown provider %q" , o .getProvider ())
147
148
}
148
149
return nil
149
150
}
150
151
151
152
// findDefaultGoogleCredentials will default to google.FindDefaultCredentials and will only be changed for testing purposes
152
153
var findDefaultGoogleCredentials = google .FindDefaultCredentials
153
154
154
- func (o * OAuth2 ) validateGoogleProvider () error {
155
+ func (o * oauth2Config ) validateGoogleProvider () error {
155
156
if o .TokenURL != "" || o .ClientID != "" || o .ClientSecret != "" ||
156
157
o .AzureTenantID != "" || o .AzureResource != "" || len (o .EndpointParams ) > 0 {
157
158
return errors .New ("invalid configuration: none of token_url and client credentials can be used, use google.credentials_file, google.jwt_file, google.credentials_json or ADC instead" )
@@ -191,7 +192,7 @@ func (o *OAuth2) validateGoogleProvider() error {
191
192
return fmt .Errorf ("invalid configuration: no authentication credentials were configured or detected (ADC)" )
192
193
}
193
194
194
- func (o * OAuth2 ) populateCredentialsJSONFromFile (file string ) error {
195
+ func (o * oauth2Config ) populateCredentialsJSONFromFile (file string ) error {
195
196
if _ , err := os .Stat (file ); os .IsNotExist (err ) {
196
197
return fmt .Errorf ("invalid configuration: the file %q cannot be found" , file )
197
198
}
@@ -210,7 +211,7 @@ func (o *OAuth2) populateCredentialsJSONFromFile(file string) error {
210
211
return nil
211
212
}
212
213
213
- func (o * OAuth2 ) validateAzureProvider () error {
214
+ func (o * oauth2Config ) validateAzureProvider () error {
214
215
if o .TokenURL == "" && o .AzureTenantID == "" {
215
216
return errors .New ("invalid configuration: at least one of token_url or tenant_id must be provided" )
216
217
}
0 commit comments