| 1 | import { createToken } from './_hmac.js' |
| 2 | |
| 3 | function jsonResponse(data, status = 200) { |
| 4 | return new Response(JSON.stringify(data), { |
| 5 | status, |
| 6 | headers: { 'Content-Type': 'application/json' }, |
| 7 | }) |
| 8 | } |
| 9 | |
| 10 | export const onRequestGet = async ({ env }) => { |
| 11 | if (!env.HMAC_SECRET) { |
| 12 | return jsonResponse({ error: 'Server misconfigured' }, 500) |
| 13 | } |
| 14 | |
| 15 | try { |
| 16 | const { token } = await createToken(env.HMAC_SECRET) |
| 17 | return jsonResponse({ token }) |
| 18 | } catch (err) { |
| 19 | console.error('Failed to create challenge token:', err) |
| 20 | return jsonResponse({ error: 'Failed to create challenge' }, 500) |
| 21 | } |
| 22 | } |