-
Notifications
You must be signed in to change notification settings - Fork 0
Enforce service surface adapter contracts #199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3626c98
Enforce service surface adapter contracts
brylie 002e80c
Fix service contract lint checks
brylie 4b9c17a
Preserve manifest method types in parity test
brylie 3330c63
test: enforce adapter registration contracts
brylie 757abe5
test: cover all UI service manifest entries
brylie b2412d4
refactor: decouple token storage from MCP
brylie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,130 +1,15 @@ | ||
| import { createHash, randomBytes } from 'node:crypto'; | ||
| import { desc, eq } from 'drizzle-orm'; | ||
| import { getDb } from '$lib/server/store.js'; | ||
| import { accessTokens } from '$lib/server/db/schema.js'; | ||
|
|
||
| export interface AccessToken { | ||
| tokenHash: string; | ||
| clientLabel: string; | ||
| allowedDocumentIds: string[]; | ||
| allowedCollectionIds: string[]; | ||
| allowedSpaceIds: string[]; | ||
| createdAt: number; | ||
| revokedAt?: number; | ||
| } | ||
|
|
||
| function rowToToken(row: typeof accessTokens.$inferSelect): AccessToken { | ||
| return { | ||
| tokenHash: row.tokenHash, | ||
| clientLabel: row.clientLabel, | ||
| allowedDocumentIds: row.allowedDocumentIds, | ||
| allowedCollectionIds: row.allowedCollectionIds, | ||
| allowedSpaceIds: row.allowedSpaceIds, | ||
| createdAt: row.createdAt, | ||
| revokedAt: row.revokedAt ?? undefined | ||
| }; | ||
| } | ||
|
|
||
| /** Derives the stored lookup/comparison key for a bearer token — only this hash is ever persisted, never the raw token. */ | ||
| export function hashToken(token: string): string { | ||
| return createHash('sha256').update(token).digest('hex'); | ||
| } | ||
|
|
||
| /** Returns the raw bearer token once — only its hash is ever stored. */ | ||
| export function createToken(input: { | ||
| clientLabel: string; | ||
| allowedDocumentIds: string[]; | ||
| allowedCollectionIds: string[]; | ||
| allowedSpaceIds?: string[]; | ||
| }): { token: string; record: AccessToken } { | ||
| const token = `as_${randomBytes(24).toString('base64url')}`; | ||
| const record: AccessToken = { | ||
| tokenHash: hashToken(token), | ||
| clientLabel: input.clientLabel, | ||
| allowedDocumentIds: input.allowedDocumentIds, | ||
| allowedCollectionIds: input.allowedCollectionIds, | ||
| allowedSpaceIds: input.allowedSpaceIds ?? [], | ||
| createdAt: Date.now() | ||
| }; | ||
|
|
||
| getDb().insert(accessTokens).values(record).run(); | ||
|
|
||
| return { token, record }; | ||
| } | ||
|
|
||
| /** Verifies a raw bearer token and returns its (non-revoked) record, or null. */ | ||
| export function verifyToken(token: string): AccessToken | null { | ||
| const row = getDb() | ||
| .select() | ||
| .from(accessTokens) | ||
| .where(eq(accessTokens.tokenHash, hashToken(token))) | ||
| .get(); | ||
| if (!row || row.revokedAt) return null; | ||
| return rowToToken(row); | ||
| } | ||
|
|
||
| /** Lists all access tokens (including revoked ones), newest first, for the token-management UI. */ | ||
| export function listTokens(): AccessToken[] { | ||
| const rows = getDb().select().from(accessTokens).orderBy(desc(accessTokens.createdAt)).all(); | ||
| return rows.map(rowToToken); | ||
| } | ||
|
|
||
| /** A team member can revoke their own client's connection at any time — no admin action required (PRD). */ | ||
| export function revokeToken(tokenHash: string): void { | ||
| getDb() | ||
| .update(accessTokens) | ||
| .set({ revokedAt: Date.now() }) | ||
| .where(eq(accessTokens.tokenHash, tokenHash)) | ||
| .run(); | ||
| } | ||
|
|
||
| /** Persists an access grant for a newly created document to SQLite so subsequent tool calls succeed. */ | ||
| export function grantDocumentAccess(tokenHash: string, documentId: string): void { | ||
| const db = getDb(); | ||
| const row = db | ||
| .select({ allowedDocumentIds: accessTokens.allowedDocumentIds }) | ||
| .from(accessTokens) | ||
| .where(eq(accessTokens.tokenHash, tokenHash)) | ||
| .get(); | ||
| if (!row) return; | ||
| if (!row.allowedDocumentIds.includes(documentId)) { | ||
| db.update(accessTokens) | ||
| .set({ allowedDocumentIds: [...row.allowedDocumentIds, documentId] }) | ||
| .where(eq(accessTokens.tokenHash, tokenHash)) | ||
| .run(); | ||
| } | ||
| } | ||
|
|
||
| /** Persists an access grant for a newly created collection to SQLite so subsequent tool calls succeed. */ | ||
| export function grantCollectionAccess(tokenHash: string, collectionId: string): void { | ||
| const db = getDb(); | ||
| const row = db | ||
| .select({ allowedCollectionIds: accessTokens.allowedCollectionIds }) | ||
| .from(accessTokens) | ||
| .where(eq(accessTokens.tokenHash, tokenHash)) | ||
| .get(); | ||
| if (!row) return; | ||
| if (!row.allowedCollectionIds.includes(collectionId)) { | ||
| db.update(accessTokens) | ||
| .set({ allowedCollectionIds: [...row.allowedCollectionIds, collectionId] }) | ||
| .where(eq(accessTokens.tokenHash, tokenHash)) | ||
| .run(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * True when `token` may access `parentId` — either because it's directly | ||
| * allowlisted (per-Document/per-Collection grant), or because `spaceId` (the | ||
| * record's own catalog Space, when the caller has it — see | ||
| * services/permissions.ts's resolveParentWorkspaceContext) is one of the | ||
| * token's Space-level grants (#6). Resolved live against the token's current | ||
| * `allowedSpaceIds`, not backfilled onto individual records, so a Space | ||
| * grant automatically covers content created in that Space later. | ||
| * Compatibility exports for MCP transport callers. Token persistence belongs | ||
| * to the neutral server store so application services do not depend on MCP. | ||
| */ | ||
| export function tokenAllowsParent(token: AccessToken, parentId: string, spaceId?: string): boolean { | ||
| return ( | ||
| token.allowedDocumentIds.includes(parentId) || | ||
| token.allowedCollectionIds.includes(parentId) || | ||
| (spaceId !== undefined && token.allowedSpaceIds.includes(spaceId)) | ||
| ); | ||
| } | ||
| export { | ||
| createToken, | ||
| grantCollectionAccess, | ||
| grantDocumentAccess, | ||
| hashToken, | ||
| listTokens, | ||
| revokeToken, | ||
| tokenAllowsParent, | ||
| verifyToken, | ||
| type AccessToken | ||
| } from '$lib/server/token-store'; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.