-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
config.go
584 lines (484 loc) · 18 KB
/
config.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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
package oidc
import (
"context"
"crypto/sha512"
"hash"
"html/template"
"net/url"
"time"
"github.com/hashicorp/go-retryablehttp"
"github.com/ory/fosite"
"github.com/ory/fosite/handler/oauth2"
"github.com/ory/fosite/handler/openid"
"github.com/ory/fosite/handler/par"
"github.com/ory/fosite/handler/pkce"
"github.com/ory/fosite/i18n"
"github.com/ory/fosite/token/hmac"
"github.com/ory/fosite/token/jwt"
"github.com/authelia/authelia/v4/internal/configuration/schema"
"github.com/authelia/authelia/v4/internal/utils"
)
func NewConfig(config *schema.OpenIDConnectConfiguration) *Config {
c := &Config{
GlobalSecret: []byte(utils.HashSHA256FromString(config.HMACSecret)),
SendDebugMessagesToClients: config.EnableClientDebugMessages,
MinParameterEntropy: config.MinimumParameterEntropy,
Lifespans: LifespanConfig{
AccessToken: config.AccessTokenLifespan,
AuthorizeCode: config.AuthorizeCodeLifespan,
IDToken: config.IDTokenLifespan,
RefreshToken: config.RefreshTokenLifespan,
},
ProofKeyCodeExchange: ProofKeyCodeExchangeConfig{
Enforce: config.EnforcePKCE == "always",
EnforcePublicClients: config.EnforcePKCE != "never",
AllowPlainChallengeMethod: config.EnablePKCEPlainChallenge,
},
}
prefix := "authelia_%s_"
c.Strategy.Core = &HMACCoreStrategy{
Enigma: &hmac.HMACStrategy{Config: c},
Config: c,
prefix: &prefix,
}
return c
}
type Config struct {
// GlobalSecret is the global secret used to sign and verify signatures.
GlobalSecret []byte
// RotatedGlobalSecrets is a list of global secrets that are used to verify signatures.
RotatedGlobalSecrets [][]byte
Issuers IssuersConfig
SendDebugMessagesToClients bool
DisableRefreshTokenValidation bool
OmitRedirectScopeParameter bool
JWTScopeField jwt.JWTScopeFieldEnum
JWTMaxDuration time.Duration
Hasher *AdaptiveHasher
Hash HashConfig
Strategy StrategyConfig
PAR PARConfig
Handlers HandlersConfig
Lifespans LifespanConfig
ProofKeyCodeExchange ProofKeyCodeExchangeConfig
GrantTypeJWTBearer GrantTypeJWTBearerConfig
TokenURL string
TokenEntropy int
MinParameterEntropy int
SanitationWhiteList []string
AllowedPrompts []string
RefreshTokenScopes []string
HTTPClient *retryablehttp.Client
FormPostHTMLTemplate *template.Template
MessageCatalog i18n.MessageCatalog
}
type HashConfig struct {
ClientSecrets fosite.Hasher
HMAC func() (h hash.Hash)
}
type StrategyConfig struct {
Core oauth2.CoreStrategy
OpenID openid.OpenIDConnectTokenStrategy
Audience fosite.AudienceMatchingStrategy
Scope fosite.ScopeStrategy
JWKSFetcher fosite.JWKSFetcherStrategy
ClientAuthentication fosite.ClientAuthenticationStrategy
}
type PARConfig struct {
Enforced bool
URIPrefix string
ContextLifespan time.Duration
}
type IssuersConfig struct {
IDToken string
AccessToken string
}
type HandlersConfig struct {
// ResponseMode provides an extension handler for custom response modes.
ResponseMode fosite.ResponseModeHandler
// AuthorizeEndpoint is a list of handlers that are called before the authorization endpoint is served.
AuthorizeEndpoint fosite.AuthorizeEndpointHandlers
// TokenEndpoint is a list of handlers that are called before the token endpoint is served.
TokenEndpoint fosite.TokenEndpointHandlers
// TokenIntrospection is a list of handlers that are called before the token introspection endpoint is served.
TokenIntrospection fosite.TokenIntrospectionHandlers
// Revocation is a list of handlers that are called before the revocation endpoint is served.
Revocation fosite.RevocationHandlers
// PushedAuthorizeEndpoint is a list of handlers that are called before the PAR endpoint is served.
PushedAuthorizeEndpoint fosite.PushedAuthorizeEndpointHandlers
}
type GrantTypeJWTBearerConfig struct {
OptionalClientAuth bool
OptionalJTIClaim bool
OptionalIssuedDate bool
}
type ProofKeyCodeExchangeConfig struct {
Enforce bool
EnforcePublicClients bool
AllowPlainChallengeMethod bool
}
type LifespanConfig struct {
AccessToken time.Duration
AuthorizeCode time.Duration
IDToken time.Duration
RefreshToken time.Duration
}
const (
PromptNone = none
PromptLogin = "login"
PromptConsent = "consent"
)
func (c *Config) LoadHandlers(store *Store, strategy jwt.Signer) {
validator := openid.NewOpenIDConnectRequestValidator(strategy, c)
handlers := []any{
&oauth2.AuthorizeExplicitGrantHandler{
AccessTokenStrategy: c.Strategy.Core,
RefreshTokenStrategy: c.Strategy.Core,
AuthorizeCodeStrategy: c.Strategy.Core,
CoreStorage: store,
TokenRevocationStorage: store,
Config: c,
},
&oauth2.AuthorizeImplicitGrantTypeHandler{
AccessTokenStrategy: c.Strategy.Core,
AccessTokenStorage: store,
Config: c,
},
&oauth2.ClientCredentialsGrantHandler{
HandleHelper: &oauth2.HandleHelper{
AccessTokenStrategy: c.Strategy.Core,
AccessTokenStorage: store,
Config: c,
},
Config: c,
},
&oauth2.RefreshTokenGrantHandler{
AccessTokenStrategy: c.Strategy.Core,
RefreshTokenStrategy: c.Strategy.Core,
TokenRevocationStorage: store,
Config: c,
},
&openid.OpenIDConnectExplicitHandler{
IDTokenHandleHelper: &openid.IDTokenHandleHelper{
IDTokenStrategy: c.Strategy.OpenID,
},
OpenIDConnectRequestValidator: validator,
OpenIDConnectRequestStorage: store,
Config: c,
},
&openid.OpenIDConnectImplicitHandler{
AuthorizeImplicitGrantTypeHandler: &oauth2.AuthorizeImplicitGrantTypeHandler{
AccessTokenStrategy: c.Strategy.Core,
AccessTokenStorage: store,
Config: c,
},
IDTokenHandleHelper: &openid.IDTokenHandleHelper{
IDTokenStrategy: c.Strategy.OpenID,
},
OpenIDConnectRequestValidator: validator,
Config: c,
},
&openid.OpenIDConnectHybridHandler{
AuthorizeExplicitGrantHandler: &oauth2.AuthorizeExplicitGrantHandler{
AccessTokenStrategy: c.Strategy.Core,
RefreshTokenStrategy: c.Strategy.Core,
AuthorizeCodeStrategy: c.Strategy.Core,
CoreStorage: store,
Config: c,
},
Config: c,
AuthorizeImplicitGrantTypeHandler: &oauth2.AuthorizeImplicitGrantTypeHandler{
AccessTokenStrategy: c.Strategy.Core,
AccessTokenStorage: store,
Config: c,
},
IDTokenHandleHelper: &openid.IDTokenHandleHelper{
IDTokenStrategy: c.Strategy.OpenID,
},
OpenIDConnectRequestValidator: validator,
OpenIDConnectRequestStorage: store,
},
&openid.OpenIDConnectRefreshHandler{
IDTokenHandleHelper: &openid.IDTokenHandleHelper{
IDTokenStrategy: c.Strategy.OpenID,
},
Config: c,
},
&oauth2.CoreValidator{
CoreStrategy: c.Strategy.Core,
CoreStorage: store,
Config: c,
},
&oauth2.TokenRevocationHandler{
AccessTokenStrategy: c.Strategy.Core,
RefreshTokenStrategy: c.Strategy.Core,
TokenRevocationStorage: store,
},
&pkce.Handler{
AuthorizeCodeStrategy: c.Strategy.Core,
Storage: store,
Config: c,
},
&par.PushedAuthorizeHandler{
Storage: store,
Config: c,
},
}
x := HandlersConfig{}
for _, handler := range handlers {
if h, ok := handler.(fosite.AuthorizeEndpointHandler); ok {
x.AuthorizeEndpoint.Append(h)
}
if h, ok := handler.(fosite.TokenEndpointHandler); ok {
x.TokenEndpoint.Append(h)
}
if h, ok := handler.(fosite.TokenIntrospector); ok {
x.TokenIntrospection.Append(h)
}
if h, ok := handler.(fosite.RevocationHandler); ok {
x.Revocation.Append(h)
}
}
c.Handlers = x
}
// GetAllowedPrompts returns the allowed prompts.
func (c *Config) GetAllowedPrompts(ctx context.Context) (prompts []string) {
if len(c.AllowedPrompts) == 0 {
c.AllowedPrompts = []string{PromptNone, PromptLogin, PromptConsent}
}
return c.AllowedPrompts
}
// GetEnforcePKCE returns the enforcement of PKCE.
func (c *Config) GetEnforcePKCE(ctx context.Context) (enforce bool) {
return c.ProofKeyCodeExchange.Enforce
}
// GetEnforcePKCEForPublicClients returns the enforcement of PKCE for public clients.
func (c *Config) GetEnforcePKCEForPublicClients(ctx context.Context) (enforce bool) {
return c.GetEnforcePKCE(ctx) || c.ProofKeyCodeExchange.EnforcePublicClients
}
// GetEnablePKCEPlainChallengeMethod returns the enable PKCE plain challenge method.
func (c *Config) GetEnablePKCEPlainChallengeMethod(ctx context.Context) (enable bool) {
return c.ProofKeyCodeExchange.AllowPlainChallengeMethod
}
// GetGrantTypeJWTBearerCanSkipClientAuth returns the grant type JWT bearer can skip client auth.
func (c *Config) GetGrantTypeJWTBearerCanSkipClientAuth(ctx context.Context) (skip bool) {
return c.GrantTypeJWTBearer.OptionalClientAuth
}
// GetGrantTypeJWTBearerIDOptional returns the grant type JWT bearer ID optional.
func (c *Config) GetGrantTypeJWTBearerIDOptional(ctx context.Context) (optional bool) {
return c.GrantTypeJWTBearer.OptionalJTIClaim
}
// GetGrantTypeJWTBearerIssuedDateOptional returns the grant type JWT bearer issued date optional.
func (c *Config) GetGrantTypeJWTBearerIssuedDateOptional(ctx context.Context) (optional bool) {
return c.GrantTypeJWTBearer.OptionalIssuedDate
}
// GetJWTMaxDuration returns the JWT max duration.
func (c *Config) GetJWTMaxDuration(ctx context.Context) (duration time.Duration) {
if c.JWTMaxDuration == 0 {
c.JWTMaxDuration = time.Hour * 24
}
return c.JWTMaxDuration
}
// GetRedirectSecureChecker returns the redirect URL security validator.
func (c *Config) GetRedirectSecureChecker(ctx context.Context) func(context.Context, *url.URL) (secure bool) {
return fosite.IsRedirectURISecure
}
// GetOmitRedirectScopeParam must be set to true if the scope query param is to be omitted
// in the authorization's redirect URI.
func (c *Config) GetOmitRedirectScopeParam(ctx context.Context) (omit bool) {
return c.OmitRedirectScopeParameter
}
// GetSanitationWhiteList is a whitelist of form values that are required by the token endpoint. These values
// are safe for storage in a database (cleartext).
func (c *Config) GetSanitationWhiteList(ctx context.Context) (whitelist []string) {
return c.SanitationWhiteList
}
// GetJWTScopeField returns the JWT scope field.
func (c *Config) GetJWTScopeField(ctx context.Context) (field jwt.JWTScopeFieldEnum) {
if c.JWTScopeField == jwt.JWTScopeFieldUnset {
c.JWTScopeField = jwt.JWTScopeFieldList
}
return c.JWTScopeField
}
// GetIDTokenIssuer returns the ID token issuer.
func (c *Config) GetIDTokenIssuer(ctx context.Context) (issuer string) {
return c.Issuers.IDToken
}
// GetAccessTokenIssuer returns the access token issuer.
func (c *Config) GetAccessTokenIssuer(ctx context.Context) (issuer string) {
return c.Issuers.AccessToken
}
// GetDisableRefreshTokenValidation returns the disable refresh token validation flag.
func (c *Config) GetDisableRefreshTokenValidation(ctx context.Context) (disable bool) {
return c.DisableRefreshTokenValidation
}
// GetAuthorizeCodeLifespan returns the authorization code lifespan.
func (c *Config) GetAuthorizeCodeLifespan(ctx context.Context) (lifespan time.Duration) {
if c.Lifespans.AuthorizeCode <= 0 {
c.Lifespans.AccessToken = lifespanAuthorizeCodeDefault
}
return c.Lifespans.AuthorizeCode
}
// GetRefreshTokenLifespan returns the refresh token lifespan.
func (c *Config) GetRefreshTokenLifespan(ctx context.Context) (lifespan time.Duration) {
if c.Lifespans.RefreshToken <= 0 {
c.Lifespans.AccessToken = lifespanRefreshTokenDefault
}
return c.Lifespans.RefreshToken
}
// GetIDTokenLifespan returns the ID token lifespan.
func (c *Config) GetIDTokenLifespan(ctx context.Context) (lifespan time.Duration) {
if c.Lifespans.IDToken <= 0 {
c.Lifespans.AccessToken = lifespanTokenDefault
}
return c.Lifespans.IDToken
}
// GetAccessTokenLifespan returns the access token lifespan.
func (c *Config) GetAccessTokenLifespan(ctx context.Context) (lifespan time.Duration) {
if c.Lifespans.AccessToken <= 0 {
c.Lifespans.AccessToken = lifespanTokenDefault
}
return c.Lifespans.AccessToken
}
// GetTokenEntropy returns the token entropy.
func (c *Config) GetTokenEntropy(ctx context.Context) (entropy int) {
if c.TokenEntropy == 0 {
c.TokenEntropy = 32
}
return c.TokenEntropy
}
// GetGlobalSecret returns the global secret.
func (c *Config) GetGlobalSecret(ctx context.Context) (secret []byte, err error) {
return c.GlobalSecret, nil
}
// GetRotatedGlobalSecrets returns the rotated global secrets.
func (c *Config) GetRotatedGlobalSecrets(ctx context.Context) (secrets [][]byte, err error) {
return c.RotatedGlobalSecrets, nil
}
// GetHTTPClient returns the HTTP client provider.
func (c *Config) GetHTTPClient(ctx context.Context) (client *retryablehttp.Client) {
if c.HTTPClient == nil {
c.HTTPClient = retryablehttp.NewClient()
}
return c.HTTPClient
}
// GetRefreshTokenScopes returns the refresh token scopes.
func (c *Config) GetRefreshTokenScopes(ctx context.Context) (scopes []string) {
if c.RefreshTokenScopes == nil {
c.RefreshTokenScopes = []string{ScopeOffline, ScopeOfflineAccess}
}
return c.RefreshTokenScopes
}
// GetScopeStrategy returns the scope strategy.
func (c *Config) GetScopeStrategy(ctx context.Context) (strategy fosite.ScopeStrategy) {
if c.Strategy.Scope == nil {
c.Strategy.Scope = fosite.ExactScopeStrategy
}
return c.Strategy.Scope
}
// GetAudienceStrategy returns the audience strategy.
func (c *Config) GetAudienceStrategy(ctx context.Context) (strategy fosite.AudienceMatchingStrategy) {
if c.Strategy.Audience == nil {
c.Strategy.Audience = fosite.DefaultAudienceMatchingStrategy
}
return c.Strategy.Audience
}
// GetMinParameterEntropy returns the minimum parameter entropy.
func (c *Config) GetMinParameterEntropy(_ context.Context) (entropy int) {
if c.MinParameterEntropy == 0 {
c.MinParameterEntropy = fosite.MinParameterEntropy
}
return c.MinParameterEntropy
}
// GetHMACHasher returns the hash function.
func (c *Config) GetHMACHasher(ctx context.Context) func() (h hash.Hash) {
if c.Hash.HMAC == nil {
c.Hash.HMAC = sha512.New512_256
}
return c.Hash.HMAC
}
// GetSendDebugMessagesToClients returns the send debug messages to clients.
func (c *Config) GetSendDebugMessagesToClients(ctx context.Context) (send bool) {
return c.SendDebugMessagesToClients
}
// GetJWKSFetcherStrategy returns the JWKS fetcher strategy.
func (c *Config) GetJWKSFetcherStrategy(ctx context.Context) (strategy fosite.JWKSFetcherStrategy) {
if c.Strategy.JWKSFetcher == nil {
c.Strategy.JWKSFetcher = fosite.NewDefaultJWKSFetcherStrategy()
}
return c.Strategy.JWKSFetcher
}
// GetClientAuthenticationStrategy returns the client authentication strategy.
func (c *Config) GetClientAuthenticationStrategy(ctx context.Context) (strategy fosite.ClientAuthenticationStrategy) {
return c.Strategy.ClientAuthentication
}
// GetMessageCatalog returns the message catalog.
func (c *Config) GetMessageCatalog(ctx context.Context) (catalog i18n.MessageCatalog) {
return c.MessageCatalog
}
// GetFormPostHTMLTemplate returns the form post HTML template.
func (c *Config) GetFormPostHTMLTemplate(ctx context.Context) (tmpl *template.Template) {
return c.FormPostHTMLTemplate
}
// GetTokenURL returns the token URL.
func (c *Config) GetTokenURL(ctx context.Context) (tokenURL string) {
return c.TokenURL
}
// GetSecretsHasher returns the client secrets hashing function.
func (c *Config) GetSecretsHasher(ctx context.Context) (hasher fosite.Hasher) {
if c.Hash.ClientSecrets == nil {
c.Hash.ClientSecrets, _ = NewAdaptiveHasher()
}
return c.Hash.ClientSecrets
}
// GetUseLegacyErrorFormat returns whether to use the legacy error format.
//
// Deprecated: Do not use this flag anymore.
func (c *Config) GetUseLegacyErrorFormat(ctx context.Context) (use bool) {
return false
}
// GetAuthorizeEndpointHandlers returns the authorize endpoint handlers.
func (c *Config) GetAuthorizeEndpointHandlers(ctx context.Context) (handlers fosite.AuthorizeEndpointHandlers) {
return c.Handlers.AuthorizeEndpoint
}
// GetTokenEndpointHandlers returns the token endpoint handlers.
func (c *Config) GetTokenEndpointHandlers(ctx context.Context) (handlers fosite.TokenEndpointHandlers) {
return c.Handlers.TokenEndpoint
}
// GetTokenIntrospectionHandlers returns the token introspection handlers.
func (c *Config) GetTokenIntrospectionHandlers(ctx context.Context) (handlers fosite.TokenIntrospectionHandlers) {
return c.Handlers.TokenIntrospection
}
// GetRevocationHandlers returns the revocation handlers.
func (c *Config) GetRevocationHandlers(ctx context.Context) (handlers fosite.RevocationHandlers) {
return c.Handlers.Revocation
}
// GetPushedAuthorizeEndpointHandlers returns the handlers.
func (c *Config) GetPushedAuthorizeEndpointHandlers(ctx context.Context) fosite.PushedAuthorizeEndpointHandlers {
return c.Handlers.PushedAuthorizeEndpoint
}
// GetResponseModeHandlerExtension returns the response mode handler extension.
func (c *Config) GetResponseModeHandlerExtension(ctx context.Context) (handler fosite.ResponseModeHandler) {
return c.Handlers.ResponseMode
}
// GetPushedAuthorizeRequestURIPrefix is the request URI prefix. This is
// usually 'urn:ietf:params:oauth:request_uri:'.
func (c *Config) GetPushedAuthorizeRequestURIPrefix(ctx context.Context) string {
if c.PAR.URIPrefix == "" {
c.PAR.URIPrefix = urnPARPrefix
}
return c.PAR.URIPrefix
}
// EnforcePushedAuthorize indicates if PAR is enforced. In this mode, a client
// cannot pass authorize parameters at the 'authorize' endpoint. The 'authorize' endpoint
// must contain the PAR request_uri.
func (c *Config) EnforcePushedAuthorize(ctx context.Context) bool {
return c.PAR.Enforced
}
// GetPushedAuthorizeContextLifespan is the lifespan of the short-lived PAR context.
func (c *Config) GetPushedAuthorizeContextLifespan(ctx context.Context) (lifespan time.Duration) {
if c.PAR.ContextLifespan == 0 {
c.PAR.ContextLifespan = lifespanPARContextDefault
}
return c.PAR.ContextLifespan
}