/
oidc.go
504 lines (429 loc) · 15.8 KB
/
oidc.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*
Copyright 2020 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package types
import (
"net/url"
"slices"
"time"
"github.com/gravitational/trace"
"golang.org/x/crypto/ssh"
"github.com/gravitational/teleport/api/constants"
"github.com/gravitational/teleport/api/defaults"
"github.com/gravitational/teleport/api/utils"
)
// OIDCConnector specifies configuration for Open ID Connect compatible external
// identity provider, e.g. google in some organization
type OIDCConnector interface {
// ResourceWithSecrets provides common methods for objects
ResourceWithSecrets
ResourceWithOrigin
// Issuer URL is the endpoint of the provider, e.g. https://accounts.google.com
GetIssuerURL() string
// ClientID is id for authentication client (in our case it's our Auth server)
GetClientID() string
// ClientSecret is used to authenticate our client and should not
// be visible to end user
GetClientSecret() string
// GetRedirectURLs returns list of redirect URLs.
GetRedirectURLs() []string
// GetACR returns the Authentication Context Class Reference (ACR) value.
GetACR() string
// GetProvider returns the identity provider.
GetProvider() string
// Display - Friendly name for this provider.
GetDisplay() string
// Scope is additional scopes set by provider
GetScope() []string
// ClaimsToRoles specifies dynamic mapping from claims to roles
GetClaimsToRoles() []ClaimMapping
// GetClaims returns list of claims expected by mappings
GetClaims() []string
// GetTraitMappings converts gets all claim mappings in the
// generic trait mapping format.
GetTraitMappings() TraitMappingSet
// SetClientSecret sets client secret to some value
SetClientSecret(secret string)
// SetClientID sets id for authentication client (in our case it's our Auth server)
SetClientID(string)
// SetIssuerURL sets the endpoint of the provider
SetIssuerURL(string)
// SetRedirectURLs sets the list of redirectURLs
SetRedirectURLs([]string)
// SetPrompt sets OIDC prompt value
SetPrompt(string)
// GetPrompt returns OIDC prompt value,
GetPrompt() string
// SetACR sets the Authentication Context Class Reference (ACR) value.
SetACR(string)
// SetProvider sets the identity provider.
SetProvider(string)
// SetScope sets additional scopes set by provider
SetScope([]string)
// SetClaimsToRoles sets dynamic mapping from claims to roles
SetClaimsToRoles([]ClaimMapping)
// GetUsernameClaim gets the name of the claim from the OIDC connector to be used as the user's username.
GetUsernameClaim() string
// SetDisplay sets friendly name for this provider.
SetDisplay(string)
// GetGoogleServiceAccountURI returns path to google service account URI
GetGoogleServiceAccountURI() string
// GetGoogleServiceAccount returns google service account json for Google
GetGoogleServiceAccount() string
// SetGoogleServiceAccount sets the google service account json contents
SetGoogleServiceAccount(string)
// GetGoogleAdminEmail returns a google admin user email
// https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority
// "Note: Although you can use service accounts in applications that run from a Google Workspace (formerly G Suite) domain, service accounts are not members of your Google Workspace account and aren’t subject to domain policies set by administrators. For example, a policy set in the Google Workspace admin console to restrict the ability of end users to share documents outside of the domain would not apply to service accounts."
GetGoogleAdminEmail() string
// GetAllowUnverifiedEmail returns true if unverified emails should be allowed in received users.
GetAllowUnverifiedEmail() bool
// GetMaxAge returns the amount of time that user logins are
// valid for and true if MaxAge is set. If a user logs in, but then
// does not login again within this time period, they will be forced
// to re-authenticate.
GetMaxAge() (time.Duration, bool)
}
// NewOIDCConnector returns a new OIDCConnector based off a name and OIDCConnectorSpecV3.
func NewOIDCConnector(name string, spec OIDCConnectorSpecV3) (OIDCConnector, error) {
o := &OIDCConnectorV3{
Metadata: Metadata{
Name: name,
},
Spec: spec,
}
if err := o.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return o, nil
}
// SetPrompt sets OIDC prompt value
func (o *OIDCConnectorV3) SetPrompt(p string) {
o.Spec.Prompt = p
}
// GetPrompt returns OIDC prompt value,
// * if not set, default to select_account for backwards compatibility
// * if set to none, it will be omitted
// * and any other non empty value, pass it as is
func (o *OIDCConnectorV3) GetPrompt() string {
if o.Spec.Prompt == "" {
return constants.OIDCPromptSelectAccount
}
if o.Spec.Prompt == constants.OIDCPromptNone {
return ""
}
return o.Spec.Prompt
}
// GetGoogleServiceAccountURI returns an optional path to google service account file
func (o *OIDCConnectorV3) GetGoogleServiceAccountURI() string {
return o.Spec.GoogleServiceAccountURI
}
// GetGoogleServiceAccount returns a string representing a Google service account
func (o *OIDCConnectorV3) GetGoogleServiceAccount() string {
return o.Spec.GoogleServiceAccount
}
// SetGoogleServiceAccount sets a string representing a Google service account
func (o *OIDCConnectorV3) SetGoogleServiceAccount(s string) {
o.Spec.GoogleServiceAccount = s
}
// GetGoogleAdminEmail returns a google admin user email
func (o *OIDCConnectorV3) GetGoogleAdminEmail() string {
return o.Spec.GoogleAdminEmail
}
// GetVersion returns resource version
func (o *OIDCConnectorV3) GetVersion() string {
return o.Version
}
// GetSubKind returns resource sub kind
func (o *OIDCConnectorV3) GetSubKind() string {
return o.SubKind
}
// SetSubKind sets resource subkind
func (o *OIDCConnectorV3) SetSubKind(s string) {
o.SubKind = s
}
// GetKind returns resource kind
func (o *OIDCConnectorV3) GetKind() string {
return o.Kind
}
// GetResourceID returns resource ID
func (o *OIDCConnectorV3) GetResourceID() int64 {
return o.Metadata.ID
}
// SetResourceID sets resource ID
func (o *OIDCConnectorV3) SetResourceID(id int64) {
o.Metadata.ID = id
}
// GetRevision returns the revision
func (o *OIDCConnectorV3) GetRevision() string {
return o.Metadata.GetRevision()
}
// SetRevision sets the revision
func (o *OIDCConnectorV3) SetRevision(rev string) {
o.Metadata.SetRevision(rev)
}
// WithoutSecrets returns an instance of resource without secrets.
func (o *OIDCConnectorV3) WithoutSecrets() Resource {
if o.GetClientSecret() == "" && o.GetGoogleServiceAccount() == "" {
return o
}
o2 := *o
o2.SetClientSecret("")
o2.SetGoogleServiceAccount("")
return &o2
}
// V3 returns V3 version of the resource
func (o *OIDCConnectorV3) V3() *OIDCConnectorV3 {
return o
}
// SetDisplay sets friendly name for this provider.
func (o *OIDCConnectorV3) SetDisplay(display string) {
o.Spec.Display = display
}
// GetMetadata returns object metadata
func (o *OIDCConnectorV3) GetMetadata() Metadata {
return o.Metadata
}
// Origin returns the origin value of the resource.
func (o *OIDCConnectorV3) Origin() string {
return o.Metadata.Origin()
}
// SetOrigin sets the origin value of the resource.
func (o *OIDCConnectorV3) SetOrigin(origin string) {
o.Metadata.SetOrigin(origin)
}
// SetExpiry sets expiry time for the object
func (o *OIDCConnectorV3) SetExpiry(expires time.Time) {
o.Metadata.SetExpiry(expires)
}
// Expiry returns object expiry setting
func (o *OIDCConnectorV3) Expiry() time.Time {
return o.Metadata.Expiry()
}
// GetName returns the name of the connector
func (o *OIDCConnectorV3) GetName() string {
return o.Metadata.GetName()
}
// SetName sets client secret to some value
func (o *OIDCConnectorV3) SetName(name string) {
o.Metadata.SetName(name)
}
// SetIssuerURL sets client secret to some value
func (o *OIDCConnectorV3) SetIssuerURL(issuerURL string) {
o.Spec.IssuerURL = issuerURL
}
// SetRedirectURLs sets the list of redirectURLs
func (o *OIDCConnectorV3) SetRedirectURLs(redirectURLs []string) {
o.Spec.RedirectURLs = redirectURLs
}
// SetACR sets the Authentication Context Class Reference (ACR) value.
func (o *OIDCConnectorV3) SetACR(acrValue string) {
o.Spec.ACR = acrValue
}
// SetProvider sets the identity provider.
func (o *OIDCConnectorV3) SetProvider(identityProvider string) {
o.Spec.Provider = identityProvider
}
// SetScope sets additional scopes set by provider
func (o *OIDCConnectorV3) SetScope(scope []string) {
o.Spec.Scope = scope
}
// SetClaimsToRoles sets dynamic mapping from claims to roles
func (o *OIDCConnectorV3) SetClaimsToRoles(claims []ClaimMapping) {
o.Spec.ClaimsToRoles = claims
}
// SetClientID sets id for authentication client (in our case it's our Auth server)
func (o *OIDCConnectorV3) SetClientID(clintID string) {
o.Spec.ClientID = clintID
}
// SetClientSecret sets client secret to some value
func (o *OIDCConnectorV3) SetClientSecret(secret string) {
o.Spec.ClientSecret = secret
}
// GetIssuerURL is the endpoint of the provider, e.g. https://accounts.google.com
func (o *OIDCConnectorV3) GetIssuerURL() string {
return o.Spec.IssuerURL
}
// GetClientID is id for authentication client (in our case it's our Auth server)
func (o *OIDCConnectorV3) GetClientID() string {
return o.Spec.ClientID
}
// GetClientSecret is used to authenticate our client and should not
// be visible to end user
func (o *OIDCConnectorV3) GetClientSecret() string {
return o.Spec.ClientSecret
}
// GetRedirectURLs returns a list of the connector's redirect URLs.
func (o *OIDCConnectorV3) GetRedirectURLs() []string {
return o.Spec.RedirectURLs
}
// GetACR returns the Authentication Context Class Reference (ACR) value.
func (o *OIDCConnectorV3) GetACR() string {
return o.Spec.ACR
}
// GetProvider returns the identity provider.
func (o *OIDCConnectorV3) GetProvider() string {
return o.Spec.Provider
}
// GetDisplay - Friendly name for this provider.
func (o *OIDCConnectorV3) GetDisplay() string {
if o.Spec.Display != "" {
return o.Spec.Display
}
return o.GetName()
}
// GetScope is additional scopes set by provider
func (o *OIDCConnectorV3) GetScope() []string {
return o.Spec.Scope
}
// GetUsernameClaim gets the name of the claim from the OIDC connector to be used as the user's username.
func (o *OIDCConnectorV3) GetUsernameClaim() string {
return o.Spec.UsernameClaim
}
// GetClaimsToRoles specifies dynamic mapping from claims to roles
func (o *OIDCConnectorV3) GetClaimsToRoles() []ClaimMapping {
return o.Spec.ClaimsToRoles
}
// GetClaims returns list of claims expected by mappings
func (o *OIDCConnectorV3) GetClaims() []string {
var out []string
for _, mapping := range o.Spec.ClaimsToRoles {
out = append(out, mapping.Claim)
}
return utils.Deduplicate(out)
}
// GetTraitMappings returns the OIDCConnector's TraitMappingSet
func (o *OIDCConnectorV3) GetTraitMappings() TraitMappingSet {
tms := make([]TraitMapping, 0, len(o.Spec.ClaimsToRoles))
for _, mapping := range o.Spec.ClaimsToRoles {
tms = append(tms, TraitMapping{
Trait: mapping.Claim,
Value: mapping.Value,
Roles: mapping.Roles,
})
}
return TraitMappingSet(tms)
}
// setStaticFields sets static resource header and metadata fields.
func (o *OIDCConnectorV3) setStaticFields() {
o.Kind = KindOIDCConnector
}
// CheckAndSetDefaults checks and set default values for any missing fields.
func (o *OIDCConnectorV3) CheckAndSetDefaults() error {
o.setStaticFields()
switch o.Version {
case V2, V3:
// V2 is also supported
case "":
o.Version = V3
default:
return trace.BadParameter("Version: invalid OIDC connector version %v", o.Version)
}
if err := o.Metadata.CheckAndSetDefaults(); err != nil {
return trace.Wrap(err)
}
if name := o.Metadata.Name; slices.Contains(constants.SystemConnectors, name) {
return trace.BadParameter("ID: invalid connector name, %v is a reserved name", name)
}
if o.Spec.ClientID == "" {
return trace.BadParameter("ClientID: missing client id")
}
if len(o.GetClaimsToRoles()) == 0 {
return trace.BadParameter("claims_to_roles is empty, authorization with connector would never assign any roles")
}
for _, v := range o.Spec.ClaimsToRoles {
if len(v.Roles) == 0 {
return trace.BadParameter("add roles in claims_to_roles")
}
}
if _, err := url.Parse(o.GetIssuerURL()); err != nil {
return trace.BadParameter("bad IssuerURL '%v', err: %v", o.GetIssuerURL(), err)
}
if len(o.GetRedirectURLs()) == 0 {
return trace.BadParameter("RedirectURL: missing redirect_url")
}
for _, redirectURL := range o.GetRedirectURLs() {
if _, err := url.Parse(redirectURL); err != nil {
return trace.BadParameter("bad RedirectURL '%v', err: %v", redirectURL, err)
}
}
if o.GetGoogleServiceAccountURI() != "" && o.GetGoogleServiceAccount() != "" {
return trace.BadParameter("one of either google_service_account_uri or google_service_account is supported, not both")
}
if o.GetGoogleServiceAccountURI() != "" {
uri, err := utils.ParseSessionsURI(o.GetGoogleServiceAccountURI())
if err != nil {
return trace.Wrap(err)
}
if uri.Scheme != "file" {
return trace.BadParameter("only file:// scheme is supported for google_service_account_uri")
}
if o.GetGoogleAdminEmail() == "" {
return trace.BadParameter("whenever google_service_account_uri is specified, google_admin_email should be set as well, read https://developers.google.com/identity/protools/OAuth2ServiceAccount#delegatingauthority for more details")
}
}
if o.GetGoogleServiceAccount() != "" {
if o.GetGoogleAdminEmail() == "" {
return trace.BadParameter("whenever google_service_account is specified, google_admin_email should be set as well, read https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority for more details")
}
}
if o.Spec.MaxAge != nil {
maxAge := o.Spec.MaxAge.Value.Duration()
if maxAge < 0 {
return trace.BadParameter("max_age cannot be negative")
}
if maxAge.Round(time.Second) != maxAge {
return trace.BadParameter("max_age must be a multiple of seconds")
}
}
return nil
}
// GetAllowUnverifiedEmail returns true if unverified emails should be allowed in received users.
func (o *OIDCConnectorV3) GetAllowUnverifiedEmail() bool {
return o.Spec.AllowUnverifiedEmail
}
// GetMaxAge returns the amount of time that user logins are
// valid for and true if MaxAge is set. If a user logs in, but then
// does not login again within this time period, they will be forced
// to re-authenticate.
func (o *OIDCConnectorV3) GetMaxAge() (time.Duration, bool) {
if o.Spec.MaxAge == nil {
return 0, false
}
return o.Spec.MaxAge.Value.Duration(), true
}
// Check returns nil if all parameters are great, err otherwise
func (i *OIDCAuthRequest) Check() error {
if i.ConnectorID == "" {
return trace.BadParameter("ConnectorID: missing value")
}
if i.StateToken == "" {
return trace.BadParameter("StateToken: missing value")
}
if len(i.PublicKey) != 0 {
_, _, _, _, err := ssh.ParseAuthorizedKey(i.PublicKey)
if err != nil {
return trace.BadParameter("PublicKey: bad key: %v", err)
}
if (i.CertTTL > defaults.MaxCertDuration) || (i.CertTTL < defaults.MinCertDuration) {
return trace.BadParameter("CertTTL: wrong certificate TTL")
}
}
// we could collapse these two checks into one, but the error message would become ambiguous.
if i.SSOTestFlow && i.ConnectorSpec == nil {
return trace.BadParameter("ConnectorSpec cannot be nil when SSOTestFlow is true")
}
if !i.SSOTestFlow && i.ConnectorSpec != nil {
return trace.BadParameter("ConnectorSpec must be nil when SSOTestFlow is false")
}
return nil
}