-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.ts
32 lines (26 loc) · 979 Bytes
/
auth.ts
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
import { AccessToken, PeerClaims, PeerPolicy } from "@pulsebeam/server/workerd";
// This is an example Cloudflare Page Function for Serving PulseBeam Tokens
// For more details, see
// https://pulsebeam.dev/docs/guides/token/#cloudflare-page-functions
interface Env {
PULSEBEAM_API_KEY: string;
PULSEBEAM_API_SECRET: string;
}
export const onRequest: PagesFunction<Env> = async (context) => {
const url = new URL(context.request.url);
const groupId = url.searchParams.get("groupId");
const peerId = url.searchParams.get("peerId");
if (!groupId || !peerId) {
throw new Error("groupId and peerId are required");
}
const claims = new PeerClaims(groupId, peerId);
const rule = new PeerPolicy("*", "*");
claims.setAllowPolicy(rule);
claims.setAllowPolicy(rule);
const app = new AccessToken(
context.env.PULSEBEAM_API_KEY,
context.env.PULSEBEAM_API_SECRET,
);
const token = app.createToken(claims, 3600);
return new Response(token);
};