-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
config.go
208 lines (168 loc) · 5.89 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
// Copyright © 2022 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package fositex
import (
"context"
"crypto/sha512"
"hash"
"html/template"
"net/url"
"github.com/hashicorp/go-retryablehttp"
"github.com/ory/fosite"
"github.com/ory/fosite/compose"
"github.com/ory/fosite/i18n"
"github.com/ory/fosite/token/jwt"
"github.com/ory/hydra/v2/driver/config"
"github.com/ory/hydra/v2/oauth2"
"github.com/ory/hydra/v2/persistence"
"github.com/ory/hydra/v2/x"
"github.com/ory/x/stringslice"
"github.com/ory/x/urlx"
)
type configDependencies interface {
config.Provider
persistence.Provider
x.HTTPClientProvider
GetJWKSFetcherStrategy() fosite.JWKSFetcherStrategy
ClientHasher() fosite.Hasher
ExtraFositeFactories() []Factory
}
type Factory func(config fosite.Configurator, storage interface{}, strategy interface{}) interface{}
type Config struct {
deps configDependencies
authorizeEndpointHandlers fosite.AuthorizeEndpointHandlers
tokenEndpointHandlers fosite.TokenEndpointHandlers
tokenIntrospectionHandlers fosite.TokenIntrospectionHandlers
revocationHandlers fosite.RevocationHandlers
*config.DefaultProvider
}
var defaultResponseModeHandler = fosite.NewDefaultResponseModeHandler()
var defaultFactories = []Factory{
compose.OAuth2AuthorizeExplicitFactory,
compose.OAuth2AuthorizeImplicitFactory,
compose.OAuth2ClientCredentialsGrantFactory,
compose.OAuth2RefreshTokenGrantFactory,
compose.OpenIDConnectExplicitFactory,
compose.OpenIDConnectHybridFactory,
compose.OpenIDConnectImplicitFactory,
compose.OpenIDConnectRefreshFactory,
compose.OAuth2TokenRevocationFactory,
compose.OAuth2TokenIntrospectionFactory,
compose.OAuth2PKCEFactory,
compose.RFC7523AssertionGrantFactory,
compose.OIDCUserinfoVerifiableCredentialFactory,
}
func NewConfig(deps configDependencies) *Config {
c := &Config{
deps: deps,
DefaultProvider: deps.Config(),
}
return c
}
func (c *Config) LoadDefaultHandlers(strategy interface{}) {
factories := append(defaultFactories, c.deps.ExtraFositeFactories()...)
for _, factory := range factories {
res := factory(c, c.deps.Persister(), strategy)
if ah, ok := res.(fosite.AuthorizeEndpointHandler); ok {
c.authorizeEndpointHandlers.Append(ah)
}
if th, ok := res.(fosite.TokenEndpointHandler); ok {
c.tokenEndpointHandlers.Append(th)
}
if tv, ok := res.(fosite.TokenIntrospector); ok {
c.tokenIntrospectionHandlers.Append(tv)
}
if rh, ok := res.(fosite.RevocationHandler); ok {
c.revocationHandlers.Append(rh)
}
}
}
func (c *Config) GetJWKSFetcherStrategy(context.Context) fosite.JWKSFetcherStrategy {
return c.deps.GetJWKSFetcherStrategy()
}
func (c *Config) GetHTTPClient(ctx context.Context) *retryablehttp.Client {
return c.deps.HTTPClient(ctx)
}
func (c *Config) GetAuthorizeEndpointHandlers(context.Context) fosite.AuthorizeEndpointHandlers {
return c.authorizeEndpointHandlers
}
func (c *Config) GetTokenEndpointHandlers(context.Context) fosite.TokenEndpointHandlers {
return c.tokenEndpointHandlers
}
func (c *Config) GetTokenIntrospectionHandlers(context.Context) (r fosite.TokenIntrospectionHandlers) {
return c.tokenIntrospectionHandlers
}
func (c *Config) GetRevocationHandlers(context.Context) fosite.RevocationHandlers {
return c.revocationHandlers
}
func (c *Config) GetGrantTypeJWTBearerCanSkipClientAuth(context.Context) bool {
return false
}
func (c *Config) GetAudienceStrategy(context.Context) fosite.AudienceMatchingStrategy {
return fosite.DefaultAudienceMatchingStrategy
}
func (c *Config) GetOmitRedirectScopeParam(context.Context) bool {
return false
}
func (c *Config) GetSanitationWhiteList(context.Context) []string {
return []string{"code", "redirect_uri"}
}
func (c *Config) GetEnablePKCEPlainChallengeMethod(context.Context) bool {
return false
}
func (c *Config) GetDisableRefreshTokenValidation(context.Context) bool {
return false
}
func (c *Config) GetRefreshTokenScopes(context.Context) []string {
return []string{"offline", "offline_access"}
}
func (c *Config) GetMinParameterEntropy(_ context.Context) int {
return fosite.MinParameterEntropy
}
func (c *Config) GetClientAuthenticationStrategy(context.Context) fosite.ClientAuthenticationStrategy {
// Fosite falls back to the default fosite.Fosite.DefaultClientAuthenticationStrategy when this is nil.
return nil
}
func (c *Config) GetResponseModeHandlerExtension(context.Context) fosite.ResponseModeHandler {
return defaultResponseModeHandler
}
func (c *Config) GetSendDebugMessagesToClients(ctx context.Context) bool {
return c.deps.Config().GetSendDebugMessagesToClients(ctx)
}
func (c *Config) GetMessageCatalog(context.Context) i18n.MessageCatalog {
// Fosite falls back to the default messages when this is nil.
return nil
}
func (c *Config) GetSecretsHasher(context.Context) fosite.Hasher {
return c.deps.ClientHasher()
}
func (c *Config) GetTokenEntropy(context.Context) int {
return 32
}
func (c *Config) GetHMACHasher(context.Context) func() hash.Hash {
return sha512.New512_256
}
func (c *Config) GetIDTokenIssuer(ctx context.Context) string {
return c.deps.Config().IssuerURL(ctx).String()
}
func (c *Config) GetAllowedPrompts(context.Context) []string {
return []string{"login", "none", "consent", "registration"}
}
func (c *Config) GetRedirectSecureChecker(context.Context) func(context.Context, *url.URL) bool {
return x.IsRedirectURISecure(c.deps.Config())
}
func (c *Config) GetAccessTokenIssuer(ctx context.Context) string {
return c.deps.Config().IssuerURL(ctx).String()
}
func (c *Config) GetJWTScopeField(ctx context.Context) jwt.JWTScopeFieldEnum {
return c.deps.Config().GetJWTScopeField(ctx)
}
func (c *Config) GetFormPostHTMLTemplate(context.Context) *template.Template {
return fosite.DefaultFormPostTemplate
}
func (c *Config) GetTokenURLs(ctx context.Context) []string {
return stringslice.Unique([]string{
c.OAuth2TokenURL(ctx).String(),
urlx.AppendPaths(c.deps.Config().PublicURL(ctx), oauth2.TokenPath).String(),
})
}