chore: refactored mcp auth... again
This commit is contained in:
36
packages/auth/package.json
Normal file
36
packages/auth/package.json
Normal 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"
|
||||
}
|
||||
}
|
||||
2
packages/auth/src/index.ts
Normal file
2
packages/auth/src/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from "./oauth/index.js";
|
||||
export * from "./utils/index.js";
|
||||
1
packages/auth/src/oauth/index.ts
Normal file
1
packages/auth/src/oauth/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./oauthUrls.js";
|
||||
32
packages/auth/src/oauth/oauthUrls.ts
Normal file
32
packages/auth/src/oauth/oauthUrls.ts
Normal 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(", ")}`;
|
||||
};
|
||||
13
packages/auth/src/utils/getBearerToken.ts
Normal file
13
packages/auth/src/utils/getBearerToken.ts
Normal 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;
|
||||
};
|
||||
1
packages/auth/src/utils/index.ts
Normal file
1
packages/auth/src/utils/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./getBearerToken.js";
|
||||
34
packages/auth/tsconfig.json
Normal file
34
packages/auth/tsconfig.json
Normal 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"]
|
||||
}
|
||||
Reference in New Issue
Block a user