Skip to content

Commit

Permalink
access-control: added refresh interval for policies (#2029)
Browse files Browse the repository at this point in the history
  • Loading branch information
gioelecerati authored Feb 12, 2024
1 parent 0b54ab7 commit de35e86
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
25 changes: 13 additions & 12 deletions packages/api/src/controllers/access-control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ const WEBHOOK_TIMEOUT = 30 * 1000;
const MAX_ALLOWED_VIEWERS_FOR_FREE_TIER = 30;
const app = Router();

interface HitRecord {
timestamp: number;
}

const playbackHits: Record<string, HitRecord[]> = {};
type GateConfig = {
refresh_interval: number;
rate_limit: number;
};

async function fireGateWebhook(
webhook: DBWebhook,
Expand Down Expand Up @@ -133,16 +132,18 @@ app.post(

const playbackPolicyType = content.playbackPolicy?.type ?? "public";

let response = {};
let config: Partial<GateConfig> = {};

if (user.createdAt > HACKER_DISABLE_CUTOFF_DATE) {
if (isFreeTierUser(user)) {
response = {
rateLimit: MAX_ALLOWED_VIEWERS_FOR_FREE_TIER,
};
config.rate_limit = MAX_ALLOWED_VIEWERS_FOR_FREE_TIER;
}
}

if (content.playbackPolicy?.refreshInterval) {
config.refresh_interval = content.playbackPolicy.refreshInterval;
}

if (
playbackPolicyType !== "public" &&
req.body.pub === req.config.accessControlAdminPubkey &&
Expand All @@ -156,7 +157,7 @@ app.post(
switch (playbackPolicyType) {
case "public":
res.status(200);
return res.json(response);
return res.json(config);
case "jwt":
if (!req.body.pub) {
console.log(`
Expand Down Expand Up @@ -215,7 +216,7 @@ app.post(

tracking.recordSigningKeyValidation(signingKey.id);
res.status(200);
return res.json(response);
return res.json(config);
case "webhook":
if (!req.body.accessKey || req.body.type !== "accessKey") {
throw new ForbiddenError(
Expand All @@ -240,7 +241,7 @@ app.post(
);
if (statusCode >= 200 && statusCode < 300) {
res.status(200);
return res.json(response);
return res.json(config);
} else if (statusCode === 0) {
console.log(`
access-control: gate: content with playbackId=${playbackId} is gated but webhook=${webhook.id} failed, disallowing playback
Expand Down
5 changes: 5 additions & 0 deletions packages/api/src/schema/api-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2044,6 +2044,11 @@ components:
type: object
description: User-defined webhook context
additionalProperties: true
refreshInterval:
type: number
description: |
Interval (in seconds) at which the playback policy should be
refreshed (default 600 seconds)
room:
type: object
required:
Expand Down

0 comments on commit de35e86

Please sign in to comment.