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