| 1 | import type { Context } from 'hono' |
| 2 | import { HTTPException } |
| 3 | import { getOrganizationBillingStatus } |
| 4 | import { auth } |
| 5 | import type { statements |
| 6 | |
| 7 | type Statements |
| 8 | export type RequiredPermissions |
| 9 | [R |
| 10 | } |
| 11 | |
| 12 | export interface VerifiedApiKey |
| 13 | id: string |
| 14 | metadata: Record |
| 15 | organizationId: string |
| 16 | } |
| 17 | |
| 18 | const ACTIVE_STATUSES = new |
| 19 | |
| 20 | export function throwHttp( |
| 21 | status: 401 |
| 22 | message: string, |
| 23 | headers?: Record |
| 24 | ): never { |
| 25 | throw new HTTPException( |
| 26 | message, |
| 27 | res: headers |
| 28 | ? new Response( |
| 29 | status, |
| 30 | headers: { 'content-type': |
| 31 | }) |
| 32 | : undefined, |
| 33 | }) |
| 34 | } |
| 35 | |
| 36 | interface RateLimitInfo { |
| 37 | lastRequest?: Date |
| 38 | rateLimitEnabled?: boolean |
| 39 | rateLimitMax?: number |
| 40 | rateLimitTimeWindow?: number |
| 41 | requestCount?: number |
| 42 | } |
| 43 | |
| 44 | function computeRateLimitHeaders( |
| 45 | key: RateLimitInfo |
| 46 | ): Record<string |
| 47 | if (key.rateLimitEnabled |
| 48 | return null |
| 49 | } |
| 50 | const limit = |
| 51 | const window = |
| 52 | if (!(limit |
| 53 | return null |
| 54 | } |
| 55 | const used = |
| 56 | const remaining = |
| 57 | const lastRequestMs = |
| 58 | ? new Date( |
| 59 | : Date.now() |
| 60 | const resetSeconds = |
| 61 | 0, |
| 62 | Math.ceil((lastRequestMs |
| 63 | ) |
| 64 | return { |
| 65 | 'X-RateLimit-Limit': String( |
| 66 | 'X-RateLimit-Remaining': String( |
| 67 | 'X-RateLimit-Reset': String( |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | function readStoredPermissions( |
| 72 | metadata: Record |
| 73 | ): Record<string |
| 74 | const perms = |
| 75 | if (!perms || |
| 76 | return {} |
| 77 | } |
| 78 | return perms as |
| 79 | } |
| 80 | |
| 81 | function assertPermissions( |
| 82 | stored: Record |
| 83 | required: RequiredPermissions |
| 84 | ) { |
| 85 | for (const [resource |
| 86 | if (!actions || |
| 87 | continue |
| 88 | } |
| 89 | const granted = |
| 90 | for (const action |
| 91 | if (!granted.includes |
| 92 | throwHttp(403, `Missing permission: |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | export async function requireApiKey |
| 99 | c: Context |
| 100 | permissions?: RequiredPermissions |
| 101 | ): Promise<VerifiedApiKey |
| 102 | const key = |
| 103 | if (!key) |
| 104 | throwHttp(401, 'API key is required |
| 105 | } |
| 106 | |
| 107 | const result = |
| 108 | body: { key |
| 109 | headers: c.req |
| 110 | }) |
| 111 | |
| 112 | if (!(result. |
| 113 | const code = |
| 114 | if (code === |
| 115 | throwHttp(429, 'Rate limit exceeded |
| 116 | } |
| 117 | if (code === |
| 118 | throwHttp(429, 'API key usage limit reached |
| 119 | } |
| 120 | if (code === |
| 121 | throwHttp(401, 'API key has expired |
| 122 | } |
| 123 | if (code === |
| 124 | throwHttp(401, 'API key is disabled |
| 125 | } |
| 126 | throwHttp(401, 'Invalid API key |
| 127 | } |
| 128 | |
| 129 | const rateLimitHeaders = |
| 130 | if (rateLimitHeaders) { |
| 131 | for (const [name |
| 132 | c.header(name |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | const organizationId = |
| 137 | if (!organizationId) |
| 138 | throwHttp(401, 'API key is not linked to an organization |
| 139 | } |
| 140 | |
| 141 | const metadata = |
| 142 | |
| 143 | if (permissions) { |
| 144 | assertPermissions(readStoredPermissions(metadata |
| 145 | } |
| 146 | |
| 147 | const billing = |
| 148 | const hasActiveSubscription = |
| 149 | ACTIVE_STATUSES.has(sub |
| 150 | ) |
| 151 | if (!hasActiveSubscription) |
| 152 | throwHttp(402, 'Organization does not have an active Pro subscription |
| 153 | } |
| 154 | |
| 155 | return { id: |
| 156 | } |