Skip to content

Commit

Permalink
feat(op): authorize callback handler as argument in legacy server reg…
Browse files Browse the repository at this point in the history
…istration

This change requires an additional argument to the op.RegisterLegacyServer constructor which passes the Authorize Callback Handler.
This allows implementations to use their own handler instead of the one provided by the package.
The current handler is exported for legacy behavior.

This change is not considered breaking, as RegisterLegacyServer is flagged experimental.

Related to zitadel/zitadel#6882
  • Loading branch information
muhlemmer committed Apr 30, 2024
1 parent 099081f commit e56783c
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 10 deletions.
2 changes: 1 addition & 1 deletion example/server/exampleop/op.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func SetupServer(issuer string, storage Storage, logger *slog.Logger, wrapServer

handler := http.Handler(provider)
if wrapServer {
handler = op.RegisterLegacyServer(op.NewLegacyServer(provider, *op.DefaultEndpoints))
handler = op.RegisterLegacyServer(op.NewLegacyServer(provider, *op.DefaultEndpoints), op.AuthorizeCallbackHandler(provider))
}

// we register the http handler of the OP on the root, so that the discovery endpoint (/.well-known/openid-configuration)
Expand Down
2 changes: 1 addition & 1 deletion pkg/op/auth_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func authorizeHandler(authorizer Authorizer) func(http.ResponseWriter, *http.Req
}
}

func authorizeCallbackHandler(authorizer Authorizer) func(http.ResponseWriter, *http.Request) {
func AuthorizeCallbackHandler(authorizer Authorizer) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
AuthorizeCallback(w, r, authorizer)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/op/op.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func CreateRouter(o OpenIDProvider, interceptors ...HttpInterceptor) chi.Router
router.HandleFunc(readinessEndpoint, readyHandler(o.Probes()))
router.HandleFunc(oidc.DiscoveryEndpoint, discoveryHandler(o, o.Storage()))
router.HandleFunc(o.AuthorizationEndpoint().Relative(), authorizeHandler(o))
router.HandleFunc(authCallbackPath(o), authorizeCallbackHandler(o))
router.HandleFunc(authCallbackPath(o), AuthorizeCallbackHandler(o))
router.HandleFunc(o.TokenEndpoint().Relative(), tokenHandler(o))
router.HandleFunc(o.IntrospectionEndpoint().Relative(), introspectionHandler(o))
router.HandleFunc(o.UserinfoEndpoint().Relative(), userinfoHandler(o))
Expand Down
2 changes: 1 addition & 1 deletion pkg/op/server_http_routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func jwtProfile() (string, error) {
}

func TestServerRoutes(t *testing.T) {
server := op.RegisterLegacyServer(op.NewLegacyServer(testProvider, *op.DefaultEndpoints))
server := op.RegisterLegacyServer(op.NewLegacyServer(testProvider, *op.DefaultEndpoints), op.AuthorizeCallbackHandler(testProvider))

storage := testProvider.Storage().(routesTestStorage)
ctx := op.ContextWithIssuer(context.Background(), testIssuer)
Expand Down
11 changes: 5 additions & 6 deletions pkg/op/server_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@ type ExtendedLegacyServer interface {
}

// RegisterLegacyServer registers a [LegacyServer] or an extension thereof.
// It takes care of registering the IssuerFromRequest middleware
// and Authorization Callback Routes.
// It takes care of registering the IssuerFromRequest middleware.
// The authorizeCallbackHandler is registered on `/callback` under the authorization endpoint.
// Neither are part of the bare [Server] interface.
//
// EXPERIMENTAL: may change until v4
func RegisterLegacyServer(s ExtendedLegacyServer, options ...ServerOption) http.Handler {
provider := s.Provider()
func RegisterLegacyServer(s ExtendedLegacyServer, authorizeCallbackHandler http.HandlerFunc, options ...ServerOption) http.Handler {
options = append(options,
WithHTTPMiddleware(intercept(provider.IssuerFromRequest)),
WithHTTPMiddleware(intercept(s.Provider().IssuerFromRequest)),
WithSetRouter(func(r chi.Router) {
r.HandleFunc(s.Endpoints().Authorization.Relative()+authCallbackPathSuffix, authorizeCallbackHandler(provider))
r.HandleFunc(s.Endpoints().Authorization.Relative()+authCallbackPathSuffix, authorizeCallbackHandler)
}),
)
return RegisterServer(s, s.Endpoints(), options...)
Expand Down

0 comments on commit e56783c

Please sign in to comment.