authentication improvements

This commit is contained in:
Devin Zuczek
2026-07-06 21:08:24 -04:00
parent fbc1aedfcd
commit baf43bfe31
57 changed files with 536 additions and 146 deletions
+3 -6
View File
@@ -50,7 +50,7 @@ async function authedId(c: Context<App>): Promise<number | null> {
if (!authHeader.toLowerCase().startsWith('bearer ')) return null
const token = authHeader.slice('Bearer '.length)
const accountId = await validateAndGetAccountId(token)
const accountId = await validateAndGetAccountId(token, await c.env.JWT_SECRET.get())
if (!accountId) return null
const id = Number.parseInt(accountId, 10)
@@ -599,12 +599,9 @@ const app = new Hono<App>({ strict: false })
if (room.CreatorAccountId === accountId) return c.json(true)
// Otherwise the caller needs a room role at least as high as requested.
const roles = Array.isArray(room.Roles)
? (room.Roles as Array<Record<string, unknown>>)
: []
const roles = Array.isArray(room.Roles) ? (room.Roles as Array<Record<string, unknown>>) : []
const hasRole = roles.some(
(r) =>
r.AccountId === accountId && typeof r.Role === 'number' && r.Role >= (role || 0)
(r) => r.AccountId === accountId && typeof r.Role === 'number' && r.Role >= (role || 0)
)
return c.json(hasRole)
})
+4
View File
@@ -2,6 +2,10 @@ import type { HonoApp } from '@repo/hono-helpers'
import type { SharedHonoEnv, SharedHonoVariables } from '@repo/hono-helpers/src/types'
export type Env = SharedHonoEnv & {
// Shared Secrets Store binding for the HS256 JWT signing key. Resolve the value
// with `await env.JWT_SECRET.get()`; all workers bind the same store so tokens
// signed by `auth` verify here.
JWT_SECRET: SecretsStoreSecret
/**
* Base domain the share-link URL is derived from, e.g. `rec.example.com`.
* Injected at deploy time via `--var DOMAIN`; defaults in `wrangler.jsonc`
+3 -4
View File
@@ -1,10 +1,9 @@
/**
* Minimal HS256 JWT validation.
*
* Uses the same placeholder dev secret as the `auth` worker (`apps/auth/src/jwt.ts`).
* Swap both for a shared secret binding before this is used for anything real.
* The signing key is supplied by the caller from the shared `JWT_SECRET` Secrets
* Store binding (see context.ts) - the same key the `auth` worker signs with.
*/
const DEV_SECRET = 'dev-insecure-signing-key-change-me'
function base64urlToBytes(input: string): Uint8Array {
const padded = input.replace(/-/g, '+').replace(/_/g, '/')
@@ -22,7 +21,7 @@ function base64urlToBytes(input: string): Uint8Array {
*/
export async function validateAndGetAccountId(
token: string,
secret: string = DEV_SECRET
secret: string
): Promise<string | null> {
const parts = token.split('.')
if (parts.length !== 3) return null
+16 -12
View File
@@ -1,4 +1,4 @@
import { env } from 'cloudflare:test'
import { adminSecretsStore, env } from 'cloudflare:test'
import { exports } from 'cloudflare:workers'
import { beforeAll, describe, expect, test } from 'vitest'
@@ -44,6 +44,8 @@ const TEST_ROOMS = [
]
beforeAll(async () => {
// Seed the shared JWT signing key into the local Secrets Store so .get() resolves.
await adminSecretsStore(env.JWT_SECRET).create('test-signing-key')
await env.DB.prepare(
`CREATE TABLE IF NOT EXISTS rooms (
data TEXT NOT NULL,
@@ -75,9 +77,9 @@ beforeAll(async () => {
for (const stmt of IMAGES_SCHEMA_DDL) await env.DB.prepare(stmt).run()
})
// Mint a token the way the `auth` worker does, using the same dev secret, so the
// Mint a token the way the `auth` worker does, signing with the shared test key seeded into the JWT_SECRET store, so the
// api worker's validation accepts it. Kept inline to avoid a cross-package import.
const DEV_SECRET = 'dev-insecure-signing-key-change-me'
const TEST_SECRET = 'test-signing-key'
function b64url(input: ArrayBuffer | string): string {
const bytes = typeof input === 'string' ? new TextEncoder().encode(input) : new Uint8Array(input)
@@ -93,7 +95,7 @@ async function bearer(sub = '42'): Promise<Record<string, string>> {
)}`
const key = await crypto.subtle.importKey(
'raw',
new TextEncoder().encode(DEV_SECRET),
new TextEncoder().encode(TEST_SECRET),
{ name: 'HMAC', hash: 'SHA-256' },
false,
['sign']
@@ -325,10 +327,7 @@ describe('room server', () => {
})
test('POST /api/rooms/v1/verifyRole checks creator + room roles', async () => {
const verify = async (
fields: Record<string, string>,
sub?: string
): Promise<boolean> => {
const verify = async (fields: Record<string, string>, sub?: string): Promise<boolean> => {
const res = await exports.default.fetch(`${ORIGIN}/api/rooms/v1/verifyRole`, {
method: 'POST',
headers: {
@@ -618,7 +617,12 @@ describe('images', () => {
seed({ Id: 202, PlayerId: 700, CreatedAt: '2026-04-01T00:00:00.000Z' }),
seed({ Id: 203, PlayerId: 700, Accessibility: 0 }), // private → hidden
// Taken by someone else, but player 700 is tagged in it → feed only.
seed({ Id: 204, PlayerId: 999, TaggedPlayerIds: [700], CreatedAt: '2026-05-01T00:00:00.000Z' }),
seed({
Id: 204,
PlayerId: 999,
TaggedPlayerIds: [700],
CreatedAt: '2026-05-01T00:00:00.000Z',
}),
// Unrelated to 700 → in neither.
seed({ Id: 205, PlayerId: 999, TaggedPlayerIds: [111] }),
])
@@ -642,9 +646,9 @@ describe('images', () => {
expect(feed.map((i) => i.Id)).toEqual([204, 202, 201])
// A player with no photos → empty array on both.
expect(await (await exports.default.fetch(`${ORIGIN}/api/images/v4/player/424242`)).json()).toEqual(
[]
)
expect(
await (await exports.default.fetch(`${ORIGIN}/api/images/v4/player/424242`)).json()
).toEqual([])
expect(
await (await exports.default.fetch(`${ORIGIN}/api/images/v3/feed/player/424242`)).json()
).toEqual([])