forked from cloudflare/mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudflare-identity.ts
More file actions
287 lines (255 loc) · 9.43 KB
/
Copy pathcloudflare-identity.ts
File metadata and controls
287 lines (255 loc) · 9.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import { env as cloudflareEnv } from 'cloudflare:workers'
import { z } from 'zod'
import { fetchWithRetry } from '../utils/fetch-retry'
import {
AccountSchema,
AccountsSchema,
ACCOUNTS_PROBE_PAGE_SIZE,
MAX_STORED_ACCOUNTS,
UserSchema
} from './types'
import { OAuthError } from './workers-oauth-utils'
const env = cloudflareEnv as Env
const API_TOKEN_IDENTITY_CALLER = 'api_token_identity_probe'
const OAUTH_IDENTITY_CALLER = 'oauth_callback_identity_probe'
// /accounts enforces a minimum page size of five. That is still enough to
// prove the account-token invariant without fetching the 31-record user page.
const ACCOUNT_TOKEN_PROBE_PAGE_SIZE = 5
export type CloudflareTokenOwner = 'account' | 'unknown' | 'user'
const AccountIdentitySchema = z.object({
type: z.literal('account'),
account: AccountSchema
})
const UserIdentitySchema = z.object({
type: z.literal('user'),
user: UserSchema,
accounts: AccountsSchema,
accountCount: z.number().int().nonnegative().optional()
})
export const CloudflareIdentitySchema = z.discriminatedUnion('type', [
AccountIdentitySchema,
UserIdentitySchema
])
export type CloudflareIdentity = z.infer<typeof CloudflareIdentitySchema>
export type CloudflareUserIdentity = z.infer<typeof UserIdentitySchema>
type AccountPage = {
accounts: z.infer<typeof AccountsSchema>
totalCount?: number
}
const UserResponseSchema = z.object({
success: z.boolean().optional(),
result: UserSchema.nullable().optional()
})
const AccountsResponseSchema = z.object({
success: z.boolean().optional(),
result: AccountsSchema.optional(),
result_info: z
.object({
count: z.number().optional(),
total_count: z.number().optional()
})
.optional()
})
function retryAfterHeaders(...responses: Response[]): Record<string, string> {
return {
'Retry-After':
responses.find((response) => response.status === 429)?.headers.get('Retry-After') ?? '30'
}
}
function throwIdentityProbeError(...responses: [Response, ...Response[]]): never {
const statuses = responses.map((response) => response.status)
if (statuses.some((status) => status >= 500)) {
throw new OAuthError('server_error', 'Cloudflare API is temporarily unavailable', 502)
}
if (statuses.includes(429)) {
throw new OAuthError(
'temporarily_unavailable',
'Rate limited, try again later',
429,
retryAfterHeaders(...responses)
)
}
if (statuses.includes(401)) {
throw new OAuthError('invalid_token', 'Access token is invalid or expired', 401)
}
if (statuses.includes(403)) {
throw new OAuthError(
'insufficient_scope',
'Token lacks required user:read or account:read scope',
403
)
}
if (statuses.includes(400)) {
throw new OAuthError(
'invalid_token',
'Access token appears malformed; reauthenticate and try again',
401
)
}
throw new OAuthError('invalid_token', 'Access token is invalid or expired', statuses[0])
}
async function fetchProbe(url: string, accessToken: string, caller: string): Promise<Response> {
try {
return await fetchWithRetry(
url,
{ headers: { Authorization: `Bearer ${accessToken}` } },
{ caller }
)
} catch (error) {
console.error('Cloudflare API request failed', error)
throw new OAuthError('server_error', 'Cloudflare API is temporarily unavailable', 502)
}
}
function fetchUser(accessToken: string, caller: string): Promise<Response> {
return fetchProbe(`${env.CLOUDFLARE_API_BASE}/user`, accessToken, caller)
}
function fetchAccounts(
accessToken: string,
caller: string,
pageSize = ACCOUNTS_PROBE_PAGE_SIZE
): Promise<Response> {
return fetchProbe(`${env.CLOUDFLARE_API_BASE}/accounts?per_page=${pageSize}`, accessToken, caller)
}
async function parseUser(response: Response): Promise<CloudflareUserIdentity['user'] | null> {
try {
const parsed = UserResponseSchema.safeParse(await response.json())
if (parsed.success && parsed.data.success && parsed.data.result) return parsed.data.result
if (!parsed.success) {
console.error('Cloudflare API /user payload did not match expected shape', parsed.error)
}
} catch (error) {
console.error('Cloudflare API /user response is not valid JSON', error)
}
return null
}
async function parseAccounts(response: Response): Promise<AccountPage> {
try {
const parsed = AccountsResponseSchema.safeParse(await response.json())
if (!parsed.success) {
console.error('Cloudflare API /accounts payload did not match expected shape', parsed.error)
return { accounts: [] }
}
if (!parsed.data.success || !parsed.data.result) return { accounts: [] }
const accounts = parsed.data.result
const reported = parsed.data.result_info?.total_count
if (reported !== undefined && Number.isFinite(reported) && reported >= 0) {
return { accounts, totalCount: reported }
}
const pageCount = parsed.data.result_info?.count
const totalCount =
pageCount !== undefined && Number.isFinite(pageCount) && pageCount >= 0
? Math.max(pageCount, accounts.length)
: accounts.length
console.warn(
`Cloudflare API /accounts response is missing a valid result_info.total_count; ` +
`falling back to page count ${totalCount}`
)
return { accounts, totalCount }
} catch (error) {
console.error('Cloudflare API /accounts response is not valid JSON', error)
return { accounts: [] }
}
}
function accountIdentity(page: AccountPage): CloudflareIdentity {
if (page.accounts.length !== 1 || (page.totalCount !== undefined && page.totalCount !== 1)) {
throw new OAuthError(
'invalid_token',
'Account token must resolve to exactly one Cloudflare account',
401
)
}
return { type: 'account', account: page.accounts[0] }
}
function userIdentity(
user: CloudflareUserIdentity['user'],
page: AccountPage
): CloudflareUserIdentity {
if (page.totalCount !== undefined && page.totalCount > MAX_STORED_ACCOUNTS) {
return { type: 'user', user, accounts: [], accountCount: page.totalCount }
}
return { type: 'user', user, accounts: page.accounts }
}
async function resolveAccountCredential(
accessToken: string,
caller: string
): Promise<CloudflareIdentity> {
// One minimum-size page is enough to prove whether an account-owned token
// resolves to exactly one account without fetching the larger user page.
const accountsResponse = await fetchAccounts(accessToken, caller, ACCOUNT_TOKEN_PROBE_PAGE_SIZE)
if (!accountsResponse.ok) throwIdentityProbeError(accountsResponse)
return accountIdentity(await parseAccounts(accountsResponse))
}
async function resolveUserCredential(
accessToken: string,
caller: string
): Promise<CloudflareUserIdentity> {
const [userResponse, accountsResponse] = await Promise.all([
fetchUser(accessToken, caller),
fetchAccounts(accessToken, caller)
])
const failures = [userResponse, accountsResponse].filter((response) => !response.ok)
if (failures.length > 0) {
throwIdentityProbeError(failures[0], ...failures.slice(1))
}
const [user, accounts] = await Promise.all([
parseUser(userResponse),
parseAccounts(accountsResponse)
])
if (!user) {
throw new OAuthError('invalid_token', 'Failed to verify token: no user information', 401)
}
return userIdentity(user, accounts)
}
async function resolveLegacyCredential(
accessToken: string,
caller: string
): Promise<CloudflareIdentity> {
const [userResponse, accountsResponse] = await Promise.all([
fetchUser(accessToken, caller),
fetchAccounts(accessToken, caller)
])
if (!accountsResponse.ok) {
if (!userResponse.ok) throwIdentityProbeError(userResponse, accountsResponse)
throwIdentityProbeError(accountsResponse)
}
const [user, accounts] = await Promise.all([
userResponse.ok ? parseUser(userResponse) : Promise.resolve(null),
accountsResponse.ok ? parseAccounts(accountsResponse) : Promise.resolve({ accounts: [] })
])
if (user) return userIdentity(user, accounts)
// A transient /user failure cannot prove account ownership.
if (!userResponse.ok && userResponse.status >= 429) throwIdentityProbeError(userResponse)
return accountIdentity(accounts)
}
/** Resolve a direct Cloudflare API or OAuth credential using its ownership hint. */
export function resolveCloudflareCredential(
accessToken: string,
tokenOwner: CloudflareTokenOwner
): Promise<CloudflareIdentity> {
switch (tokenOwner) {
case 'account':
return resolveAccountCredential(accessToken, API_TOKEN_IDENTITY_CALLER)
case 'user':
return resolveUserCredential(accessToken, API_TOKEN_IDENTITY_CALLER)
case 'unknown':
return resolveLegacyCredential(accessToken, API_TOKEN_IDENTITY_CALLER)
}
}
/** Resolve the upstream Cloudflare OAuth grant used by this server's OAuth flow. */
export async function getCloudflareOAuthUser(accessToken: string): Promise<CloudflareUserIdentity> {
const [userResponse, accountsResponse] = await Promise.all([
fetchUser(accessToken, OAUTH_IDENTITY_CALLER),
fetchAccounts(accessToken, OAUTH_IDENTITY_CALLER)
])
if (!userResponse.ok && !accountsResponse.ok) {
throwIdentityProbeError(userResponse, accountsResponse)
}
const [user, accounts] = await Promise.all([
userResponse.ok ? parseUser(userResponse) : Promise.resolve(null),
accountsResponse.ok ? parseAccounts(accountsResponse) : Promise.resolve({ accounts: [] })
])
if (!user) {
throw new OAuthError('server_error', 'Failed to fetch user information from Cloudflare', 500)
}
return userIdentity(user, accounts)
}