chore: refactored mcp auth... again

This commit is contained in:
johnyeo
2026-06-05 17:50:17 +01:00
parent 305233aad3
commit 658ba34ba9
34 changed files with 586 additions and 452 deletions

View File

@@ -0,0 +1,36 @@
{
"name": "@autumn/auth",
"version": "0.0.1",
"author": "Autumn",
"type": "module",
"sideEffects": false,
"exports": {
".": {
"types": "./src/index.ts",
"import": "./src/index.ts",
"default": "./src/index.ts"
},
"./utils": {
"types": "./src/utils/index.ts",
"import": "./src/utils/index.ts",
"default": "./src/utils/index.ts"
},
"./oauth": {
"types": "./src/oauth/index.ts",
"import": "./src/oauth/index.ts",
"default": "./src/oauth/index.ts"
}
},
"files": ["src"],
"scripts": {
"build": "tsc",
"ts": "tsc --noEmit",
"prepack": "bun run build",
"prepublishOnly": "bun run build"
},
"devDependencies": {
"@types/bun": "^1.2.13",
"@types/node": "^18.19.3",
"typescript": "~5.8.3"
}
}

View File

@@ -0,0 +1,2 @@
export * from "./oauth/index.js";
export * from "./utils/index.js";

View File

@@ -0,0 +1 @@
export * from "./oauthUrls.js";

View File

@@ -0,0 +1,32 @@
const trimTrailingSlash = (url: string) =>
url.endsWith("/") ? url.slice(0, -1) : url;
export const getOAuthIssuerUrl = ({
authPath = "/api/auth",
baseUrl,
}: {
authPath?: string;
baseUrl: string;
}): string => trimTrailingSlash(new URL(authPath, baseUrl).href);
export const getProtectedResourceMetadataUrl = ({
resourceUrl,
}: {
resourceUrl: string;
}): string => {
const url = new URL(resourceUrl);
const path = url.pathname === "/" ? "" : url.pathname;
return new URL(`/.well-known/oauth-protected-resource${path}`, url).href;
};
export const getWwwAuthenticateHeader = ({
error,
resourceMetadataUrl,
}: {
error?: string;
resourceMetadataUrl: string;
}): string => {
const params = [`resource_metadata="${resourceMetadataUrl}"`];
if (error) params.push(`error="${error}"`);
return `Bearer ${params.join(", ")}`;
};

View File

@@ -0,0 +1,13 @@
const BEARER_PREFIX = "Bearer ";
export const getBearerToken = ({
headers,
}: {
headers: Headers;
}): string | undefined => {
const authorization = headers.get("authorization");
if (!authorization?.startsWith(BEARER_PREFIX)) return undefined;
const token = authorization.slice(BEARER_PREFIX.length).trim();
return token.length ? token : undefined;
};

View File

@@ -0,0 +1 @@
export * from "./getBearerToken.js";

View File

@@ -0,0 +1,34 @@
{
"compilerOptions": {
"allowJs": true,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"checkJs": true,
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"exactOptionalPropertyTypes": false,
"forceConsistentCasingInFileNames": true,
"incremental": false,
"isolatedModules": true,
"lib": ["dom", "dom.iterable", "es2024"],
"module": "Preserve",
"moduleResolution": "bundler",
"noFallthroughCasesInSwitch": true,
"noImplicitOverride": false,
"noImplicitReturns": false,
"noPropertyAccessFromIndexSignature": false,
"noUncheckedIndexedAccess": false,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noEmit": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"target": "es2022",
"types": ["bun", "node"],
"useUnknownInCatchVariables": true
},
"exclude": ["node_modules"],
"include": ["src/**/*.ts"]
}