dataforseo-mcp-server
Version:
CLI and MCP server for DataForSEO API — browse documentation and make authenticated API requests
54 lines • 2.4 kB
JavaScript
import { getAuthServerUrl } from "../config/index.js";
import { buildWwwAuthenticateHeader, oauthProtectedResourcePayload, resolveAuthHeader, } from "../core/api/resolve-auth.js";
import { getEnv } from "../core/env.js";
function isDebugEnabled() {
return getEnv("DEBUG") === "true";
}
function resourceMetadataUrl(req) {
return `${req.protocol}://${req.get("host")}/.well-known/oauth-protected-resource`;
}
export function createAuthMiddleware() {
return (req, res, next) => {
const metadataUrl = resourceMetadataUrl(req);
const resolved = resolveAuthHeader(req.headers.authorization);
if (!resolved.ok) {
if (isDebugEnabled()) {
console.log(`auth rejected: ${resolved.reason}`);
}
res.setHeader("WWW-Authenticate", buildWwwAuthenticateHeader(metadataUrl, resolved.reason));
res.status(401).json({
error: resolved.reason === "expired_bearer"
? "invalid_token"
: "invalid auth",
error_description: resolved.reason === "expired_bearer"
? "expired bearer token"
: "invalid auth",
});
return;
}
if (isDebugEnabled()) {
console.log(`auth source: ${resolved.source}`);
}
req.authHeader = resolved.authHeader;
next();
};
}
/**
* Always registered: OAuth discovery is the default auth path.
* Env credentials remain a request-time fallback in createAuthMiddleware.
*/
export function registerOAuthProtectedResourceRoutes(app) {
const protectedResourceHandler = (resourcePath) => (req, res) => {
const base = `${req.protocol}://${req.get("host")}`;
const resource = resourcePath ? `${base}/${resourcePath}` : base;
const payload = oauthProtectedResourcePayload(resource, getAuthServerUrl());
if (isDebugEnabled()) {
console.log(`.well-known/oauth-protected-resource resp payload: ${JSON.stringify(payload)}`);
}
res.json(payload);
};
app.get("/.well-known/oauth-protected-resource", protectedResourceHandler(""));
app.get("/.well-known/oauth-protected-resource/mcp", protectedResourceHandler("mcp"));
app.get("/.well-known/oauth-protected-resource/http", protectedResourceHandler("http"));
}
//# sourceMappingURL=auth-middleware.js.map