Jobley / website · jobley.ai / test / passkeys.test.mjs
| 1 | import { test } from 'node:test'; |
| 2 | import assert from 'node:assert/strict'; |
| 3 | import { createHash } from 'node:crypto'; |
| 4 | import { ADMIN, MODERATOR, call, makeEnv } from './helpers.mjs'; |
| 5 | import { Authenticator } from './authenticator.mjs'; |
| 6 | import { onRequest as middleware } from '../functions/_middleware.js'; |
| 7 | |
| 8 | const SECRET = 'a-setup-secret-that-is-at-least-32-characters-long'; |
| 9 | const setupEnv = (extra = {}) => makeEnv({ JOBLEY_PASSKEY_SETUP_SECRET: SECRET, ...extra }); |
| 10 | const setupLink = (env) => call(env, 'POST', '/api/passkeys/setup', { headers: { 'x-setup-secret': SECRET } }); |
| 11 | |
| 12 | // Adds a passkey with a setup link and returns the sign-in reply. |
| 13 | async function enroll(env, link, key = new Authenticator(), name = 'Test iPhone') { |
| 14 | const options = await call(env, 'POST', '/api/passkeys/register/options', { body: { code: link } }); |
| 15 | assert.equal(options.status, 200, JSON.stringify(options.data)); |
| 16 | return call(env, 'POST', '/api/passkeys/register', { body: { challenge_id: options.data.challenge_id, response: key.register(options.data.options), name }, client: 'iphone' }); |
| 17 | } |
| 18 | async function signIn(env, key) { |
| 19 | const options = await call(env, 'POST', '/api/passkeys/login/options', { body: {} }); |
| 20 | return { options, reply: await call(env, 'POST', '/api/passkeys/login', { body: { challenge_id: options.data.challenge_id, response: key.login(options.data.options) }, client: 'mac' }) }; |
| 21 | } |
| 22 | |
| 23 | test('registration options: rpID is the site domain, user verification required, discoverable credential', async () => { |
| 24 | const env = setupEnv(); |
| 25 | const link = await setupLink(env); |
| 26 | const r = await call(env, 'POST', '/api/passkeys/register/options', { body: { code: link.data.url } }); |
| 27 | assert.equal(r.status, 200); |
| 28 | const o = r.data.options; |
| 29 | assert.equal(o.rp.id, 'jobley.ai'); |
| 30 | assert.equal(o.rp.name, 'Jobley'); |
| 31 | assert.equal(o.authenticatorSelection.userVerification, 'required'); |
| 32 | assert.equal(o.authenticatorSelection.residentKey, 'required'); |
| 33 | assert.equal(o.authenticatorSelection.requireResidentKey, true); |
| 34 | assert.equal(o.attestation, 'none'); |
| 35 | assert.ok(o.pubKeyCredParams.some((p) => p.alg === -7)); |
| 36 | assert.match(r.data.challenge_id, /^ch_/); |
| 37 | }); |
| 38 | |
| 39 | test('sign-in options: rpID is the site domain, user verification required, no allow list (discoverable)', async () => { |
| 40 | const env = makeEnv(); |
| 41 | const r = await call(env, 'POST', '/api/passkeys/login/options', { body: {} }); |
| 42 | assert.equal(r.status, 200); |
| 43 | assert.equal(r.data.options.rpId, 'jobley.ai'); |
| 44 | assert.equal(r.data.options.userVerification, 'required'); |
| 45 | assert.ok(!r.data.options.allowCredentials || r.data.options.allowCredentials.length === 0); |
| 46 | }); |
| 47 | |
| 48 | test('setup endpoint: 404 without the secret, with a wrong one, a short one or none configured; 200 with it', async () => { |
| 49 | const env = setupEnv(); |
| 50 | assert.equal((await call(env, 'POST', '/api/passkeys/setup')).status, 404); |
| 51 | assert.equal((await call(env, 'POST', '/api/passkeys/setup', { headers: { 'x-setup-secret': SECRET + 'x' } })).status, 404); |
| 52 | assert.equal((await call(env, 'GET', '/api/passkeys/setup', { headers: { 'x-setup-secret': SECRET } })).status, 404); |
| 53 | const none = makeEnv(); |
| 54 | assert.equal((await call(none, 'POST', '/api/passkeys/setup', { headers: { 'x-setup-secret': 'anything' } })).status, 404); |
| 55 | const short = makeEnv({ JOBLEY_PASSKEY_SETUP_SECRET: 'too-short-secret' }); |
| 56 | assert.equal((await call(short, 'POST', '/api/passkeys/setup', { headers: { 'x-setup-secret': 'too-short-secret' } })).status, 404); |
| 57 | const ok = await setupLink(env); |
| 58 | assert.equal(ok.status, 200); |
| 59 | assert.match(ok.data.url, /^https:\/\/jobley\.ai\/passkey\/#jl_enroll_[0-9a-f]{64}$/); |
| 60 | const minutes = (Date.parse(ok.data.expires_at) - Date.now()) / 60000; |
| 61 | assert.ok(minutes > 14 && minutes <= 15, `expires in ${minutes} minutes`); |
| 62 | const stored = await env.DB.prepare('SELECT code_hash FROM passkey_enrollments').first(); |
| 63 | assert.equal(stored.code_hash, createHash('sha256').update(ok.data.code).digest('hex')); |
| 64 | }); |
| 65 | |
| 66 | test('the curator enrolls with the setup link once, then signs in with the passkey', async () => { |
| 67 | const env = setupEnv(); |
| 68 | const link = await setupLink(env); |
| 69 | const key = new Authenticator(); |
| 70 | const enrolled = await enroll(env, link.data.url, key); |
| 71 | assert.equal(enrolled.status, 201, JSON.stringify(enrolled.data)); |
| 72 | assert.equal(enrolled.data.role, 'curator'); |
| 73 | assert.match(enrolled.data.token, /^jl_session_[0-9a-f]{64}$/); |
| 74 | assert.equal((await call(env, 'GET', '/api/admin/me', { token: enrolled.data.token })).data.role, 'curator'); |
| 75 | |
| 76 | // The link works once: no new options, and a second registration is refused. |
| 77 | assert.equal((await call(env, 'POST', '/api/passkeys/register/options', { body: { code: link.data.url } })).status, 410); |
| 78 | |
| 79 | const { reply } = await signIn(env, key); |
| 80 | assert.equal(reply.status, 200, JSON.stringify(reply.data)); |
| 81 | assert.equal(reply.data.role, 'curator'); |
| 82 | const me = await call(env, 'GET', '/api/admin/me', { token: reply.data.token }); |
| 83 | assert.equal(me.data.role, 'curator'); |
| 84 | assert.equal((await call(env, 'POST', '/api/admin/sign-out', { token: reply.data.token, body: {} })).status, 200); |
| 85 | assert.equal((await call(env, 'GET', '/api/admin/me', { token: reply.data.token })).status, 401); |
| 86 | }); |
| 87 | |
| 88 | test('a setup link is single use even when two registrations race for it', async () => { |
| 89 | const env = setupEnv(); |
| 90 | const link = await setupLink(env); |
| 91 | const [a, b] = [new Authenticator(), new Authenticator()]; |
| 92 | const first = await call(env, 'POST', '/api/passkeys/register/options', { body: { code: link.data.code } }); |
| 93 | const second = await call(env, 'POST', '/api/passkeys/register/options', { body: { code: link.data.code } }); |
| 94 | const ok = await call(env, 'POST', '/api/passkeys/register', { body: { challenge_id: first.data.challenge_id, response: a.register(first.data.options) } }); |
| 95 | assert.equal(ok.status, 201); |
| 96 | const late = await call(env, 'POST', '/api/passkeys/register', { body: { challenge_id: second.data.challenge_id, response: b.register(second.data.options) } }); |
| 97 | assert.equal(late.status, 410); |
| 98 | assert.equal((await env.DB.prepare('SELECT COUNT(*) AS n FROM passkeys').first()).n, 1); |
| 99 | }); |
| 100 | |
| 101 | test('an expired setup link is refused', async () => { |
| 102 | const env = setupEnv(); |
| 103 | const link = await setupLink(env); |
| 104 | await env.DB.prepare(`UPDATE passkey_enrollments SET expires_at = ?`).bind(new Date(Date.now() - 1000).toISOString()).run(); |
| 105 | assert.equal((await call(env, 'POST', '/api/passkeys/register/options', { body: { code: link.data.url } })).status, 410); |
| 106 | }); |
| 107 | |
| 108 | test('challenges are single use: replaying a sign-in fails', async () => { |
| 109 | const env = setupEnv(); |
| 110 | const key = new Authenticator(); |
| 111 | await enroll(env, (await setupLink(env)).data.url, key); |
| 112 | const { options, reply } = await signIn(env, key); |
| 113 | assert.equal(reply.status, 200); |
| 114 | const replay = await call(env, 'POST', '/api/passkeys/login', { body: { challenge_id: options.data.challenge_id, response: key.login(options.data.options) } }); |
| 115 | assert.equal(replay.status, 400); |
| 116 | assert.equal(replay.data.error, 'challenge_expired'); |
| 117 | assert.equal((await env.DB.prepare('SELECT COUNT(*) AS n FROM passkey_challenges').first()).n, 0); |
| 118 | }); |
| 119 | |
| 120 | test('an expired challenge, a passkey without user verification, the wrong origin and an unknown passkey are refused', async () => { |
| 121 | const env = setupEnv(); |
| 122 | const key = new Authenticator(); |
| 123 | await enroll(env, (await setupLink(env)).data.url, key); |
| 124 | |
| 125 | const expired = await call(env, 'POST', '/api/passkeys/login/options', { body: {} }); |
| 126 | await env.DB.prepare(`UPDATE passkey_challenges SET expires_at = ?`).bind(new Date(Date.now() - 1000).toISOString()).run(); |
| 127 | assert.equal((await call(env, 'POST', '/api/passkeys/login', { body: { challenge_id: expired.data.challenge_id, response: key.login(expired.data.options) } })).status, 400); |
| 128 | |
| 129 | key.verify = false; |
| 130 | assert.equal((await signIn(env, key)).reply.status, 401); |
| 131 | key.verify = true; |
| 132 | key.origin = 'https://jobley.example'; |
| 133 | assert.equal((await signIn(env, key)).reply.status, 401); |
| 134 | assert.equal((await signIn(env, new Authenticator())).reply.status, 401); |
| 135 | |
| 136 | const noUV = new Authenticator({ verify: false }); |
| 137 | const link = await setupLink(env); |
| 138 | assert.equal((await enroll(env, link.data.url, noUV)).status, 400); |
| 139 | }); |
| 140 | |
| 141 | test('old pasted tokens no longer sign in: ADMIN_TOKEN and moderator tokens are refused', async () => { |
| 142 | const oldAdmin = 'dev-admin-token-not-for-production-0000'; |
| 143 | const env = makeEnv({ ADMIN_TOKEN: oldAdmin }); |
| 144 | assert.equal((await call(env, 'GET', '/api/admin/me', { token: oldAdmin })).status, 401); |
| 145 | // A moderator approved before passkeys, with the fingerprint of their old token. |
| 146 | const oldToken = 'jl_mod_' + 'ab'.repeat(32); |
| 147 | await env.DB.prepare(`INSERT INTO moderators (id, name, email, token_hash, active, created_at) VALUES ('m_old', 'Old', 'old@example.org', ?, 1, ?)`) |
| 148 | .bind(createHash('sha256').update(oldToken).digest('hex'), new Date().toISOString()).run(); |
| 149 | assert.equal((await call(env, 'GET', '/api/admin/me', { token: oldToken })).status, 401); |
| 150 | assert.equal((await call(env, 'GET', '/api/admin/me', { token: ADMIN })).status, 200); |
| 151 | }); |
| 152 | |
| 153 | test('a signed-in person adds a second passkey and cannot remove the last one', async () => { |
| 154 | const env = setupEnv(); |
| 155 | const first = new Authenticator(); |
| 156 | const enrolled = await enroll(env, (await setupLink(env)).data.url, first); |
| 157 | const token = enrolled.data.token; |
| 158 | const list = async () => (await call(env, 'GET', '/api/admin/passkeys', { token })).data.passkeys; |
| 159 | assert.equal((await list()).length, 1); |
| 160 | assert.equal((await list())[0].name, 'Test iPhone'); |
| 161 | assert.equal((await call(env, 'DELETE', `/api/admin/passkeys/${first.id}`, { token })).status, 409); |
| 162 | |
| 163 | const second = new Authenticator(); |
| 164 | const options = await call(env, 'POST', '/api/admin/passkeys/options', { token, body: {} }); |
| 165 | assert.equal(options.status, 200); |
| 166 | assert.deepEqual(options.data.options.excludeCredentials.map((c) => c.id), [first.id]); |
| 167 | assert.equal(options.data.options.user.id, first.userHandle); |
| 168 | // Agents can never add passkeys. |
| 169 | assert.equal((await call(env, 'POST', '/api/admin/passkeys/options', { token, body: {}, client: 'mcp' })).status, 403); |
| 170 | const added = await call(env, 'POST', '/api/admin/passkeys', { token, body: { challenge_id: options.data.challenge_id, response: second.register(options.data.options), name: 'Mac' } }); |
| 171 | assert.equal(added.status, 201, JSON.stringify(added.data)); |
| 172 | assert.equal((await list()).length, 2); |
| 173 | assert.equal((await signIn(env, second)).reply.data.role, 'curator'); |
| 174 | |
| 175 | // Remove the first (this session was made with it, so it ends too); the second still signs in and is now the last. |
| 176 | const fresh = (await signIn(env, second)).reply.data.token; |
| 177 | assert.equal((await call(env, 'DELETE', `/api/admin/passkeys/${first.id}`, { token: fresh })).status, 200); |
| 178 | assert.equal((await call(env, 'GET', '/api/admin/me', { token })).status, 401); |
| 179 | assert.equal((await call(env, 'DELETE', `/api/admin/passkeys/${second.id}`, { token: fresh })).status, 409); |
| 180 | assert.equal((await signIn(env, first)).reply.status, 401); |
| 181 | }); |
| 182 | |
| 183 | test('moderators: approval makes a one-time setup link, the passkey signs them in, access is limited, revoke ends it', async () => { |
| 184 | const env = makeEnv(); |
| 185 | const apply = await call(env, 'POST', '/api/moderators', { body: MODERATOR }); |
| 186 | assert.equal(apply.status, 201); |
| 187 | const ok = await call(env, 'POST', `/api/admin/moderator-applications/${apply.data.id}/approve`, { token: ADMIN, body: {} }); |
| 188 | assert.equal(ok.status, 201); |
| 189 | assert.equal(ok.data.token, undefined); |
| 190 | assert.match(ok.data.setup.url, /^https:\/\/jobley\.ai\/passkey\/#jl_enroll_[0-9a-f]{64}$/); |
| 191 | assert.equal((await call(env, 'POST', `/api/admin/moderator-applications/${apply.data.id}/approve`, { token: ADMIN, body: {} })).status, 409); |
| 192 | |
| 193 | const key = new Authenticator(); |
| 194 | const enrolled = await enroll(env, ok.data.setup.url, key); |
| 195 | assert.equal(enrolled.status, 201, JSON.stringify(enrolled.data)); |
| 196 | assert.equal(enrolled.data.role, 'moderator'); |
| 197 | assert.equal((await enroll(env, ok.data.setup.url, new Authenticator()).catch(() => ({ status: 410 }))).status, 410); |
| 198 | |
| 199 | const token = (await signIn(env, key)).reply.data.token; |
| 200 | const me = await call(env, 'GET', '/api/admin/me', { token }); |
| 201 | assert.deepEqual([me.data.role, me.data.name, me.data.can_see_candidates], ['moderator', MODERATOR.name, false]); |
| 202 | assert.equal((await call(env, 'GET', '/api/admin/candidates', { token })).status, 403); |
| 203 | assert.equal((await call(env, 'GET', '/api/admin/moderators', { token })).status, 403); |
| 204 | assert.equal((await call(env, 'POST', `/api/admin/moderators/${ok.data.moderator.id}/setup-link`, { token, body: {} })).status, 403); |
| 205 | await call(env, 'POST', `/api/admin/moderators/${ok.data.moderator.id}/access`, { token: ADMIN, body: { can_see_candidates: true } }); |
| 206 | assert.equal((await call(env, 'GET', '/api/admin/candidates', { token })).status, 200); |
| 207 | |
| 208 | // The curator can make a new setup link for an existing moderator (one who had a token before passkeys). |
| 209 | const again = await call(env, 'POST', `/api/admin/moderators/${ok.data.moderator.id}/setup-link`, { token: ADMIN, body: {} }); |
| 210 | assert.equal(again.status, 201); |
| 211 | |
| 212 | await call(env, 'POST', `/api/admin/moderators/${ok.data.moderator.id}/revoke`, { token: ADMIN, body: {} }); |
| 213 | assert.equal((await call(env, 'GET', '/api/admin/me', { token })).status, 401); |
| 214 | assert.equal((await signIn(env, key)).reply.status, 401); |
| 215 | assert.equal((await call(env, 'POST', '/api/passkeys/register/options', { body: { code: again.data.setup.url } })).status, 410); |
| 216 | }); |
| 217 | |
| 218 | test('apple-app-site-association lists the apps for webcredentials', async () => { |
| 219 | const res = await middleware({ request: new Request('https://jobley.ai/.well-known/apple-app-site-association'), next: async () => new Response('static', { status: 404 }) }); |
| 220 | assert.equal(res.status, 200); |
| 221 | assert.match(res.headers.get('content-type'), /^application\/json/); |
| 222 | assert.deepEqual(await res.json(), { webcredentials: { apps: ['22HV69L32T.com.protomota.jobley'] } }); |
| 223 | const page = await middleware({ request: new Request('https://jobley.ai/passkey/'), next: async () => new Response('static', { status: 404 }) }); |
| 224 | assert.equal(page.status, 200); |
| 225 | assert.match(await page.text(), /Add a passkey/); |
| 226 | }); |