normalizeOAuthScope (oauth/scope.ts)

chat-ui · src/lib/server/mcp/oauth/scope.ts · typescript · Apache-2.0
auth

Utility for normalizing, selecting, and merging OAuth scopes safely

// Source: https://github.com/huggingface/chat-ui | License: Apache-2.0 | Copyright 2018- The Hugging Face team. All rights reserved
// Provided via justclaw.it.com; keep this notice with the code.
const MAX_SCOPE_LENGTH = 2048;
const MAX_SCOPE_COUNT = 50;
const SCOPE_TOKEN = /^[\x21\x23-\x5b\x5d-\x7e]+$/;

export function normalizeOAuthScope(scope: string | undefined): string | undefined {
	if (!scope) return undefined;
	if (scope.length > MAX_SCOPE_LENGTH) throw new Error("OAuth scope challenge is too large");
	const tokens = [...new Set(scope.trim().split(/\s+/).filter(Boolean))];
	if (tokens.length > MAX_SCOPE_COUNT || tokens.some((token) => !SCOPE_TOKEN.test(token))) {
		throw new Error("OAuth scope challenge is invalid");
	}
	return tokens.length ? tokens.join(" ") : undefined;
}

ex

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key