Skip to content

Commit

Permalink
feat: allow injecting extra fosite strategies (#3646)
Browse files Browse the repository at this point in the history
  • Loading branch information
hperl committed Oct 17, 2023
1 parent 2dc52b4 commit 88b0b7c
Show file tree
Hide file tree
Showing 18 changed files with 548 additions and 18 deletions.
2 changes: 1 addition & 1 deletion client/manager.go
Expand Up @@ -31,7 +31,7 @@ type Filter struct {
type Manager interface {
Storage

Authenticate(ctx context.Context, id string, secret []byte) (*Client, error)
AuthenticateClient(ctx context.Context, id string, secret []byte) (*Client, error)
}

type Storage interface {
Expand Down
4 changes: 2 additions & 2 deletions client/manager_test_helpers.go
Expand Up @@ -52,11 +52,11 @@ func TestHelperClientAuthenticate(k string, m Manager) func(t *testing.T) {
RedirectURIs: []string{"http://redirect"},
}))

c, err := m.Authenticate(ctx, "1234321", []byte("secret1"))
c, err := m.AuthenticateClient(ctx, "1234321", []byte("secret1"))
require.Error(t, err)
require.Nil(t, c)

c, err = m.Authenticate(ctx, "1234321", []byte("secret"))
c, err = m.AuthenticateClient(ctx, "1234321", []byte("secret"))
require.NoError(t, err)
assert.Equal(t, "1234321", c.GetID())
}
Expand Down
7 changes: 7 additions & 0 deletions driver/config/provider.go
Expand Up @@ -86,6 +86,7 @@ const (
KeyAdminURL = "urls.self.admin"
KeyIssuerURL = "urls.self.issuer"
KeyIdentityProviderAdminURL = "urls.identity_provider.url"
KeyIdentityProviderPublicURL = "urls.identity_provider.publicUrl"
KeyIdentityProviderHeaders = "urls.identity_provider.headers"
KeyAccessTokenStrategy = "strategies.access_token"
KeyJWTScopeClaimStrategy = "strategies.jwt.scope_claim"
Expand Down Expand Up @@ -415,6 +416,12 @@ func (p *DefaultProvider) KratosAdminURL(ctx context.Context) (*url.URL, bool) {

return u, u != nil
}
func (p *DefaultProvider) KratosPublicURL(ctx context.Context) (*url.URL, bool) {
u := p.getProvider(ctx).RequestURIF(KeyIdentityProviderPublicURL, nil)

return u, u != nil
}

func (p *DefaultProvider) KratosRequestHeader(ctx context.Context) http.Header {
hh := map[string]string{}
if err := p.getProvider(ctx).Unmarshal(KeyIdentityProviderHeaders, &hh); err != nil {
Expand Down
18 changes: 14 additions & 4 deletions driver/factory.go
Expand Up @@ -8,6 +8,7 @@ import (
"io/fs"

"github.com/ory/hydra/v2/driver/config"
"github.com/ory/hydra/v2/fositex"
"github.com/ory/x/configx"
"github.com/ory/x/logrusx"
"github.com/ory/x/otelx"
Expand All @@ -22,10 +23,11 @@ type (
opts []configx.OptionModifier
config *config.DefaultProvider
// The first default refers to determining the NID at startup; the second default referes to the fact that the Contextualizer may dynamically change the NID.
skipNetworkInit bool
tracerWrapper TracerWrapper
extraMigrations []fs.FS
goMigrations []popx.Migration
skipNetworkInit bool
tracerWrapper TracerWrapper
extraMigrations []fs.FS
goMigrations []popx.Migration
fositexFactories []fositex.Factory
}
OptionsModifier func(*options)

Expand Down Expand Up @@ -94,6 +96,12 @@ func WithGoMigrations(m ...popx.Migration) OptionsModifier {
}
}

func WithExtraFositeFactories(f ...fositex.Factory) OptionsModifier {
return func(o *options) {
o.fositexFactories = append(o.fositexFactories, f...)
}
}

func New(ctx context.Context, sl *servicelocatorx.Options, opts []OptionsModifier) (Registry, error) {
o := newOptions()
for _, f := range opts {
Expand Down Expand Up @@ -132,6 +140,8 @@ func New(ctx context.Context, sl *servicelocatorx.Options, opts []OptionsModifie
r.WithTracerWrapper(o.tracerWrapper)
}

r.WithExtraFositeFactories(o.fositexFactories)

if err = r.Init(ctx, o.skipNetworkInit, false, ctxter, o.extraMigrations, o.goMigrations); err != nil {
l.WithError(err).Error("Unable to initialize service registry.")
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions driver/registry.go
Expand Up @@ -10,6 +10,7 @@ import (

"go.opentelemetry.io/otel/trace"

"github.com/ory/hydra/v2/fositex"
"github.com/ory/hydra/v2/internal/kratos"
"github.com/ory/x/httprouterx"
"github.com/ory/x/popx"
Expand Down Expand Up @@ -59,6 +60,9 @@ type Registry interface {
x.HTTPClientProvider
GetJWKSFetcherStrategy() fosite.JWKSFetcherStrategy

WithExtraFositeFactories(f []fositex.Factory) Registry
ExtraFositeFactories() []fositex.Factory

contextx.Provider
config.Provider
persistence.Provider
Expand Down
11 changes: 11 additions & 0 deletions driver/registry_base.go
Expand Up @@ -90,6 +90,7 @@ type RegistryBase struct {
fc *fositex.Config
publicCORS *cors.Cors
kratos kratos.Client
fositeFactories []fositex.Factory
}

func (m *RegistryBase) GetJWKSFetcherStrategy() fosite.JWKSFetcherStrategy {
Expand Down Expand Up @@ -417,6 +418,16 @@ func (m *RegistryBase) OAuth2Config() *fositex.Config {
return m.fc
}

func (m *RegistryBase) ExtraFositeFactories() []fositex.Factory {
return m.fositeFactories
}

func (m *RegistryBase) WithExtraFositeFactories(f []fositex.Factory) Registry {
m.fositeFactories = f

return m.r
}

func (m *RegistryBase) OAuth2ProviderConfig() fosite.Configurator {
if m.oc != nil {
return m.oc
Expand Down
8 changes: 5 additions & 3 deletions fositex/config.go
Expand Up @@ -29,9 +29,10 @@ type configDependencies interface {
x.HTTPClientProvider
GetJWKSFetcherStrategy() fosite.JWKSFetcherStrategy
ClientHasher() fosite.Hasher
ExtraFositeFactories() []Factory
}

type factory func(config fosite.Configurator, storage interface{}, strategy interface{}) interface{}
type Factory func(config fosite.Configurator, storage interface{}, strategy interface{}) interface{}

type Config struct {
deps configDependencies
Expand All @@ -45,7 +46,7 @@ type Config struct {
}

var defaultResponseModeHandler = fosite.NewDefaultResponseModeHandler()
var defaultFactories = []factory{
var defaultFactories = []Factory{
compose.OAuth2AuthorizeExplicitFactory,
compose.OAuth2AuthorizeImplicitFactory,
compose.OAuth2ClientCredentialsGrantFactory,
Expand All @@ -70,7 +71,8 @@ func NewConfig(deps configDependencies) *Config {
}

func (c *Config) LoadDefaultHandlers(strategy interface{}) {
for _, factory := range defaultFactories {
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)
Expand Down
2 changes: 1 addition & 1 deletion internal/httpclient/api_oidc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 88b0b7c

Please sign in to comment.