Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.
feat(mcp): generate and hash personal access tokens
spacesdrive7271fe28d695
diff --git a/backend/lib/mcp/accessToken.ts b/backend/lib/mcp/accessToken.tsnew file mode 100644index 0000000..4c8aea8--- /dev/null+++ b/backend/lib/mcp/accessToken.ts@@ -0,0 +1,35 @@+// personal access tokens for Claude Code and scripts: shown once, stored only as a SHA-256 hash++export type McpPermission = 'read' | 'write';++const PREFIX = 'twl_';+// 32 random bytes, so guessing is hopeless and a plain hash (no salt or stretching) is enough to store it+const TOKEN_BYTES = 32;+const TOKEN_PATTERN = /^twl_[A-Za-z0-9_-]{43}$/;++export function toBase64Url(bytes: Uint8Array): string {+ return btoa(String.fromCharCode(...bytes)).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');+}++export function generateAccessToken(): string {+ return PREFIX + toBase64Url(crypto.getRandomValues(new Uint8Array(TOKEN_BYTES)));+}++// the prefix lets the auth middleware tell a token from a Supabase JWT without a database call+export function isAccessToken(value: string): boolean {+ return TOKEN_PATTERN.test(value);+}++export async function hashAccessToken(token: string): Promise<string> {+ const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(token));+ return [...new Uint8Array(digest)].map(byte => byte.toString(16).padStart(2, '0')).join('');+}++// the last characters let a person tell their tokens apart in the list without the token itself being kept+export function accessTokenHint(token: string): string {+ return token.slice(-4);+}++export function canWrite(permission: McpPermission): boolean {+ return permission === 'write';+}diff --git a/backend/tests/lib/mcp/accessToken.test.ts b/backend/tests/lib/mcp/accessToken.test.tsnew file mode 100644index 0000000..4125d94--- /dev/null+++ b/backend/tests/lib/mcp/accessToken.test.ts@@ -0,0 +1,25 @@+import { test } from 'node:test';+import assert from 'node:assert/strict';+import { accessTokenHint, generateAccessToken, hashAccessToken, isAccessToken } from '../../../lib/mcp/accessToken.ts';++test('a new token has the twl_ prefix, 43 url-safe characters and is different every time', () => {+ const first = generateAccessToken();+ const second = generateAccessToken();+ assert.match(first, /^twl_[A-Za-z0-9_-]{43}$/);+ assert.notEqual(first, second);+ assert.ok(isAccessToken(first));+});++test('a Supabase JWT or a short string is not taken for an access token', () => {+ assert.equal(isAccessToken('eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ4In0.sig'), false);+ assert.equal(isAccessToken('twl_short'), false);+});++test('the stored hash is the SHA-256 hex of the token and never the token itself', async () => {+ const token = 'twl_' + 'a'.repeat(43);+ const hash = await hashAccessToken(token);+ assert.match(hash, /^[0-9a-f]{64}$/);+ assert.equal(hash, await hashAccessToken(token));+ assert.ok(!hash.includes('aaaa'));+ assert.equal(accessTokenHint(token), 'aaaa');+});