Skip to content

GOWA - WhatsApp REST API with support for UI, Multi Account, Webhooks, and MCP, and Chatwoot. Built with Golang for efficient memory use.

fix(auth): scope OAuth middleware to MCP

Aldino Kemalc94ae064b80e
diff --git a/src/cmd/mcp_oauth.go b/src/cmd/mcp_oauth.go
index 037d327c..f151d583 100644
--- a/src/cmd/mcp_oauth.go
+++ b/src/cmd/mcp_oauth.go
@@ -104,7 +104,7 @@ func registerMcpOAuth(app *fiber.App, dm *whatsapp.DeviceManager) (*mcpoauth.Ser
if config.AppBasePath != "" {
mcpRouter = app.Group(config.AppBasePath)
}
- mcpRouter = mcpRouter.Group("", oauthServer.MCPAuthMiddleware(validateCredential))
+ useMcpOAuthMiddleware(mcpRouter, oauthServer.MCPAuthMiddleware(validateCredential))
uimcp.Register(mcpRouter, dm, uimcp.Deps{
App: appUsecase,
Send: sendUsecase,
@@ -117,6 +117,13 @@ func registerMcpOAuth(app *fiber.App, dm *whatsapp.DeviceManager) (*mcpoauth.Ser
return oauthServer, true, nil
}
+// useMcpOAuthMiddleware limits the OAuth-or-Basic challenge to the MCP
+// transport. Mounting it on an empty-prefix group also intercepts unrelated
+// REST/UI routes registered later and replaces their normal Basic challenge.
+func useMcpOAuthMiddleware(router fiber.Router, auth fiber.Handler) {
+ router.Use("/mcp", auth)
+}
+
func mcpOAuthCredentialValidator(credentials []string) (mcpoauth.CredentialValidator, error) {
if len(credentials) == 0 {
return nil, errors.New("MCP OAuth requires APP_BASIC_AUTH credentials")
diff --git a/src/cmd/mcp_oauth_test.go b/src/cmd/mcp_oauth_test.go
new file mode 100644
index 00000000..a154982e
--- /dev/null
+++ b/src/cmd/mcp_oauth_test.go
@@ -0,0 +1,61 @@
+package cmd
+
+import (
+ "net/http/httptest"
+ "testing"
+
+ "github.com/gofiber/fiber/v3"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestMcpOAuthMiddlewareOnlyProtectsMcpRoute(t *testing.T) {
+ tests := []struct {
+ name string
+ basePath string
+ }{
+ {name: "root deployment"},
+ {name: "base path deployment", basePath: "/gowa"},
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ app := fiber.New()
+ var router fiber.Router = app
+ if tt.basePath != "" {
+ router = app.Group(tt.basePath)
+ }
+
+ useMcpOAuthMiddleware(router, func(c fiber.Ctx) error {
+ c.Set(fiber.HeaderWWWAuthenticate, `Bearer resource_metadata="https://gowa.example.com/.well-known/oauth-protected-resource/mcp"`)
+ return c.SendStatus(fiber.StatusUnauthorized)
+ })
+ router.Post("/mcp", func(c fiber.Ctx) error {
+ return c.SendStatus(fiber.StatusNoContent)
+ })
+
+ app.Use(newBasicAuthMiddleware(map[string]string{"user": "secret"}))
+ app.Get(tt.basePath+"/", func(c fiber.Ctx) error {
+ return c.SendStatus(fiber.StatusOK)
+ })
+ app.Get("/favicon.ico", func(c fiber.Ctx) error {
+ return c.SendStatus(fiber.StatusOK)
+ })
+
+ mcpResp, err := app.Test(httptest.NewRequest("POST", tt.basePath+"/mcp", nil))
+ require.NoError(t, err)
+ assert.Equal(t, fiber.StatusUnauthorized, mcpResp.StatusCode)
+ assert.Contains(t, mcpResp.Header.Get(fiber.HeaderWWWAuthenticate), "Bearer")
+
+ rootResp, err := app.Test(httptest.NewRequest("GET", tt.basePath+"/", nil))
+ require.NoError(t, err)
+ assert.Equal(t, fiber.StatusUnauthorized, rootResp.StatusCode)
+ assert.Equal(t, `Basic realm="Restricted", charset="UTF-8"`, rootResp.Header.Get(fiber.HeaderWWWAuthenticate))
+
+ faviconResp, err := app.Test(httptest.NewRequest("GET", "/favicon.ico", nil))
+ require.NoError(t, err)
+ assert.Equal(t, fiber.StatusUnauthorized, faviconResp.StatusCode)
+ assert.Equal(t, `Basic realm="Restricted", charset="UTF-8"`, faviconResp.Header.Get(fiber.HeaderWWWAuthenticate))
+ })
+ }
+}