@atproto/oauth-scopes 0.0.0-spaces-alpha-20260818021221

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/CHANGELOG.md +220 -0
  2. package/LICENSE.txt +7 -0
  3. package/dist/atproto-oauth-scope.d.ts +17 -0
  4. package/dist/atproto-oauth-scope.d.ts.map +1 -0
  5. package/dist/atproto-oauth-scope.js +63 -0
  6. package/dist/atproto-oauth-scope.js.map +1 -0
  7. package/dist/index.d.ts +13 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +13 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/lib/lexicon.d.ts +26 -0
  12. package/dist/lib/lexicon.d.ts.map +1 -0
  13. package/dist/lib/lexicon.js +2 -0
  14. package/dist/lib/lexicon.js.map +1 -0
  15. package/dist/lib/mime.d.ts +7 -0
  16. package/dist/lib/mime.d.ts.map +1 -0
  17. package/dist/lib/mime.js +61 -0
  18. package/dist/lib/mime.js.map +1 -0
  19. package/dist/lib/nsid.d.ts +3 -0
  20. package/dist/lib/nsid.d.ts.map +1 -0
  21. package/dist/lib/nsid.js +3 -0
  22. package/dist/lib/nsid.js.map +1 -0
  23. package/dist/lib/parser.d.ts +29 -0
  24. package/dist/lib/parser.d.ts.map +1 -0
  25. package/dist/lib/parser.js +127 -0
  26. package/dist/lib/parser.js.map +1 -0
  27. package/dist/lib/resource-permission.d.ts +10 -0
  28. package/dist/lib/resource-permission.d.ts.map +1 -0
  29. package/dist/lib/resource-permission.js +2 -0
  30. package/dist/lib/resource-permission.js.map +1 -0
  31. package/dist/lib/syntax-lexicon.d.ts +17 -0
  32. package/dist/lib/syntax-lexicon.d.ts.map +1 -0
  33. package/dist/lib/syntax-lexicon.js +62 -0
  34. package/dist/lib/syntax-lexicon.js.map +1 -0
  35. package/dist/lib/syntax-string.d.ts +16 -0
  36. package/dist/lib/syntax-string.d.ts.map +1 -0
  37. package/dist/lib/syntax-string.js +102 -0
  38. package/dist/lib/syntax-string.js.map +1 -0
  39. package/dist/lib/syntax.d.ts +24 -0
  40. package/dist/lib/syntax.d.ts.map +1 -0
  41. package/dist/lib/syntax.js +22 -0
  42. package/dist/lib/syntax.js.map +1 -0
  43. package/dist/lib/util.d.ts +7 -0
  44. package/dist/lib/util.d.ts.map +1 -0
  45. package/dist/lib/util.js +15 -0
  46. package/dist/lib/util.js.map +1 -0
  47. package/dist/scope-missing-error.d.ts +9 -0
  48. package/dist/scope-missing-error.d.ts.map +1 -0
  49. package/dist/scope-missing-error.js +15 -0
  50. package/dist/scope-missing-error.js.map +1 -0
  51. package/dist/scope-permissions-transition.d.ts +21 -0
  52. package/dist/scope-permissions-transition.d.ts.map +1 -0
  53. package/dist/scope-permissions-transition.js +58 -0
  54. package/dist/scope-permissions-transition.js.map +1 -0
  55. package/dist/scope-permissions.d.ts +25 -0
  56. package/dist/scope-permissions.d.ts.map +1 -0
  57. package/dist/scope-permissions.js +72 -0
  58. package/dist/scope-permissions.js.map +1 -0
  59. package/dist/scopes/account-permission.d.ts +35 -0
  60. package/dist/scopes/account-permission.d.ts.map +1 -0
  61. package/dist/scopes/account-permission.js +55 -0
  62. package/dist/scopes/account-permission.js.map +1 -0
  63. package/dist/scopes/blob-permission.d.ts +27 -0
  64. package/dist/scopes/blob-permission.d.ts.map +1 -0
  65. package/dist/scopes/blob-permission.js +73 -0
  66. package/dist/scopes/blob-permission.js.map +1 -0
  67. package/dist/scopes/identity-permission.d.ts +25 -0
  68. package/dist/scopes/identity-permission.d.ts.map +1 -0
  69. package/dist/scopes/identity-permission.js +39 -0
  70. package/dist/scopes/identity-permission.js.map +1 -0
  71. package/dist/scopes/include-scope.d.ts +55 -0
  72. package/dist/scopes/include-scope.d.ts.map +1 -0
  73. package/dist/scopes/include-scope.js +165 -0
  74. package/dist/scopes/include-scope.js.map +1 -0
  75. package/dist/scopes/repo-permission.d.ts +40 -0
  76. package/dist/scopes/repo-permission.d.ts.map +1 -0
  77. package/dist/scopes/repo-permission.js +81 -0
  78. package/dist/scopes/repo-permission.js.map +1 -0
  79. package/dist/scopes/rpc-permission.d.ts +38 -0
  80. package/dist/scopes/rpc-permission.d.ts.map +1 -0
  81. package/dist/scopes/rpc-permission.js +59 -0
  82. package/dist/scopes/rpc-permission.js.map +1 -0
  83. package/dist/scopes/space-permission.d.ts +111 -0
  84. package/dist/scopes/space-permission.d.ts.map +1 -0
  85. package/dist/scopes/space-permission.js +195 -0
  86. package/dist/scopes/space-permission.js.map +1 -0
  87. package/dist/scopes-set.d.ts +34 -0
  88. package/dist/scopes-set.d.ts.map +1 -0
  89. package/dist/scopes-set.js +102 -0
  90. package/dist/scopes-set.js.map +1 -0
  91. package/package.json +43 -0
@@ -0,0 +1,195 @@
1
+ import { isValidDid, isValidRecordKey } from '@atproto/syntax';
2
+ import { isNsid } from '../lib/nsid.js';
3
+ import { Parser } from '../lib/parser.js';
4
+ import { ScopeStringSyntax } from '../lib/syntax-string.js';
5
+ import { isScopeStringFor, } from '../lib/syntax.js';
6
+ import { knownValuesValidator } from '../lib/util.js';
7
+ export const SPACE_ACTIONS = Object.freeze([
8
+ 'read_self',
9
+ 'read',
10
+ 'create',
11
+ 'update',
12
+ 'delete',
13
+ ]);
14
+ export const isSpaceAction = knownValuesValidator(SPACE_ACTIONS);
15
+ // `read_self` is omitted because `read` already implies it.
16
+ export const SPACE_DEFAULT_ACTIONS = Object.freeze([
17
+ 'read',
18
+ 'create',
19
+ 'update',
20
+ 'delete',
21
+ ]);
22
+ export const SPACE_MANAGE_OPS = Object.freeze([
23
+ 'create',
24
+ 'update',
25
+ 'delete',
26
+ ]);
27
+ export const isSpaceManageOp = knownValuesValidator(SPACE_MANAGE_OPS);
28
+ export const isSpaceTypeParam = (value) => value === '*' || isNsid(value);
29
+ const isDidString = (value) => typeof value === 'string' && isValidDid(value);
30
+ export const isSpaceAuthorityParam = (value) => value === '*' || value === 'self' || isDidString(value);
31
+ export const isSpaceSkeyParam = (value) => typeof value === 'string' && isValidRecordKey(value);
32
+ export const isSpaceCollectionParam = (value) => value === '*' || isNsid(value);
33
+ export class SpacePermission {
34
+ constructor(type, authority, skey, collection, action, manage) {
35
+ this.type = type;
36
+ this.authority = authority;
37
+ this.skey = skey;
38
+ this.collection = collection;
39
+ this.action = action;
40
+ this.manage = manage;
41
+ }
42
+ matches(target) {
43
+ // An unresolved `self` authority matches nothing, since `target.authority`
44
+ // is always a concrete DID.
45
+ if (this.type !== '*' && this.type !== target.type)
46
+ return false;
47
+ if (this.authority !== '*' && this.authority !== target.authority) {
48
+ return false;
49
+ }
50
+ if (this.skey !== '*' && this.skey !== target.skey)
51
+ return false;
52
+ if (target.action === undefined) {
53
+ return this.manage.includes(target.manage);
54
+ }
55
+ // Reads are collection-independent, and `read` implies `read_self`.
56
+ if (target.action === 'read') {
57
+ return this.action.includes('read');
58
+ }
59
+ if (target.action === 'read_self') {
60
+ return this.action.includes('read') || this.action.includes('read_self');
61
+ }
62
+ return (this.action.includes(target.action) &&
63
+ this.collectionAllows(target.collection));
64
+ }
65
+ collectionAllows(collection) {
66
+ return (this.collection.includes('*') ||
67
+ this.collection.includes(collection));
68
+ }
69
+ get hasCollections() {
70
+ return this.collection.length > 0;
71
+ }
72
+ /**
73
+ * Materializes a space type's declared collections into a bare
74
+ * `space:<type>` grant. Called at token-issuance time, since the matcher is
75
+ * context-free and cannot resolve declarations itself.
76
+ */
77
+ withDefaultCollections(collections) {
78
+ if (this.hasCollections || collections.length === 0)
79
+ return this;
80
+ return new SpacePermission(this.type, this.authority, this.skey, collections, this.action, this.manage);
81
+ }
82
+ get isSelfAuthority() {
83
+ return this.authority === 'self';
84
+ }
85
+ /**
86
+ * Resolves an `authority` of `self` to the granting user's DID. Called at
87
+ * token-issuance time, alongside {@link withDefaultCollections}.
88
+ */
89
+ withResolvedAuthority(userDid) {
90
+ if (this.authority !== 'self')
91
+ return this;
92
+ return new SpacePermission(this.type, userDid, this.skey, this.collection, this.action, this.manage);
93
+ }
94
+ toString() {
95
+ return SpacePermission.parser.format(this);
96
+ }
97
+ static { this.parser = new Parser('space', {
98
+ type: {
99
+ multiple: false,
100
+ required: true,
101
+ validate: isSpaceTypeParam,
102
+ },
103
+ authority: {
104
+ multiple: false,
105
+ required: false,
106
+ default: 'self',
107
+ validate: isSpaceAuthorityParam,
108
+ },
109
+ skey: {
110
+ multiple: false,
111
+ required: false,
112
+ default: '*',
113
+ validate: (value) => value === '*' || isSpaceSkeyParam(value),
114
+ },
115
+ collection: {
116
+ multiple: true,
117
+ required: false,
118
+ validate: isSpaceCollectionParam,
119
+ // Empty means no write targets, not "all collections".
120
+ default: [],
121
+ normalize: (value) => {
122
+ if (value.length > 1) {
123
+ if (value.includes('*'))
124
+ return ['*'];
125
+ return [...new Set(value)].sort();
126
+ }
127
+ return value;
128
+ },
129
+ },
130
+ action: {
131
+ multiple: true,
132
+ required: false,
133
+ validate: isSpaceAction,
134
+ default: SPACE_DEFAULT_ACTIONS,
135
+ normalize: (value) => {
136
+ return SPACE_ACTIONS.filter(includedIn, value);
137
+ },
138
+ },
139
+ manage: {
140
+ multiple: true,
141
+ required: false,
142
+ validate: isSpaceManageOp,
143
+ default: [],
144
+ normalize: (value) => {
145
+ return SPACE_MANAGE_OPS.filter(includedIn, value);
146
+ },
147
+ },
148
+ }, 'type'); }
149
+ static fromString(scope) {
150
+ if (!isScopeStringFor(scope, 'space'))
151
+ return null;
152
+ const syntax = ScopeStringSyntax.fromString(scope);
153
+ return SpacePermission.fromSyntax(syntax);
154
+ }
155
+ static fromSyntax(syntax) {
156
+ const result = SpacePermission.parser.parse(syntax);
157
+ if (!result)
158
+ return null;
159
+ return new SpacePermission(result.type, result.authority, result.skey, result.collection, result.action, result.manage);
160
+ }
161
+ static scopeNeededFor(options) {
162
+ const base = {
163
+ type: options.type,
164
+ authority: options.authority,
165
+ skey: options.skey,
166
+ };
167
+ // Omitting `action` would default it to the full record action list, so
168
+ // pair the verb with `read_self` — the narrowest action expressible — to
169
+ // avoid suggesting read/write over every record.
170
+ if ('manage' in options && options.manage !== undefined) {
171
+ return SpacePermission.parser.format({
172
+ ...base,
173
+ collection: [],
174
+ action: ['read_self'],
175
+ manage: [options.manage],
176
+ });
177
+ }
178
+ const collection = (options.action === 'read' ||
179
+ options.action === 'read_self'
180
+ ? []
181
+ : [
182
+ options.collection,
183
+ ]);
184
+ return SpacePermission.parser.format({
185
+ ...base,
186
+ collection,
187
+ action: [options.action],
188
+ manage: [],
189
+ });
190
+ }
191
+ }
192
+ function includedIn(value) {
193
+ return this.includes(value);
194
+ }
195
+ //# sourceMappingURL=space-permission.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"space-permission.js","sourceRoot":"","sources":["../../src/scopes/space-permission.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAC9D,OAAO,EAAa,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAClD,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AAEzC,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAA;AAC3D,OAAO,EAIL,gBAAgB,GACjB,MAAM,kBAAkB,CAAA;AACzB,OAAO,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAA;AAErD,MAAM,CAAC,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,CAAC;IACzC,WAAW;IACX,MAAM;IACN,QAAQ;IACR,QAAQ;IACR,QAAQ;CACA,CAAC,CAAA;AAEX,MAAM,CAAC,MAAM,aAAa,GAAG,oBAAoB,CAAC,aAAa,CAAC,CAAA;AAEhE,4DAA4D;AAC5D,MAAM,CAAC,MAAM,qBAAqB,GAAG,MAAM,CAAC,MAAM,CAAC;IACjD,MAAM;IACN,QAAQ;IACR,QAAQ;IACR,QAAQ;CACA,CAAC,CAAA;AAEX,MAAM,CAAC,MAAM,gBAAgB,GAAG,MAAM,CAAC,MAAM,CAAC;IAC5C,QAAQ;IACR,Q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port { Parser } from '../lib/parser.js'\nimport type { ResourcePermission } from '../lib/resource-permission.js'\nimport { ScopeStringSyntax } from '../lib/syntax-string.js'\nimport {\n type NeArray,\n type NeRoArray,\n type ScopeSyntax,\n isScopeStringFor,\n} from '../lib/syntax.js'\nimport { knownValuesValidator } from '../lib/util.js'\n\nexport const SPACE_ACTIONS = Object.freeze([\n 'read_self',\n 'read',\n 'create',\n 'update',\n 'delete',\n] as const)\nexport type SpaceAction = (typeof SPACE_ACTIONS)[number]\nexport const isSpaceAction = knownValuesValidator(SPACE_ACTIONS)\n\n// `read_self` is omitted because `read` already implies it.\nexport const SPACE_DEFAULT_ACTIONS = Object.freeze([\n 'read',\n 'create',\n 'update',\n 'delete',\n] as const)\n\nexport const SPACE_MANAGE_OPS = Object.freeze([\n 'create',\n 'update',\n 'delete',\n] as const)\nexport type SpaceManageOp = (typeof SPACE_MANAGE_OPS)[number]\nexport const isSpaceManageOp = knownValuesValidator(SPACE_MANAGE_OPS)\n\nexport type SpaceTypeParam = '*' | Nsid\nexport const isSpaceTypeParam = (value: unknown): value is SpaceTypeParam =>\n value === '*' || isNsid(value)\n\ntype DidString = `did:${string}:${string}`\nconst isDidString = (value: unknown): value is DidString =>\n typeof value === 'string' && isValidDid(value)\nexport type SpaceAuthorityParam = '*' | 'self' | DidString\nexport const isSpaceAuthorityParam = (\n value: unknown,\n): value is SpaceAuthorityParam =>\n value === '*' || value === 'self' || isDidString(value)\n\n// A space key has the same syntax as a record key.\nexport type SpaceSkeyParam = string\nexport const isSpaceSkeyParam = (value: unknown): value is SpaceSkeyParam =>\n typeof value === 'string' && isValidRecordKey(value)\n\nexport type SpaceCollectionParam = '*' | Nsid\nexport const isSpaceCollectionParam = (\n value: unknown,\n): value is SpaceCollectionParam => value === '*' || isNsid(value)\n\nexport type SpacePermissionMatch = {\n type: string\n authority: string\n skey: string\n} & (\n | { action: 'read'; collection?: never; manage?: never }\n | { action: 'read_self'; collection?: never; manage?: never }\n | {\n action: 'create' | 'update' | 'delete'\n collection: string\n manage?: never\n }\n | { action?: never; collection?: never; manage: SpaceManageOp }\n)\n\nexport class SpacePermission implements ResourcePermission<\n 'space',\n SpacePermissionMatch\n> {\n constructor(\n public readonly type: SpaceTypeParam,\n public readonly authority: SpaceAuthorityParam,\n public readonly skey: SpaceSkeyParam | '*',\n public readonly collection: NeRoArray<SpaceCollectionParam>,\n public readonly action: NeRoArray<SpaceAction>,\n public readonly manage: NeRoArray<SpaceManageOp>,\n ) {}\n\n matches(target: SpacePermissionMatch) {\n // An unresolved `self` authority matches nothing, since `target.authority`\n // is always a concrete DID.\n if (this.type !== '*' && this.type !== target.type) return false\n if (this.authority !== '*' && this.authority !== target.authority) {\n return false\n }\n if (this.skey !== '*' && this.skey !== target.skey) return false\n\n if (target.action === undefined) {\n return this.manage.includes(target.manage)\n }\n\n // Reads are collection-independent, and `read` implies `read_self`.\n if (target.action === 'read') {\n return this.action.includes('read')\n }\n if (target.action === 'read_self') {\n return this.action.includes('read') || this.action.includes('read_self')\n }\n\n return (\n this.action.includes(target.action) &&\n this.collectionAllows(target.collection)\n )\n }\n\n private collectionAllows(collection: string): boolean {\n return (\n this.collection.includes('*') ||\n (this.collection as readonly string[]).includes(collection)\n )\n }\n\n get hasCollections(): boolean {\n return this.collection.length > 0\n }\n\n /**\n * Materializes a space type's declared collections into a bare\n * `space:<type>` grant. Called at token-issuance time, since the matcher is\n * context-free and cannot resolve declarations itself.\n */\n withDefaultCollections(\n collections: readonly SpaceCollectionParam[],\n ): SpacePermission {\n if (this.hasCollections || collections.length === 0) return this\n return new SpacePermission(\n this.type,\n this.authority,\n this.skey,\n collections as NeRoArray<SpaceCollectionParam>,\n this.action,\n this.manage,\n )\n }\n\n get isSelfAuthority(): boolean {\n return this.authority === 'self'\n }\n\n /**\n * Resolves an `authority` of `self` to the granting user's DID. Called at\n * token-issuance time, alongside {@link withDefaultCollections}.\n */\n withResolvedAuthority(userDid: DidString): SpacePermission {\n if (this.authority !== 'self') return this\n return new SpacePermission(\n this.type,\n userDid,\n this.skey,\n this.collection,\n this.action,\n this.manage,\n )\n }\n\n toString() {\n return SpacePermission.parser.format(this)\n }\n\n protected static readonly parser = new Parser(\n 'space',\n {\n type: {\n multiple: false,\n required: true,\n validate: isSpaceTypeParam,\n },\n authority: {\n multiple: false,\n required: false,\n default: 'self' as const,\n validate: isSpaceAuthorityParam,\n },\n skey: {\n multiple: false,\n required: false,\n default: '*' as const,\n validate: (value): value is SpaceSkeyParam | '*' =>\n value === '*' || isSpaceSkeyParam(value),\n },\n collection: {\n multiple: true,\n required: false,\n validate: isSpaceCollectionParam,\n // Empty means no write targets, not \"all collections\".\n default: [] as unknown as NeRoArray<SpaceCollectionParam>,\n normalize: (value) => {\n if (value.length > 1) {\n if (value.includes('*')) return ['*'] as const\n return [...new Set(value)].sort() as NeArray<Nsid>\n }\n return value as ['*' | Nsid]\n },\n },\n action: {\n multiple: true,\n required: false,\n validate: isSpaceAction,\n default: SPACE_DEFAULT_ACTIONS as unknown as NeRoArray<SpaceAction>,\n normalize: (value) => {\n return SPACE_ACTIONS.filter(includedIn, value) as NeArray<SpaceAction>\n },\n },\n manage: {\n multiple: true,\n required: false,\n validate: isSpaceManageOp,\n default: [] as unknown as NeRoArray<SpaceManageOp>,\n normalize: (value) => {\n return SPACE_MANAGE_OPS.filter(\n includedIn,\n value,\n ) as NeArray<SpaceManageOp>\n },\n },\n },\n 'type',\n )\n\n static fromString(scope: string): SpacePermission | null {\n if (!isScopeStringFor(scope, 'space')) return null\n const syntax = ScopeStringSyntax.fromString(scope)\n return SpacePermission.fromSyntax(syntax)\n }\n\n static fromSyntax(syntax: ScopeSyntax<'space'>): SpacePermission | null {\n const result = SpacePermission.parser.parse(syntax)\n if (!result) return null\n\n return new SpacePermission(\n result.type,\n result.authority,\n result.skey,\n result.collection,\n result.action,\n result.manage,\n )\n }\n\n static scopeNeededFor(options: SpacePermissionMatch): string {\n const base = {\n type: options.type as SpaceTypeParam,\n authority: options.authority as SpaceAuthorityParam,\n skey: options.skey as SpaceSkeyParam | '*',\n }\n // Omitting `action` would default it to the full record action list, so\n // pair the verb with `read_self` — the narrowest action expressible — to\n // avoid suggesting read/write over every record.\n if ('manage' in options && options.manage !== undefined) {\n return SpacePermission.parser.format({\n ...base,\n collection: [] as unknown as NeRoArray<SpaceCollectionParam>,\n action: ['read_self'],\n manage: [options.manage],\n })\n }\n const collection = (options.action === 'read' ||\n options.action === 'read_self'\n ? []\n : [\n options.collection as SpaceCollectionParam,\n ]) as unknown as NeRoArray<SpaceCollectionParam>\n return SpacePermission.parser.format({\n ...base,\n collection,\n action: [options.action],\n manage: [] as unknown as NeRoArray<SpaceManageOp>,\n })\n }\n}\n\nfunction includedIn<T>(this: readonly T[], value: T): boolean {\n return this.includes(value)\n}\n"]}
@@ -0,0 +1,34 @@
1
+ import { ScopeMissingError } from './scope-missing-error.js';
2
+ import { type AccountPermissionMatch } from './scopes/account-permission.js';
3
+ import { type BlobPermissionMatch } from './scopes/blob-permission.js';
4
+ import { type IdentityPermissionMatch } from './scopes/identity-permission.js';
5
+ import { type RepoPermissionMatch } from './scopes/repo-permission.js';
6
+ import { type RpcPermissionMatch } from './scopes/rpc-permission.js';
7
+ import { type SpacePermissionMatch } from './scopes/space-permission.js';
8
+ export { ScopeMissingError };
9
+ export type ScopeMatchingOptionsByResource = {
10
+ account: AccountPermissionMatch;
11
+ identity: IdentityPermissionMatch;
12
+ repo: RepoPermissionMatch;
13
+ rpc: RpcPermissionMatch;
14
+ blob: BlobPermissionMatch;
15
+ space: SpacePermissionMatch;
16
+ };
17
+ /**
18
+ * Utility class to manage a set of scopes and check if they match specific
19
+ * options for a given resource.
20
+ */
21
+ export declare class ScopesSet extends Set<string> {
22
+ /**
23
+ * Check if the container has a scope that matches the given options for a
24
+ * specific resource.
25
+ */
26
+ matches<R extends keyof ScopeMatchingOptionsByResource>(resource: R, options: ScopeMatchingOptionsByResource[R]): boolean;
27
+ assert<R extends keyof ScopeMatchingOptionsByResource>(resource: R, options: ScopeMatchingOptionsByResource[R]): void;
28
+ some(fn: (scope: string) => boolean): boolean;
29
+ every(fn: (scope: string) => boolean): boolean;
30
+ filter(fn: (scope: string) => boolean): Generator<string, void, unknown>;
31
+ map<O>(fn: (scope: string) => O): Generator<O, void, unknown>;
32
+ static fromString(string?: string): ScopesSet;
33
+ }
34
+ //# sourceMappingURL=scopes-set.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scopes-set.d.ts","sourceRoot":"","sources":["../src/scopes-set.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAA;AAC5D,OAAO,EAEL,KAAK,sBAAsB,EAC5B,MAAM,gCAAgC,CAAA;AACvC,OAAO,EAEL,KAAK,mBAAmB,EACzB,MAAM,6BAA6B,CAAA;AACpC,OAAO,EAEL,KAAK,uBAAuB,EAC7B,MAAM,iCAAiC,CAAA;AACxC,OAAO,EAEL,KAAK,mBAAmB,EACzB,MAAM,6BAA6B,CAAA;AACpC,OAAO,EAEL,KAAK,kBAAkB,EACxB,MAAM,4BAA4B,CAAA;AACnC,OAAO,EAEL,KAAK,oBAAoB,EAC1B,MAAM,8BAA8B,CAAA;AAErC,OAAO,EAAE,iBAAiB,EAAE,CAAA;AAE5B,MAAM,MAAM,8BAA8B,GAAG;IAC3C,OAAO,EAAE,sBAAsB,CAAA;IAC/B,QAAQ,EAAE,uBAAuB,CAAA;IACjC,IAAI,EAAE,mBAAmB,CAAA;IACzB,GAAG,EAAE,kBAAkB,CAAA;IACvB,IAAI,EAAE,mBAAmB,CAAA;IACzB,KAAK,EAAE,oBAAoB,CAAA;CAC5B,CAAA;AAED;;;GAGG;AACH,qBAAa,SAAU,SAAQ,GAAG,CAAC,MAAM,CAAC;IACxC;;;OAGG;IACI,OAAO,CAAC,CAAC,SAAS,MAAM,8BAA8B,EAC3D,QAAQ,EAAE,CAAC,EACX,OAAO,EAAE,8BAA8B,CAAC,CAAC,CAAC,GACzC,OAAO,CAKT;IAEM,MAAM,CAAC,CAAC,SAAS,MAAM,8BAA8B,EAC1D,QAAQ,EAAE,CAAC,EACX,OAAO,EAAE,8BAA8B,CAAC,CAAC,CAAC,QAM3C;IAEM,IAAI,CAAC,EAAE,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,GAAG,OAAO,CAGnD;IAEM,KAAK,CAAC,EAAE,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,GAAG,OAAO,CAGpD;IAEO,MAAM,CAAC,EAAE,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,oCAE5C;IAEO,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,CAAC,+BAEtC;IAED,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAE5C;CACF"}
@@ -0,0 +1,102 @@
1
+ import { ScopeMissingError } from './scope-missing-error.js';
2
+ import { AccountPermission, } from './scopes/account-permission.js';
3
+ import { BlobPermission, } from './scopes/blob-permission.js';
4
+ import { IdentityPermission, } from './scopes/identity-permission.js';
5
+ import { RepoPermission, } from './scopes/repo-permission.js';
6
+ import { RpcPermission, } from './scopes/rpc-permission.js';
7
+ import { SpacePermission, } from './scopes/space-permission.js';
8
+ export { ScopeMissingError };
9
+ /**
10
+ * Utility class to manage a set of scopes and check if they match specific
11
+ * options for a given resource.
12
+ */
13
+ export class ScopesSet extends Set {
14
+ /**
15
+ * Check if the container has a scope that matches the given options for a
16
+ * specific resource.
17
+ */
18
+ matches(resource, options) {
19
+ for (const scope of this) {
20
+ if (permissionScopeMatches(scope, resource, options))
21
+ return true;
22
+ }
23
+ return false;
24
+ }
25
+ assert(resource, options) {
26
+ if (!this.matches(resource, options)) {
27
+ const scope = scopeNeededFor(resource, options);
28
+ throw new ScopeMissingError(scope);
29
+ }
30
+ }
31
+ some(fn) {
32
+ for (const scope of this)
33
+ if (fn(scope))
34
+ return true;
35
+ return false;
36
+ }
37
+ every(fn) {
38
+ for (const scope of this)
39
+ if (!fn(scope))
40
+ return false;
41
+ return true;
42
+ }
43
+ *filter(fn) {
44
+ for (const scope of this)
45
+ if (fn(scope))
46
+ yield scope;
47
+ }
48
+ *map(fn) {
49
+ for (const scope of this)
50
+ yield fn(scope);
51
+ }
52
+ static fromString(string) {
53
+ return new ScopesSet(string?.split(' '));
54
+ }
55
+ }
56
+ function scopeNeededFor(resource, options) {
57
+ switch (resource) {
58
+ case 'account':
59
+ return AccountPermission.scopeNeededFor(options);
60
+ case 'identity':
61
+ return IdentityPermission.scopeNeededFor(options);
62
+ case 'repo':
63
+ return RepoPermission.scopeNeededFor(options);
64
+ case 'rpc':
65
+ return RpcPermission.scopeNeededFor(options);
66
+ case 'blob':
67
+ return BlobPermission.scopeNeededFor(options);
68
+ case 'space':
69
+ return SpacePermission.scopeNeededFor(options);
70
+ }
71
+ // @ts-expect-error
72
+ throw new TypeError(`Unknown resource: ${resource}`);
73
+ }
74
+ function permissionScopeMatches(scope, resource, options) {
75
+ // @NOTE we might want to cache the parsed scopes though, in practice, a
76
+ // single scope is unlikely to be parsed multiple times during a single
77
+ // request.
78
+ const permission = parsePermissionScope(resource, scope);
79
+ if (!permission)
80
+ return false;
81
+ // @ts-expect-error
82
+ return permission.matches(options);
83
+ }
84
+ function parsePermissionScope(resource, scope) {
85
+ switch (resource) {
86
+ case 'account':
87
+ return AccountPermission.fromString(scope);
88
+ case 'identity':
89
+ return IdentityPermission.fromString(scope);
90
+ case 'repo':
91
+ return RepoPermission.fromString(scope);
92
+ case 'rpc':
93
+ return RpcPermission.fromString(scope);
94
+ case 'blob':
95
+ return BlobPermission.fromString(scope);
96
+ case 'space':
97
+ return SpacePermission.fromString(scope);
98
+ default:
99
+ return null;
100
+ }
101
+ }
102
+ //# sourceMappingURL=scopes-set.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scopes-set.js","sourceRoot":"","sources":["../src/scopes-set.ts"],"names":[],"mappings":"AAAA,OAAO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KAAK,CAAA;IAE7B,mBAAmB;IACnB,OAAO,UAAU,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;AACpC,CAAC;AAED,SAAS,oBAAoB,CAAC,QAAgB,EAAE,KAAa;IAC3D,QAAQ,QAAQ,EAAE,CAAC;QACjB,KAAK,SAAS;YACZ,OAAO,iBAAiB,CAAC,UAAU,CAAC,KAAK,CAAC,CAAA;QAC5C,KAAK,UAAU;YACb,OAAO,kBAAkB,CAAC,UAAU,CAAC,KAAK,CAAC,CAAA;QAC7C,KAAK,MAAM;YACT,OAAO,cAAc,CAAC,UAAU,CAAC,KAAK,CAAC,CAAA;QACzC,KAAK,KAAK;YACR,OAAO,aAAa,CAAC,UAAU,CAAC,KAAK,CAAC,CAAA;QACxC,KAAK,MAAM;YACT,OAAO,cAAc,CAAC,UAAU,CAAC,KAAK,CAAC,CAAA;QACzC,KAAK,OAAO;YACV,OAAO,eAAe,CAAC,UAAU,CAAC,KAAK,CAAC,CAAA;QAC1C;YACE,OAAO,IAAI,CAAA;IACf,CAAC;AACH,CAAC","sourcesContent":["import { ScopeMissingError } from './scope-missing-error.js'\nimport {\n AccountPermission,\n type AccountPermissionMatch,\n} from './scopes/account-permission.js'\nimport {\n BlobPermission,\n type BlobPermissionMatch,\n} from './scopes/blob-permission.js'\nimport {\n IdentityPermission,\n type IdentityPermissionMatch,\n} from './scopes/identity-permission.js'\nimport {\n RepoPermission,\n type RepoPermissionMatch,\n} from './scopes/repo-permission.js'\nimport {\n RpcPermission,\n type RpcPermissionMatch,\n} from './scopes/rpc-permission.js'\nimport {\n SpacePermission,\n type SpacePermissionMatch,\n} from './scopes/space-permission.js'\n\nexport { ScopeMissingError }\n\nexport type ScopeMatchingOptionsByResource = {\n account: AccountPermissionMatch\n identity: IdentityPermissionMatch\n repo: RepoPermissionMatch\n rpc: RpcPermissionMatch\n blob: BlobPermissionMatch\n space: SpacePermissionMatch\n}\n\n/**\n * Utility class to manage a set of scopes and check if they match specific\n * options for a given resource.\n */\nexport class ScopesSet extends Set<string> {\n /**\n * Check if the container has a scope that matches the given options for a\n * specific resource.\n */\n public matches<R extends keyof ScopeMatchingOptionsByResource>(\n resource: R,\n options: ScopeMatchingOptionsByResource[R],\n ): boolean {\n for (const scope of this) {\n if (permissionScopeMatches(scope, resource, options)) return true\n }\n return false\n }\n\n public assert<R extends keyof ScopeMatchingOptionsByResource>(\n resource: R,\n options: ScopeMatchingOptionsByResource[R],\n ) {\n if (!this.matches(resource, options)) {\n const scope = scopeNeededFor(resource, options)\n throw new ScopeMissingError(scope)\n }\n }\n\n public some(fn: (scope: string) => boolean): boolean {\n for (const scope of this) if (fn(scope)) return true\n return false\n }\n\n public every(fn: (scope: string) => boolean): boolean {\n for (const scope of this) if (!fn(scope)) return false\n return true\n }\n\n public *filter(fn: (scope: string) => boolean) {\n for (const scope of this) if (fn(scope)) yield scope\n }\n\n public *map<O>(fn: (scope: string) => O) {\n for (const scope of this) yield fn(scope)\n }\n\n static fromString(string?: string): ScopesSet {\n return new ScopesSet(string?.split(' '))\n }\n}\n\nfunction scopeNeededFor<R extends keyof ScopeMatchingOptionsByResource>(\n resource: R,\n options: ScopeMatchingOptionsByResource[R],\n): string {\n switch (resource) {\n case 'account':\n return AccountPermission.scopeNeededFor(options as AccountPermissionMatch)\n case 'identity':\n return IdentityPermission.scopeNeededFor(\n options as IdentityPermissionMatch,\n )\n case 'repo':\n return RepoPermission.scopeNeededFor(options as RepoPermissionMatch)\n case 'rpc':\n return RpcPermission.scopeNeededFor(options as RpcPermissionMatch)\n case 'blob':\n return BlobPermission.scopeNeededFor(options as BlobPermissionMatch)\n case 'space':\n return SpacePermission.scopeNeededFor(options as SpacePermissionMatch)\n }\n // @ts-expect-error\n throw new TypeError(`Unknown resource: ${resource}`)\n}\n\nfunction permissionScopeMatches<R extends keyof ScopeMatchingOptionsByResource>(\n scope: string,\n resource: R,\n options: ScopeMatchingOptionsByResource[R],\n): boolean {\n // @NOTE we might want to cache the parsed scopes though, in practice, a\n // single scope is unlikely to be parsed multiple times during a single\n // request.\n const permission = parsePermissionScope(resource, scope)\n if (!permission) return false\n\n // @ts-expect-error\n return permission.matches(options)\n}\n\nfunction parsePermissionScope(resource: string, scope: string) {\n switch (resource) {\n case 'account':\n return AccountPermission.fromString(scope)\n case 'identity':\n return IdentityPermission.fromString(scope)\n case 'repo':\n return RepoPermission.fromString(scope)\n case 'rpc':\n return RpcPermission.fromString(scope)\n case 'blob':\n return BlobPermission.fromString(scope)\n case 'space':\n return SpacePermission.fromString(scope)\n default:\n return null\n }\n}\n"]}
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@atproto/oauth-scopes",
3
+ "version": "0.0.0-spaces-alpha-20260818021221",
4
+ "license": "MIT",
5
+ "description": "A library for manipulating and validating ATproto OAuth scopes in TypeScript.",
6
+ "keywords": [
7
+ "atproto",
8
+ "oauth",
9
+ "scopes",
10
+ "permissions"
11
+ ],
12
+ "homepage": "https://atproto.com",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://github.com/bluesky-social/atproto",
16
+ "directory": "packages/oauth/auth-scopes"
17
+ },
18
+ "files": [
19
+ "./dist",
20
+ "./CHANGELOG.md"
21
+ ],
22
+ "type": "module",
23
+ "exports": {
24
+ ".": {
25
+ "types": "./dist/index.d.ts",
26
+ "default": "./dist/index.js"
27
+ }
28
+ },
29
+ "engines": {
30
+ "node": ">=22"
31
+ },
32
+ "dependencies": {
33
+ "@atproto/did": "^0.5.4",
34
+ "@atproto/syntax": "^0.0.0-spaces-alpha-20260818021221"
35
+ },
36
+ "devDependencies": {
37
+ "vitest": "^4.1.10"
38
+ },
39
+ "scripts": {
40
+ "build": "tsc --build tsconfig.build.json",
41
+ "test": "vitest run"
42
+ }
43
+ }