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: allow OAuth token requests to omit resource (#901)

Wu Shuwen1cbdc6b36074
diff --git a/docs/mcp-oauth.md b/docs/mcp-oauth.md
index 1405e5f7..34f8e1a3 100644
--- a/docs/mcp-oauth.md
+++ b/docs/mcp-oauth.md
@@ -108,7 +108,7 @@ The implementation intentionally uses opaque tokens rather than JWTs:
- PKCE with `S256` is mandatory;
- redirect URIs must exactly match the URI registered by the client, except that native loopback-IP clients may change only the port used for their local callback;
- redirect URIs must use HTTPS, except `native` clients, which may use `http` with a loopback IP literal such as `http://127.0.0.1:PORT/callback` or `http://[::1]:PORT/callback`. For Claude Code compatibility, the exact hostname `localhost` is also accepted only with an explicit port; unlike loopback IP literals, a registered `localhost` redirect receives no dynamic-port exception and must match exactly during authorization;
-- the OAuth `resource` value is bound to the canonical MCP resource and checked when access tokens are used;
+- the OAuth `resource` value is bound to the canonical MCP resource at authorization and checked when access tokens are used; token requests may omit `resource`, in which case the resource bound to the code or refresh-token family is used, while a supplied value must match;
- successful authorization responses include RFC 9207 `iss` for authorization-server mix-up protection.
Expired authorization codes and tokens are removed opportunistically during OAuth writes so their rows do not accumulate indefinitely. SQLite can reuse the freed pages; shrinking the file itself still requires normal SQLite maintenance such as `VACUUM` while GOWA is stopped.
diff --git a/src/ui/mcp/oauth/server.go b/src/ui/mcp/oauth/server.go
index d1c59e93..fff6e295 100644
--- a/src/ui/mcp/oauth/server.go
+++ b/src/ui/mcp/oauth/server.go
@@ -430,7 +430,7 @@ func (s *Server) token(c fiber.Ctx) error {
if err := c.Bind().Body(&req); err != nil {
return oauthError(c, fiber.StatusBadRequest, "invalid_request", "invalid token request")
}
- if req.Resource != s.resource.String() {
+ if req.Resource != "" && req.Resource != s.resource.String() {
return oauthError(c, fiber.StatusBadRequest, "invalid_target", "resource does not match this MCP server")
}
var (
diff --git a/src/ui/mcp/oauth/server_test.go b/src/ui/mcp/oauth/server_test.go
index 670b3c0d..1c41793b 100644
--- a/src/ui/mcp/oauth/server_test.go
+++ b/src/ui/mcp/oauth/server_test.go
@@ -189,7 +189,7 @@ func TestAuthorizationCodeFlowBearerAndBasicFallback(t *testing.T) {
}
func TestTokenExchangeRejectsWrongResourceAndCodeReplay(t *testing.T) {
- _, app := newOAuthTestServer(t, testIssuer, testResource)
+ srv, app := newOAuthTestServer(t, testIssuer, testResource)
clientID := registerTestClient(t, app)
code, verifier := authorizeTestClient(t, app, clientID)
@@ -205,7 +205,21 @@ func TestTokenExchangeRejectsWrongResourceAndCodeReplay(t *testing.T) {
assert.Equal(t, fiber.StatusBadRequest, resp.StatusCode)
assertOAuthError(t, resp, "invalid_target")
- _ = exchangeTestCode(t, app, clientID, code, verifier, testResource)
+ delete(bad, "resource")
+ resp = postForm(t, app, "/oauth/token", bad)
+ require.Equal(t, fiber.StatusOK, resp.StatusCode)
+ var tokens tokenResponse
+ require.NoError(t, json.NewDecoder(resp.Body).Decode(&tokens))
+ assert.NotEmpty(t, tokens.AccessToken)
+ assert.NotEmpty(t, tokens.RefreshToken)
+
+ protected := app.Group("", srv.MCPAuthMiddleware(nil))
+ protected.Post("/oauth-resource-test", func(c fiber.Ctx) error { return c.SendStatus(fiber.StatusNoContent) })
+ bearerReq := httptest.NewRequest("POST", "/oauth-resource-test", nil)
+ bearerReq.Header.Set("Authorization", "Bearer "+tokens.AccessToken)
+ resp, err := app.Test(bearerReq)
+ require.NoError(t, err)
+ assert.Equal(t, fiber.StatusNoContent, resp.StatusCode)
replay := url.Values{
"grant_type": {"authorization_code"},
@@ -230,7 +244,6 @@ func TestRefreshRotationDetectsReuseAndRevokesFamily(t *testing.T) {
"grant_type": {"refresh_token"},
"client_id": {clientID},
"refresh_token": {initial.RefreshToken},
- "resource": {testResource},
}
resp := postForm(t, app, "/oauth/token", firstRefresh)
require.Equal(t, fiber.StatusOK, resp.StatusCode)
diff --git a/src/ui/mcp/oauth/store.go b/src/ui/mcp/oauth/store.go
index 46c02b92..91ababf9 100644
--- a/src/ui/mcp/oauth/store.go
+++ b/src/ui/mcp/oauth/store.go
@@ -291,7 +291,7 @@ WHERE code_hash = ?`, hashSecret(req.Code)).Scan(
if usedAt.Valid || now.Unix() >= expiresAt || clientID != req.ClientID || redirectURI != req.RedirectURI {
return TokenPair{}, ErrInvalidGrant
}
- if resource != req.Resource {
+ if req.Resource != "" && resource != req.Resource {
return TokenPair{}, ErrInvalidTarget
}
if !verifyPKCES256(req.CodeVerifier, codeChallenge) {
@@ -382,7 +382,7 @@ WHERE family_id = ? AND revoked_at IS NULL`, now.Unix(), familyID); err != nil {
if now.Unix() >= expiresAt || clientID != req.ClientID {
return TokenPair{}, ErrInvalidGrant
}
- if resource != req.Resource {
+ if req.Resource != "" && resource != req.Resource {
return TokenPair{}, ErrInvalidTarget
}