1import { agent, sessions } from "./state";
2
3export const GRANULAR_SCOPES = [
4 {
5 id: "create",
6 scope: "repo:*?action=create",
7 label: "Create records",
8 },
9 {
10 id: "update",
11 scope: "repo:*?action=update",
12 label: "Update records",
13 },
14 {
15 id: "delete",
16 scope: "repo:*?action=delete",
17 label: "Delete records",
18 },
19 {
20 id: "blob",
21 scope: "blob:*/*",
22 label: "Upload blobs",
23 },
24];
25
26export const BASE_SCOPES = ["atproto"];
27
28export const buildScopeString = (selected: Set<string>): string => {
29 const granular = GRANULAR_SCOPES.filter((s) => selected.has(s.id)).map((s) => s.scope);
30 return [...BASE_SCOPES, ...granular].join(" ");
31};
32
33export const scopeIdsToString = (scopeIds: Set<string>): string => {
34 return ["atproto", ...Array.from(scopeIds)].join(",");
35};
36
37export const parseScopeString = (scopeIdsString: string): Set<string> => {
38 if (!scopeIdsString) return new Set();
39 const ids = scopeIdsString.split(",").filter(Boolean);
40 return new Set(ids.filter((id) => id !== "atproto"));
41};
42
43export const hasScope = (grantedScopes: string | undefined, scopeId: string): boolean => {
44 if (!grantedScopes) return false;
45 return grantedScopes.split(",").includes(scopeId);
46};
47
48export const hasUserScope = (scopeId: string): boolean => {
49 if (!agent()) return false;
50 const grantedScopes = sessions[agent()!.sub]?.grantedScopes;
51 if (!grantedScopes) return true;
52 return hasScope(grantedScopes, scopeId);
53};