Skip to content

Instantly share code, notes, and snippets.

@joshtwist
Last active August 28, 2024 15:23
Show Gist options
  • Save joshtwist/0985720e81052f3e32d01a3026c8ddc3 to your computer and use it in GitHub Desktop.
Save joshtwist/0985720e81052f3e32d01a3026c8ddc3 to your computer and use it in GitHub Desktop.
==== Custom Roles-Based Access Control (RBAC) ==== rbac.ts
// Check user is in correct role, or return 403
if (request.user.data.roles.includes(options.role)) {
return request;
}
else {
return new Response('Access denied', { status: 403 });
}
==== Custom Quotas ==== custom-quotas.ts
// Quota on more than just requests
QuotaInboundPolicy.setMeters(context,
{
tokens: response.headers.get('tokens-used'),
computeUnits: response.headers.get('computeUnits')
});
=== Dynamic Rate Limiting === dynamic-rate-limiting.ts
// Load dynamic limits from DB (via cache for performance)
const limits = await loadLimitsFromDBViaCache(context);
// find the limit for the tier of this customer
const limit = limits[request.user.data.tier]
return {
key: user.sub,
requestsAllowed: limit,
timeWindowMinutes: 0.1 //(6s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment