Skip to content

Commit 1fad17b

Browse files
authored
Add OIDC authentication support to ToolHive management API server (#634)
1 parent b5b2c5f commit 1fad17b

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

cmd/thv/app/server.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/spf13/cobra"
99

1010
s "github.com/stacklok/toolhive/pkg/api"
11+
"github.com/stacklok/toolhive/pkg/auth"
1112
)
1213

1314
var (
@@ -37,7 +38,24 @@ var serveCmd = &cobra.Command{
3738
isUnixSocket = true
3839
}
3940

40-
return s.Serve(ctx, address, isUnixSocket, debugMode, enableDocs)
41+
// Get OIDC configuration if enabled
42+
var oidcConfig *auth.JWTValidatorConfig
43+
if IsOIDCEnabled(cmd) {
44+
// Get OIDC flag values
45+
issuer := GetStringFlagOrEmpty(cmd, "oidc-issuer")
46+
audience := GetStringFlagOrEmpty(cmd, "oidc-audience")
47+
jwksURL := GetStringFlagOrEmpty(cmd, "oidc-jwks-url")
48+
clientID := GetStringFlagOrEmpty(cmd, "oidc-client-id")
49+
50+
oidcConfig = &auth.JWTValidatorConfig{
51+
Issuer: issuer,
52+
Audience: audience,
53+
JWKSURL: jwksURL,
54+
ClientID: clientID,
55+
}
56+
}
57+
58+
return s.Serve(ctx, address, isUnixSocket, debugMode, enableDocs, oidcConfig)
4159
},
4260
}
4361

@@ -48,4 +66,7 @@ func init() {
4866
"Enable OpenAPI documentation endpoints (/api/openapi.json and /api/doc)")
4967
serveCmd.Flags().StringVar(&socketPath, "socket", "", "UNIX socket path to bind the "+
5068
"server to (overrides host and port if provided)")
69+
70+
// Add OIDC validation flags
71+
AddOIDCFlags(serveCmd)
5172
}

docs/cli/thv_serve.md

Lines changed: 9 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/api/server.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/go-chi/chi/v5/middleware"
2727

2828
v1 "github.com/stacklok/toolhive/pkg/api/v1"
29+
"github.com/stacklok/toolhive/pkg/auth"
2930
"github.com/stacklok/toolhive/pkg/container"
3031
"github.com/stacklok/toolhive/pkg/lifecycle"
3132
"github.com/stacklok/toolhive/pkg/logger"
@@ -78,14 +79,29 @@ func cleanupUnixSocket(address string) {
7879
// Serve starts the server on the given address and serves the API.
7980
// It is assumed that the caller sets up appropriate signal handling.
8081
// If isUnixSocket is true, address is treated as a UNIX socket path.
81-
func Serve(ctx context.Context, address string, isUnixSocket bool, debugMode bool, enableDocs bool) error {
82+
// If oidcConfig is provided, OIDC authentication will be enabled for all API endpoints.
83+
func Serve(
84+
ctx context.Context,
85+
address string,
86+
isUnixSocket bool,
87+
debugMode bool,
88+
enableDocs bool,
89+
oidcConfig *auth.JWTValidatorConfig,
90+
) error {
8291
r := chi.NewRouter()
8392
r.Use(
8493
middleware.RequestID,
8594
// TODO: Figure out logging middleware. We may want to use a different logger.
8695
middleware.Timeout(middlewareTimeout),
8796
)
8897

98+
// Add authentication middleware
99+
authMiddleware, err := auth.GetAuthenticationMiddleware(ctx, oidcConfig)
100+
if err != nil {
101+
return fmt.Errorf("failed to create authentication middleware: %v", err)
102+
}
103+
r.Use(authMiddleware)
104+
89105
manager, err := lifecycle.NewManager(ctx)
90106
if err != nil {
91107
logger.Panicf("failed to create lifecycle manager: %v", err)

0 commit comments

Comments
 (0)