Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(oauth): authenticate the client before spending a lookup, and document the challenge header

nirholas0be812a81a33
diff --git a/api/oauth/[action].js b/api/oauth/[action].js
index 325acd34a..83b975b9e 100644
--- a/api/oauth/[action].js
+++ b/api/oauth/[action].js
@@ -223,19 +223,22 @@ async function handleToken(req, res) {
if (!clientId) return error(res, 400, 'invalid_client', 'client_id required');
const rl = await limits.oauthToken(clientId);
if (!rl.success) return rateLimited(res, rl, 'too many token requests');
- const auth = await authenticateClient(req, form);
- if (!auth.ok) {
- if (auth.reason === 'unknown_client') return error(res, 400, 'invalid_client', 'unknown client');
- return invalidClientCredentials(req, res);
- }
- const client = auth.client;
// RFC 8707 §2.2: the token request MAY repeat `resource`, and a server that
// does not recognize the one it names MUST answer `invalid_target`. /oauth/
// authorize has enforced that since the audience bug; the token endpoint
// ignored the parameter, which reintroduced the exact failure canonicalResource
// exists to prevent: a client naming another resource got a 200 and a token
// whose `aud` was this server's, then hit 401s at every endpoint it tried.
+ // Checked before the client lookup, exactly as authorize does it: the answer
+ // is the same for every caller and it is already public in the well-known
+ // metadata, so there is nothing to gain from spending a query first.
if (!canonicalResource(form.resource)) return error(res, 400, 'invalid_target', `unknown resource, this server only issues tokens for ${env.MCP_RESOURCE}`);
+ const auth = await authenticateClient(req, form);
+ if (!auth.ok) {
+ if (auth.reason === 'unknown_client') return error(res, 400, 'invalid_client', 'unknown client');
+ return invalidClientCredentials(req, res);
+ }
+ const client = auth.client;
const grantType = form.grant_type;
if (grantType === 'authorization_code') {
const { code, redirect_uri, code_verifier } = form;
diff --git a/docs/authentication.md b/docs/authentication.md
index ba84a9f8c..6f1add8a7 100644
--- a/docs/authentication.md
+++ b/docs/authentication.md
@@ -536,7 +536,25 @@ curl -X POST https://three.ws/api/oauth/revoke \
-d "token=$REFRESH_TOKEN"
```
-Registration returns `client_secret_expires_at: 0` alongside an issued secret: three.ws secrets do not expire, so rotate one by registering a new client.
+Registration returns `client_secret_expires_at: 0` alongside an issued secret: three.ws secrets do not expire, so rotate one by registering a new client. It also echoes back every optional field you registered (`client_uri`, `logo_uri`, `software_id`, `software_version`), so the response is the authoritative record of what was stored (RFC 7591 section 3.2.1). Do not re-register to "fix" a field you sent; a second registration issues a second `client_id`.
+
+A rejected secret returns `401` with `{"error": "invalid_client"}`. If you sent the credentials in the `Authorization` header, the response also carries a `WWW-Authenticate: Basic realm="oauth", error="invalid_client"` challenge, as RFC 6749 section 5.2 requires. Treat it as a permanent credential failure, not a retryable one.
+
+### Resource indicators (RFC 8707)
+
+three.ws issues access tokens for exactly one resource, its MCP server. If you pass `resource`, it must name that server; anything else is rejected with `invalid_target` at both `/api/oauth/authorize` and `/api/oauth/token`, rather than handing you a token whose audience is something you did not ask for. A trailing slash is ignored, and omitting the parameter entirely is fine.
+
+```bash
+# The canonical value, also published as `resource` in
+# /.well-known/oauth-protected-resource
+curl -X POST https://three.ws/api/oauth/token \
+ -d "grant_type=authorization_code" \
+ -d "client_id=$CLIENT_ID" \
+ -d "code=$CODE" \
+ -d "redirect_uri=$REDIRECT_URI" \
+ -d "code_verifier=$VERIFIER" \
+ -d "resource=https://three.ws/api/mcp"
+```
For most use cases, API keys are simpler. OAuth is the right choice when you're building a product where your users grant your app access to their three.ws data.