|
1 | | -import { verifyToken } from '@clerk/backend' |
2 | 1 | import type { Context, Next } from 'hono' |
3 | 2 |
|
4 | | -import { env } from '../config/env.js' |
| 3 | +import { getPrivyClient } from '@/config/verbs.js' |
5 | 4 |
|
6 | 5 | export interface AuthContext { |
7 | | - userId: string |
8 | | - clerkUserId: string |
9 | | - privyAuthKey?: string |
| 6 | + userId?: string |
10 | 7 | } |
11 | 8 |
|
12 | 9 | export async function authMiddleware(c: Context, next: Next) { |
13 | 10 | const authHeader = c.req.header('Authorization') |
14 | 11 |
|
15 | 12 | if (!authHeader?.startsWith('Bearer ')) { |
16 | | - return c.json({ error: 'Missing or invalid authorization header' }, 401) |
| 13 | + // TODO (https://github.com/ethereum-optimism/verbs/issues/124): enforce auth |
| 14 | + // Fail silently |
| 15 | + await next() |
| 16 | + return |
17 | 17 | } |
18 | 18 |
|
19 | | - const token = authHeader.substring(7) |
| 19 | + const accessToken = parseAuthorizationHeader(authHeader) |
| 20 | + const authContext: AuthContext = {} |
20 | 21 |
|
21 | 22 | try { |
22 | | - const verifiedToken = await verifyToken(token, { |
23 | | - secretKey: env.CLERK_SECRET_KEY, |
24 | | - // Accept both the publishable key and localhost origins for development |
25 | | - authorizedParties: [ |
26 | | - env.CLERK_PUBLISHABLE_KEY, |
27 | | - 'http://localhost:5173', |
28 | | - 'localhost:5173', |
29 | | - ], |
30 | | - }) |
31 | | - |
32 | | - if (!verifiedToken) { |
33 | | - return c.json({ error: 'Invalid token' }, 401) |
34 | | - } |
35 | | - |
36 | | - const userId = verifiedToken.sub |
37 | | - |
38 | | - const authContext: AuthContext = { |
39 | | - userId, |
40 | | - clerkUserId: userId, |
41 | | - } |
42 | | - |
43 | | - try { |
44 | | - // Get Privy authorization key for the authenticated user |
45 | | - // This enables user-owned wallets via authenticated signers |
46 | | - const authKeyResponse = await fetch( |
47 | | - `https://auth.privy.io/api/v1/authorization/init`, |
48 | | - { |
49 | | - method: 'POST', |
50 | | - headers: { |
51 | | - 'Content-Type': 'application/json', |
52 | | - 'privy-app-id': env.PRIVY_APP_ID, |
53 | | - Authorization: `Bearer ${token}`, |
54 | | - }, |
55 | | - }, |
56 | | - ) |
| 23 | + const privy = getPrivyClient() |
| 24 | + const verifiedPrivy = await privy |
| 25 | + .verifyAuthToken(accessToken) |
| 26 | + .catch((err) => { |
| 27 | + console.error('❌ Auth middleware: Token verification failed:', err) |
| 28 | + throw c.json({ error: 'Invalid or expired token' }, 401) |
| 29 | + }) |
| 30 | + const userId = verifiedPrivy.userId |
| 31 | + authContext.userId = userId |
| 32 | + } catch { |
| 33 | + // TODO (https://github.com/ethereum-optimism/verbs/issues/124): enforce auth |
| 34 | + // Silently continue without Privy auth key if request fails |
| 35 | + } |
57 | 36 |
|
58 | | - if (authKeyResponse.ok) { |
59 | | - const authKeyData = await authKeyResponse.json() |
60 | | - authContext.privyAuthKey = authKeyData.authorizationKey |
61 | | - } |
62 | | - } catch { |
63 | | - // Silently continue without Privy auth key if request fails |
64 | | - } |
| 37 | + c.set('auth', authContext) |
| 38 | + await next() |
| 39 | +} |
65 | 40 |
|
66 | | - c.set('auth', authContext) |
67 | | - await next() |
68 | | - } catch (error) { |
69 | | - console.error('❌ Auth middleware: Token verification failed:', error) |
70 | | - return c.json({ error: 'Invalid or expired token' }, 401) |
71 | | - } |
| 41 | +const parseAuthorizationHeader = (value: string) => { |
| 42 | + return value.replace('Bearer', '').trim() |
72 | 43 | } |
| 44 | + |
| 45 | +export const PRIVY_TOKEN_COOKIE_KEY = 'privy-token' |
0 commit comments