-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathoauth.go
More file actions
304 lines (245 loc) · 11.9 KB
/
oauth.go
File metadata and controls
304 lines (245 loc) · 11.9 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package validation
import (
"fmt"
"strings"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/fielderrors"
"github.com/openshift/origin/pkg/auth/authenticator/password/ldappassword"
"github.com/openshift/origin/pkg/cmd/server/api"
"github.com/openshift/origin/pkg/cmd/server/api/latest"
"github.com/openshift/origin/pkg/user/api/validation"
)
func ValidateOAuthConfig(config *api.OAuthConfig) ValidationResults {
validationResults := ValidationResults{}
if len(config.MasterURL) == 0 {
validationResults.AddErrors(fielderrors.NewFieldRequired("masterURL"))
}
if _, urlErrs := ValidateURL(config.MasterPublicURL, "masterPublicURL"); len(urlErrs) > 0 {
validationResults.AddErrors(urlErrs...)
}
if len(config.AssetPublicURL) == 0 {
validationResults.AddErrors(fielderrors.NewFieldRequired("assetPublicURL"))
}
if config.SessionConfig != nil {
validationResults.AddErrors(ValidateSessionConfig(config.SessionConfig).Prefix("sessionConfig")...)
}
validationResults.AddErrors(ValidateGrantConfig(config.GrantConfig).Prefix("grantConfig")...)
providerNames := util.NewStringSet()
redirectingIdentityProviders := []string{}
for i, identityProvider := range config.IdentityProviders {
if identityProvider.UseAsLogin {
redirectingIdentityProviders = append(redirectingIdentityProviders, identityProvider.Name)
if api.IsPasswordAuthenticator(identityProvider) {
if config.SessionConfig == nil {
validationResults.AddErrors(fielderrors.NewFieldInvalid("sessionConfig", config, "sessionConfig is required if a password identity provider is used for browser based login"))
}
}
}
validationResults.Append(ValidateIdentityProvider(identityProvider).Prefix(fmt.Sprintf("identityProvider[%d]", i)))
if len(identityProvider.Name) > 0 {
if providerNames.Has(identityProvider.Name) {
validationResults.AddErrors(fielderrors.NewFieldInvalid(fmt.Sprintf("identityProvider[%d].name", i), identityProvider.Name, "must have a unique name"))
}
providerNames.Insert(identityProvider.Name)
}
}
if len(redirectingIdentityProviders) > 1 {
validationResults.AddErrors(fielderrors.NewFieldInvalid("identityProviders", config.IdentityProviders, fmt.Sprintf("only one identity provider can support login for a browser, found: %v", redirectingIdentityProviders)))
}
return validationResults
}
func ValidateIdentityProvider(identityProvider api.IdentityProvider) ValidationResults {
validationResults := ValidationResults{}
if len(identityProvider.Name) == 0 {
validationResults.AddErrors(fielderrors.NewFieldRequired("name"))
}
if ok, err := validation.ValidateIdentityProviderName(identityProvider.Name); !ok {
validationResults.AddErrors(fielderrors.NewFieldInvalid("name", identityProvider.Name, err))
}
if !api.IsIdentityProviderType(identityProvider.Provider) {
validationResults.AddErrors(fielderrors.NewFieldInvalid("provider", identityProvider.Provider, fmt.Sprintf("%v is invalid in this context", identityProvider.Provider)))
} else {
switch provider := identityProvider.Provider.Object.(type) {
case (*api.RequestHeaderIdentityProvider):
validationResults.AddErrors(ValidateRequestHeaderIdentityProvider(provider, identityProvider)...)
case (*api.BasicAuthPasswordIdentityProvider):
validationResults.AddErrors(ValidateRemoteConnectionInfo(provider.RemoteConnectionInfo).Prefix("provider")...)
case (*api.HTPasswdPasswordIdentityProvider):
validationResults.AddErrors(ValidateFile(provider.File, "provider.file")...)
case (*api.LDAPPasswordIdentityProvider):
validationResults.Append(ValidateLDAPIdentityProvider(provider, identityProvider))
case (*api.GitHubIdentityProvider):
validationResults.AddErrors(ValidateOAuthIdentityProvider(provider.ClientID, provider.ClientSecret, identityProvider.UseAsChallenger)...)
case (*api.GoogleIdentityProvider):
validationResults.AddErrors(ValidateOAuthIdentityProvider(provider.ClientID, provider.ClientSecret, identityProvider.UseAsChallenger)...)
case (*api.OpenIDIdentityProvider):
validationResults.AddErrors(ValidateOpenIDIdentityProvider(provider, identityProvider)...)
}
}
return validationResults
}
func ValidateLDAPIdentityProvider(provider *api.LDAPPasswordIdentityProvider, identityProvider api.IdentityProvider) ValidationResults {
validationResults := ValidationResults{}
if len(provider.URL) == 0 {
validationResults.AddErrors(fielderrors.NewFieldRequired("provider.url"))
return validationResults
}
u, err := ldappassword.ParseURL(provider.URL)
if err != nil {
validationResults.AddErrors(fielderrors.NewFieldInvalid("provider.url", provider.URL, err.Error()))
return validationResults
}
// Make sure bindDN and bindPassword are both set, or both unset
// Both unset means an anonymous bind is used for search (https://tools.ietf.org/html/rfc4513#section-5.1.1)
// Both set means the name/password simple bind is used for search (https://tools.ietf.org/html/rfc4513#section-5.1.3)
if (len(provider.BindDN) == 0) != (len(provider.BindPassword) == 0) {
validationResults.AddErrors(fielderrors.NewFieldInvalid("provider.bindDN", provider.BindDN, "bindDN and bindPassword must both be specified, or both be empty"))
validationResults.AddErrors(fielderrors.NewFieldInvalid("provider.bindPassword", "<masked>", "bindDN and bindPassword must both be specified, or both be empty"))
}
if provider.Insecure {
if u.Scheme == ldappassword.SchemeLDAPS {
validationResults.AddErrors(fielderrors.NewFieldInvalid("provider.url", provider.URL, fmt.Sprintf("Cannot use %s scheme with insecure=true", u.Scheme)))
}
if len(provider.CA) > 0 {
validationResults.AddErrors(fielderrors.NewFieldInvalid("provider.ca", provider.CA, "Cannot specify a ca with insecure=true"))
}
} else {
if len(provider.CA) > 0 {
validationResults.AddErrors(ValidateFile(provider.CA, "provider.ca")...)
}
}
// At least one attribute to use as the user id is required
if len(provider.Attributes.ID) == 0 {
validationResults.AddErrors(fielderrors.NewFieldInvalid("provider.attributes.id", "[]", "at least one id attribute is required (LDAP standard identity attribute is 'dn')"))
}
// Warn if insecure
if provider.Insecure {
validationResults.AddWarnings(fielderrors.NewFieldInvalid("provider.insecure", provider.Insecure, "validating passwords over an insecure connection could allow them to be intercepted"))
}
return validationResults
}
func ValidateRequestHeaderIdentityProvider(provider *api.RequestHeaderIdentityProvider, identityProvider api.IdentityProvider) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
if len(provider.ClientCA) > 0 {
allErrs = append(allErrs, ValidateFile(provider.ClientCA, "provider.clientCA")...)
}
if len(provider.Headers) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired("provider.headers"))
}
if identityProvider.UseAsChallenger {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("challenge", identityProvider.UseAsChallenger, "request header providers cannot be used for challenges"))
}
if identityProvider.UseAsLogin {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("login", identityProvider.UseAsChallenger, "request header providers cannot be used for browser login"))
}
return allErrs
}
func ValidateOAuthIdentityProvider(clientID, clientSecret string, challenge bool) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
if len(clientID) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired("provider.clientID"))
}
if len(clientSecret) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired("provider.clientSecret"))
}
if challenge {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("challenge", challenge, "oauth providers cannot be used for challenges"))
}
return allErrs
}
func ValidateOpenIDIdentityProvider(provider *api.OpenIDIdentityProvider, identityProvider api.IdentityProvider) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
allErrs = append(allErrs, ValidateOAuthIdentityProvider(provider.ClientID, provider.ClientSecret, identityProvider.UseAsChallenger)...)
// Communication with the Authorization Endpoint MUST utilize TLS
// http://openid.net/specs/openid-connect-core-1_0.html#AuthorizationEndpoint
_, urlErrs := ValidateSecureURL(provider.URLs.Authorize, "authorize")
allErrs = append(allErrs, urlErrs.Prefix("provider.urls")...)
// Communication with the Token Endpoint MUST utilize TLS
// http://openid.net/specs/openid-connect-core-1_0.html#TokenEndpoint
_, urlErrs = ValidateSecureURL(provider.URLs.Token, "token")
allErrs = append(allErrs, urlErrs.Prefix("provider.urls")...)
if len(provider.URLs.UserInfo) != 0 {
// Communication with the UserInfo Endpoint MUST utilize TLS
// http://openid.net/specs/openid-connect-core-1_0.html#UserInfo
_, urlErrs = ValidateSecureURL(provider.URLs.UserInfo, "userInfo")
allErrs = append(allErrs, urlErrs.Prefix("provider.urls")...)
}
// At least one claim to use as the user id is required
if len(provider.Claims.ID) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("provider.claims.id", "[]", "at least one id claim is required (OpenID standard identity claim is 'sub')"))
}
if len(provider.CA) != 0 {
allErrs = append(allErrs, ValidateFile(provider.CA, "provider.ca")...)
}
return allErrs
}
func ValidateGrantConfig(config api.GrantConfig) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
if !api.ValidGrantHandlerTypes.Has(string(config.Method)) {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("grantConfig.method", config.Method, fmt.Sprintf("must be one of: %v", api.ValidGrantHandlerTypes.List())))
}
return allErrs
}
func ValidateSessionConfig(config *api.SessionConfig) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
// Validate session secrets file, if specified
if len(config.SessionSecretsFile) > 0 {
fileErrs := ValidateFile(config.SessionSecretsFile, "sessionSecretsFile")
if len(fileErrs) != 0 {
// Missing file
allErrs = append(allErrs, fileErrs...)
} else {
// Validate file contents
secrets, err := latest.ReadSessionSecrets(config.SessionSecretsFile)
if err != nil {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("sessionSecretsFile", config.SessionSecretsFile, fmt.Sprintf("error reading file: %v", err)))
} else {
for _, err := range ValidateSessionSecrets(secrets) {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("sessionSecretsFile", config.SessionSecretsFile, err.Error()))
}
}
}
}
if len(config.SessionName) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired("sessionName"))
}
return allErrs
}
func ValidateSessionSecrets(config *api.SessionSecrets) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
if len(config.Secrets) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired("secrets"))
}
for i, secret := range config.Secrets {
switch {
case len(secret.Authentication) == 0:
allErrs = append(allErrs, fielderrors.NewFieldRequired(fmt.Sprintf("secrets[%d].authentication", i)))
case len(secret.Authentication) < 32:
// Don't output current value in error message... we don't want it logged
allErrs = append(allErrs,
fielderrors.NewFieldInvalid(
fmt.Sprintf("secrets[%d].authentpsecretsication", i),
strings.Repeat("*", len(secret.Authentication)),
"must be at least 32 characters long",
),
)
}
switch len(secret.Encryption) {
case 0:
// Require encryption secrets
allErrs = append(allErrs, fielderrors.NewFieldRequired(fmt.Sprintf("secrets[%d].encryption", i)))
case 16, 24, 32:
// Valid lengths
default:
// Don't output current value in error message... we don't want it logged
allErrs = append(allErrs,
fielderrors.NewFieldInvalid(
fmt.Sprintf("secrets[%d].encryption", i),
strings.Repeat("*", len(secret.Encryption)),
"must be 16, 24, or 32 characters long",
),
)
}
}
return allErrs
}