Skip to content

Commit

Permalink
feat: allow injecting extra fosite strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
hperl committed Oct 12, 2023
1 parent a0c06ec commit 815e82e
Show file tree
Hide file tree
Showing 5 changed files with 381 additions and 7 deletions.
18 changes: 14 additions & 4 deletions driver/factory.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading

0 comments on commit 815e82e

Please sign in to comment.