@yaks/member 0.0.0-stage → 0.2.36
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.
- package/LICENSE +202 -0
- package/README.md +471 -2
- package/_dist/asking.d.ts +4 -0
- package/_dist/asking.d.ts.map +1 -0
- package/_dist/comp.d.ts +14 -0
- package/_dist/comp.d.ts.map +1 -0
- package/_dist/deny.d.ts +19 -0
- package/_dist/deny.d.ts.map +1 -0
- package/_dist/graph.d.ts +4 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/guard.d.ts +45 -0
- package/_dist/guard.d.ts.map +1 -0
- package/_dist/keywords.d.ts +80 -0
- package/_dist/keywords.d.ts.map +1 -0
- package/_dist/mod.d.ts +112 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/owns.d.ts +18 -0
- package/_dist/owns.d.ts.map +1 -0
- package/_dist/pace.d.ts +48 -0
- package/_dist/pace.d.ts.map +1 -0
- package/_dist/personal.d.ts +46 -0
- package/_dist/personal.d.ts.map +1 -0
- package/_dist/plugin.d.ts +32 -0
- package/_dist/plugin.d.ts.map +1 -0
- package/_dist/policy.d.ts +35 -0
- package/_dist/policy.d.ts.map +1 -0
- package/_dist/privacy.d.ts +55 -0
- package/_dist/privacy.d.ts.map +1 -0
- package/_dist/roles.d.ts +20 -0
- package/_dist/roles.d.ts.map +1 -0
- package/_dist/vocab.d.ts +10 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/_dist/words.d.ts +53 -0
- package/_dist/words.d.ts.map +1 -0
- package/asking.js +65 -0
- package/asking.js.map +1 -0
- package/asking.ts +77 -0
- package/comp.js +43 -0
- package/comp.js.map +1 -0
- package/comp.ts +52 -0
- package/deno.json +1 -0
- package/deny.js +31 -0
- package/deny.js.map +1 -0
- package/deny.ts +45 -0
- package/graph.js +6 -0
- package/graph.js.map +1 -0
- package/graph.ts +16 -0
- package/guard.js +265 -0
- package/guard.js.map +1 -0
- package/guard.ts +327 -0
- package/keywords.js +170 -0
- package/keywords.js.map +1 -0
- package/keywords.ts +190 -0
- package/meta/member.vocab.json +70 -0
- package/mod.js +112 -0
- package/mod.js.map +1 -0
- package/mod.ts +113 -0
- package/owns.js +12 -0
- package/owns.js.map +1 -0
- package/owns.ts +23 -0
- package/pace.js +189 -0
- package/pace.js.map +1 -0
- package/pace.ts +195 -0
- package/package.json +33 -4
- package/personal.js +448 -0
- package/personal.js.map +1 -0
- package/personal.ts +550 -0
- package/plugin.js +63 -0
- package/plugin.js.map +1 -0
- package/plugin.ts +62 -0
- package/policy.js +115 -0
- package/policy.js.map +1 -0
- package/policy.ts +176 -0
- package/privacy.js +293 -0
- package/privacy.js.map +1 -0
- package/privacy.ts +379 -0
- package/roles.js +164 -0
- package/roles.js.map +1 -0
- package/roles.ts +245 -0
- package/vocab.js +19 -0
- package/vocab.js.map +1 -0
- package/vocab.json +272 -0
- package/vocab.ts +20 -0
- package/words.js +75 -0
- package/words.js.map +1 -0
- package/words.ts +123 -0
package/keywords.ts
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
// The registration: the custom JSON Schema keyword @yaks/member adds to a
|
|
2
|
+
// component vocabulary, declared in JSON Schema's own extension form, and the
|
|
3
|
+
// reading of it. `meta/member.vocab.json` is the authored source; this module
|
|
4
|
+
// gives it a name and the shape `loadVocab(docs, [memberKeywords])` expects.
|
|
5
|
+
//
|
|
6
|
+
// `floor` is the least a component asks of whoever writes it. The
|
|
7
|
+
// guard's `floors` option says the same thing from outside, for a program
|
|
8
|
+
// holding words it did not declare; `floor` is a vocabulary saying it about its
|
|
9
|
+
// own, so an app that declares a word also declares who may write it.
|
|
10
|
+
// `readers` is the same ladder for whoever reads it: an entity wearing the
|
|
11
|
+
// component is seen only by readers standing on that floor, whatever the mode;
|
|
12
|
+
// an object of floors to queries asks a floor only of the entities it matches.
|
|
13
|
+
// `shown` asks a floor of whoever reads the component alone: a reader below it
|
|
14
|
+
// reads the entity without the component, as `personal: "entity"` keeps one
|
|
15
|
+
// aspect of a person's entity.
|
|
16
|
+
// `permit` lets the owner of a stored reference answer with a companion mark,
|
|
17
|
+
// without gaining the declaring row or permission to change its request.
|
|
18
|
+
// `personal` names the owner of a private entity or private person component.
|
|
19
|
+
|
|
20
|
+
import type { Keywords, Vocab, VocabDoc } from 'jsr:@yaks/vocab@0.2.36'
|
|
21
|
+
import { marked } from '@yaks/graph/provenance'
|
|
22
|
+
import doc from './meta/member.vocab.json' with { type: 'json' }
|
|
23
|
+
import { type Asked, askedErrors, askedOf, asking } from './asking.js'
|
|
24
|
+
import { floor, type Floors, isFloor } from './words.js'
|
|
25
|
+
|
|
26
|
+
/** The URI a vocabulary file declares under `$vocabulary` to use member keywords. */
|
|
27
|
+
export let MEMBER_URI = 'https://yak.sh/vocab/member'
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The member keyword vocabulary, ready to register:
|
|
31
|
+
* `loadVocab(docs, [memberKeywords])` carries each component's declaration onto
|
|
32
|
+
* `v.comp(name).keywords.floor`, which is where {@link floorsIn} reads it.
|
|
33
|
+
*/
|
|
34
|
+
export let memberKeywords: Keywords = {
|
|
35
|
+
uri: MEMBER_URI,
|
|
36
|
+
comp: ['floor', 'readers', 'shown', 'permit', 'personal'],
|
|
37
|
+
doc,
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Component → what one keyword of a loaded vocabulary declares of it.
|
|
41
|
+
let said = <T>(v: Vocab, keyword: string, read: (said: unknown) => T) =>
|
|
42
|
+
Object.fromEntries(
|
|
43
|
+
v.all.flatMap((c) => {
|
|
44
|
+
let said = v.comp(c)?.keywords[keyword]
|
|
45
|
+
return said === undefined ? [] : [[c, read(said)]]
|
|
46
|
+
}),
|
|
47
|
+
) as Record<string, T>
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The floors a loaded vocabulary declares, component → floor.
|
|
51
|
+
*
|
|
52
|
+
* ```ts
|
|
53
|
+
* import { assertEquals } from '@std/assert'
|
|
54
|
+
* import { loadVocab } from '@yaks/vocab'
|
|
55
|
+
* import { floorsIn, memberKeywords } from '@yaks/member'
|
|
56
|
+
* let v = loadVocab({
|
|
57
|
+
* $defs: {
|
|
58
|
+
* line: { component: true, type: 'object', floor: 'person' },
|
|
59
|
+
* note: { component: true, type: 'object' },
|
|
60
|
+
* },
|
|
61
|
+
* }, [memberKeywords])
|
|
62
|
+
* assertEquals(floorsIn(v), { line: 'person' })
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export let floorsIn = (v: Vocab): Floors => said(v, 'floor', floor)
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* What a loaded vocabulary asks of readers, component → a floor, or the
|
|
69
|
+
* queries that choose one (./asking.ts).
|
|
70
|
+
*
|
|
71
|
+
* ```ts
|
|
72
|
+
* import { assertEquals } from '@std/assert'
|
|
73
|
+
* import { loadVocab } from '@yaks/vocab'
|
|
74
|
+
* import { memberKeywords, readersIn } from '@yaks/member'
|
|
75
|
+
* let v = loadVocab({
|
|
76
|
+
* $defs: {
|
|
77
|
+
* signup: { component: true, type: 'object', readers: 'viewer' },
|
|
78
|
+
* note: { component: true, type: 'object' },
|
|
79
|
+
* },
|
|
80
|
+
* }, [memberKeywords])
|
|
81
|
+
* assertEquals(readersIn(v), { signup: 'viewer' })
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export let readersIn = (v: Vocab): Record<string, Asked> =>
|
|
85
|
+
said(v, 'readers', askedOf)
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The floor each component a loaded vocabulary declares `shown` asks of
|
|
89
|
+
* whoever reads it, component → floor.
|
|
90
|
+
*
|
|
91
|
+
* ```ts
|
|
92
|
+
* import { assertEquals } from '@std/assert'
|
|
93
|
+
* import { loadVocab } from '@yaks/vocab'
|
|
94
|
+
* import { memberKeywords, shownIn } from '@yaks/member'
|
|
95
|
+
* let v = loadVocab({
|
|
96
|
+
* $defs: {
|
|
97
|
+
* interests: { component: true, type: 'object', shown: 'editor' },
|
|
98
|
+
* note: { component: true, type: 'object' },
|
|
99
|
+
* },
|
|
100
|
+
* }, [memberKeywords])
|
|
101
|
+
* assertEquals(shownIn(v), { interests: 'editor' })
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
export let shownIn = (v: Vocab): Floors => said(v, 'shown', floor)
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Every `floor`, `readers` or `shown` a document declares that is not a floor, or a
|
|
108
|
+
* `readers` condition that is no query of the component, as sentences, the way
|
|
109
|
+
* @yaks/vocab's `storable` answers.
|
|
110
|
+
*
|
|
111
|
+
* ```ts
|
|
112
|
+
* import { assertEquals } from '@std/assert'
|
|
113
|
+
* import { floored } from '@yaks/member'
|
|
114
|
+
* assertEquals(floored({ $defs: { line: { floor: 'admin', readers: 'all' } } }), [
|
|
115
|
+
* 'line is floored "admin" — a floor is person, viewer, editor or owner',
|
|
116
|
+
* 'line is read by "all" — readers is person, viewer, editor or owner',
|
|
117
|
+
* ])
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
export let floored = (d: VocabDoc): string[] =>
|
|
121
|
+
Object.entries(d.$defs ?? {}).flatMap(([name, s]) => [
|
|
122
|
+
...s?.floor === undefined || isFloor(s.floor) ? [] : [
|
|
123
|
+
`${name} is floored "${s.floor}" — a floor is person, viewer, editor ` +
|
|
124
|
+
'or owner',
|
|
125
|
+
],
|
|
126
|
+
...s?.readers === undefined ? [] : askedErrors(name, s.readers),
|
|
127
|
+
...s?.shown === undefined || isFloor(s.shown) ? [] : [
|
|
128
|
+
`${name} is shown to "${s.shown}" — shown is person, viewer, editor ` +
|
|
129
|
+
'or owner',
|
|
130
|
+
],
|
|
131
|
+
])
|
|
132
|
+
|
|
133
|
+
/** The `readers` conditions that name a property the loaded vocabulary does
|
|
134
|
+
* not declare, as sentences: what {@link floored} cannot see in a document
|
|
135
|
+
* alone. */
|
|
136
|
+
export let conditioned = (v: Vocab): string[] =>
|
|
137
|
+
Object.entries(readersIn(v)).flatMap(([name, asked]) => {
|
|
138
|
+
try {
|
|
139
|
+
asking(asked, v)
|
|
140
|
+
return []
|
|
141
|
+
} catch (e) {
|
|
142
|
+
return [`${name} is read by conditions that do not apply: ${e}`]
|
|
143
|
+
}
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
/** Declaring component → companion mark → its local reference property. */
|
|
147
|
+
export type Permits = Record<string, Record<string, string>>
|
|
148
|
+
|
|
149
|
+
let named = (v: unknown): v is string =>
|
|
150
|
+
typeof v == 'string' && /^[a-z][a-z0-9_]{0,39}$/.test(v)
|
|
151
|
+
|
|
152
|
+
/** Errors in `permit` declarations, checked against the whole loaded vocabulary. */
|
|
153
|
+
export let permitted = (v: Vocab): string[] =>
|
|
154
|
+
v.all.flatMap((name) => {
|
|
155
|
+
let said = v.comp(name)?.keywords.permit
|
|
156
|
+
if (said === undefined) return []
|
|
157
|
+
if (!said || typeof said != 'object' || Array.isArray(said)) {
|
|
158
|
+
return [
|
|
159
|
+
`${name}.permit maps companion marks to local reference properties`,
|
|
160
|
+
]
|
|
161
|
+
}
|
|
162
|
+
return Object.entries(said).flatMap(([comp, prop]) => {
|
|
163
|
+
let errors: string[] = []
|
|
164
|
+
let mark = v.comp(comp)
|
|
165
|
+
if (
|
|
166
|
+
!named(comp) || !mark?.wire || !marked(v, comp) ||
|
|
167
|
+
comp == 'tombstone' ||
|
|
168
|
+
v.props(comp).some((p) => !v.prop(comp, p)?.stamped)
|
|
169
|
+
) {
|
|
170
|
+
errors.push(`${name}.permit ${comp} is no stamp-only companion mark`)
|
|
171
|
+
}
|
|
172
|
+
let ref = named(prop) ? v.prop(name, prop) : undefined
|
|
173
|
+
if (!ref?.ref || ref.computed || !v.comp(ref.ref)) {
|
|
174
|
+
errors.push(
|
|
175
|
+
`${name}.permit ${String(prop)} is no stored reference property`,
|
|
176
|
+
)
|
|
177
|
+
}
|
|
178
|
+
return errors
|
|
179
|
+
})
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
/** Read valid companion permissions; a malformed declaration never grants writes. */
|
|
183
|
+
export let permitsIn = (v: Vocab): Permits => {
|
|
184
|
+
let errors = permitted(v)
|
|
185
|
+
if (errors.length) throw new Error(errors.join('; '))
|
|
186
|
+
return Object.fromEntries(v.all.flatMap((name) => {
|
|
187
|
+
let said = v.comp(name)?.keywords.permit
|
|
188
|
+
return said === undefined ? [] : [[name, said as Record<string, string>]]
|
|
189
|
+
}))
|
|
190
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"$id": "https://yak.sh/vocab/member",
|
|
4
|
+
"title": "yaks member keyword vocabulary",
|
|
5
|
+
"description": "The permission keywords @yaks/member adds to a component vocabulary: floor asks a level of whoever writes it; readers asks one of whoever reads it; shown asks one of whoever reads the component alone; permit grants a companion mark to the owner of a referenced row; personal names the owner of private data.",
|
|
6
|
+
"$comment": "A vocabulary declaration in the JSON Schema sense: a URI naming this keyword set, plus the schema of each keyword. A vocabulary file declares it under $vocabulary; a loader registers it with loadVocab(docs, [memberKeywords]).",
|
|
7
|
+
"$defs": {
|
|
8
|
+
"floor": {
|
|
9
|
+
"description": "The least a writer of this component holds. \"person\" is anyone signed in; \"viewer\", \"editor\" and \"owner\" are that level on the app, or more. Absent, the app's access mode decides alone.",
|
|
10
|
+
"enum": [
|
|
11
|
+
"person",
|
|
12
|
+
"viewer",
|
|
13
|
+
"editor",
|
|
14
|
+
"owner"
|
|
15
|
+
]
|
|
16
|
+
},
|
|
17
|
+
"readers": {
|
|
18
|
+
"description": "The least a reader of this component holds, whatever the app's mode: an entity wearing it is seen only by such readers. \"person\" is anyone signed in; \"viewer\", \"editor\" and \"owner\" are that level on the app, or more. Absent, the app's access mode decides alone. An object maps floors to queries on the component's own properties instead: an entity asks the highest floor whose query its bundle matches, and nothing when it matches none.",
|
|
19
|
+
"oneOf": [
|
|
20
|
+
{
|
|
21
|
+
"enum": [
|
|
22
|
+
"person",
|
|
23
|
+
"viewer",
|
|
24
|
+
"editor",
|
|
25
|
+
"owner"
|
|
26
|
+
]
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
"type": "object",
|
|
30
|
+
"propertyNames": {
|
|
31
|
+
"enum": [
|
|
32
|
+
"person",
|
|
33
|
+
"viewer",
|
|
34
|
+
"editor",
|
|
35
|
+
"owner"
|
|
36
|
+
]
|
|
37
|
+
},
|
|
38
|
+
"additionalProperties": {
|
|
39
|
+
"type": "string"
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
]
|
|
43
|
+
},
|
|
44
|
+
"shown": {
|
|
45
|
+
"description": "The least a reader of this component itself holds: a reader below it reads the entity without this component, while its other components stay as the app's access and their own declarations say. \"person\" is anyone signed in; \"viewer\", \"editor\" and \"owner\" are that level on the app, or more. Absent, whoever reads the entity reads it.",
|
|
46
|
+
"enum": [
|
|
47
|
+
"person",
|
|
48
|
+
"viewer",
|
|
49
|
+
"editor",
|
|
50
|
+
"owner"
|
|
51
|
+
]
|
|
52
|
+
},
|
|
53
|
+
"permit": {
|
|
54
|
+
"description": "Companion mark to local reference property. The owner of the stored reference's target may add or remove the mark on this existing row; other changes retain ordinary permissions.",
|
|
55
|
+
"type": "object",
|
|
56
|
+
"propertyNames": {
|
|
57
|
+
"pattern": "^[a-z][a-z0-9_]{0,39}$"
|
|
58
|
+
},
|
|
59
|
+
"additionalProperties": {
|
|
60
|
+
"type": "string",
|
|
61
|
+
"pattern": "^[a-z][a-z0-9_]{0,39}$"
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
"personal": {
|
|
65
|
+
"description": "Person-owned data: entity makes this component private on the authenticated person entity; a local reference property names the immutable owner of an entirely private entity.",
|
|
66
|
+
"type": "string",
|
|
67
|
+
"pattern": "^[a-z][a-z0-9_]{0,39}$"
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
package/mod.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @yaks/member — access control for a
|
|
3
|
+
* {@link https://jsr.io/@yaks/graph | @yaks/graph}: who belongs to a space, and
|
|
4
|
+
* what each of them may do.
|
|
5
|
+
*
|
|
6
|
+
* A **principal** is the entity acting — a person, or the grant behind a share
|
|
7
|
+
* link. An **app** is any entity access is decided about; this package never
|
|
8
|
+
* declares what an app is. Three components answer three questions:
|
|
9
|
+
*
|
|
10
|
+
* - **Who belongs?** A `member{space, person, role}` row puts one person on a
|
|
11
|
+
* space's roster. `owner` runs the space; `member` belongs to it.
|
|
12
|
+
* - **What may they do?** A `grant{app, person, access}` gives one principal
|
|
13
|
+
* one level — `owner`, `editor` or `viewer` — on one app.
|
|
14
|
+
* - **And everyone else?** An `access{mode}` on the app declares `public`
|
|
15
|
+
* (anyone with the link reads it), `open` (anyone with the link reads AND
|
|
16
|
+
* writes it, signed in or not), or `private` (only principals holding a
|
|
17
|
+
* permission see it at all).
|
|
18
|
+
*
|
|
19
|
+
* ## Membership is not permission
|
|
20
|
+
* A row on the roster gives nothing by itself. A member with no grant reaches
|
|
21
|
+
* an app exactly as far as a stranger with the link does — which is what makes
|
|
22
|
+
* a roster safe to be generous with. The one shortcut is the space's **owner**,
|
|
23
|
+
* who holds `owner` on every app in it, never stored per app. Removing someone
|
|
24
|
+
* is then one row: delete the membership and every permission implied by it
|
|
25
|
+
* goes too.
|
|
26
|
+
*
|
|
27
|
+
* ## The three checks
|
|
28
|
+
* ```text
|
|
29
|
+
* read the mode is not `private`, or the principal holds any level
|
|
30
|
+
* write the mode is `open`, or the principal holds owner or editor
|
|
31
|
+
* call out the credential is open to anyone, or the principal holds any level
|
|
32
|
+
* ```
|
|
33
|
+
* A `viewer` never writes, under any mode. A `member` who was never granted
|
|
34
|
+
* anything holds no level at all.
|
|
35
|
+
*
|
|
36
|
+
* ## Where each check runs
|
|
37
|
+
* A write is refused inside `apply()`: {@link members} registers a
|
|
38
|
+
* `precondition` hook, which runs inside the transaction before any row has
|
|
39
|
+
* moved, so a refused batch rolls back whole ({@link Denied}). A read
|
|
40
|
+
* never reaches `apply()`, so the HTTP layer checks first —
|
|
41
|
+
* {@link policy}`(storage).canRead(who, app)`.
|
|
42
|
+
*
|
|
43
|
+
* ```ts
|
|
44
|
+
* import { loadVocab } from '@yaks/vocab'
|
|
45
|
+
* import { graph, mint } from '@yaks/graph'
|
|
46
|
+
* import { ram } from '@yaks/ram'
|
|
47
|
+
* import { memberDoc, members, policy } from '@yaks/member'
|
|
48
|
+
*
|
|
49
|
+
* let vocab = loadVocab([memberDoc])
|
|
50
|
+
* let storage = ram(vocab)
|
|
51
|
+
* let g = graph({ storage, vocab })
|
|
52
|
+
* let club = mint()
|
|
53
|
+
* let dana = mint()
|
|
54
|
+
* let list = mint()
|
|
55
|
+
* await g.apply([
|
|
56
|
+
* { entity: { eid: list }, access: { mode: 'private' } },
|
|
57
|
+
* {
|
|
58
|
+
* entity: { eid: 'm1' },
|
|
59
|
+
* member: { space: club, person: dana, role: 'owner' },
|
|
60
|
+
* },
|
|
61
|
+
* ])
|
|
62
|
+
* g.use(members({ app: list, space: club, vocab }))
|
|
63
|
+
*
|
|
64
|
+
* let may = policy(storage, { space: club })
|
|
65
|
+
* await may.canRead(dana, list) // true
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* ## Only an owner edits the access rows
|
|
69
|
+
* Writing a `member`, a `grant` or an `access` requires `owner` — an editor
|
|
70
|
+
* writes the app's data and does not hand out permissions. That matters most on
|
|
71
|
+
* an `open` app, where anyone may write: without the rule, a visitor invited to
|
|
72
|
+
* sign the guest book could rewrite the roster. The first owner is therefore
|
|
73
|
+
* written before the guard is installed; see {@link members}.
|
|
74
|
+
*
|
|
75
|
+
* ## A component says who writes it, and how often
|
|
76
|
+
* A component may declare a `floor`, the least its writer holds whatever the
|
|
77
|
+
* mode — `person` (anyone signed in), `viewer`, `editor` or `owner` — and a
|
|
78
|
+
* `pace`, how often each writer may write it. The guard refuses the rest:
|
|
79
|
+
* {@link Denied} naming the component, or {@link Paced} saying when to try
|
|
80
|
+
* again. Load the vocabulary with {@link memberKeywords} so `floor` is kept.
|
|
81
|
+
*
|
|
82
|
+
* ## Share links
|
|
83
|
+
* A grant may name a `token` instead of a person. Whoever opens that link acts
|
|
84
|
+
* as the grant, so the HTTP layer signs their bundles with the grant's own
|
|
85
|
+
* entity id and everything above works unchanged — no account, no roster row,
|
|
86
|
+
* one revocable row.
|
|
87
|
+
*
|
|
88
|
+
* ## What is deliberately not here
|
|
89
|
+
* Authentication: establishing who someone is belongs to the HTTP layer.
|
|
90
|
+
* Invitations: a new `member` row usually means messaging somebody, which is a
|
|
91
|
+
* `created('member')` handler on
|
|
92
|
+
* {@link https://jsr.io/@yaks/effects | @yaks/effects} — the hook this package
|
|
93
|
+
* leaves for `@yaks/mail` to register. And per-grant filters (a grant good for
|
|
94
|
+
* only part of an app's data) — one level per app, on purpose.
|
|
95
|
+
*
|
|
96
|
+
* It imports no platform API, so the same rules run on a server, in a worker,
|
|
97
|
+
* and in a browser tab.
|
|
98
|
+
*
|
|
99
|
+
* @module
|
|
100
|
+
*/ export * from "./words.js";
|
|
101
|
+
export * from "./comp.js";
|
|
102
|
+
export * from "./keywords.js";
|
|
103
|
+
export * from "./policy.js";
|
|
104
|
+
export * from "./deny.js";
|
|
105
|
+
export * from "./pace.js";
|
|
106
|
+
export * from "./guard.js";
|
|
107
|
+
export * from "./plugin.js";
|
|
108
|
+
export * from "./personal.js";
|
|
109
|
+
export * from "./privacy.js";
|
|
110
|
+
export * from "./owns.js";
|
|
111
|
+
export * from "./roles.js";
|
|
112
|
+
//# sourceMappingURL=mod.js.map
|
package/mod.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.js","sources":["./mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAmGC,GAED,2BAA0B;AAC1B,0BAAyB;AACzB,8BAA6B;AAC7B,4BAA2B;AAC3B,0BAAyB;AACzB,0BAAyB;AACzB,2BAA0B;AAC1B,4BAA2B;AAC3B,8BAA6B;AAC7B,6BAA4B;AAC5B,0BAAyB;AACzB,2BAA0B"}
|
package/mod.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @yaks/member — access control for a
|
|
3
|
+
* {@link https://jsr.io/@yaks/graph | @yaks/graph}: who belongs to a space, and
|
|
4
|
+
* what each of them may do.
|
|
5
|
+
*
|
|
6
|
+
* A **principal** is the entity acting — a person, or the grant behind a share
|
|
7
|
+
* link. An **app** is any entity access is decided about; this package never
|
|
8
|
+
* declares what an app is. Three components answer three questions:
|
|
9
|
+
*
|
|
10
|
+
* - **Who belongs?** A `member{space, person, role}` row puts one person on a
|
|
11
|
+
* space's roster. `owner` runs the space; `member` belongs to it.
|
|
12
|
+
* - **What may they do?** A `grant{app, person, access}` gives one principal
|
|
13
|
+
* one level — `owner`, `editor` or `viewer` — on one app.
|
|
14
|
+
* - **And everyone else?** An `access{mode}` on the app declares `public`
|
|
15
|
+
* (anyone with the link reads it), `open` (anyone with the link reads AND
|
|
16
|
+
* writes it, signed in or not), or `private` (only principals holding a
|
|
17
|
+
* permission see it at all).
|
|
18
|
+
*
|
|
19
|
+
* ## Membership is not permission
|
|
20
|
+
* A row on the roster gives nothing by itself. A member with no grant reaches
|
|
21
|
+
* an app exactly as far as a stranger with the link does — which is what makes
|
|
22
|
+
* a roster safe to be generous with. The one shortcut is the space's **owner**,
|
|
23
|
+
* who holds `owner` on every app in it, never stored per app. Removing someone
|
|
24
|
+
* is then one row: delete the membership and every permission implied by it
|
|
25
|
+
* goes too.
|
|
26
|
+
*
|
|
27
|
+
* ## The three checks
|
|
28
|
+
* ```text
|
|
29
|
+
* read the mode is not `private`, or the principal holds any level
|
|
30
|
+
* write the mode is `open`, or the principal holds owner or editor
|
|
31
|
+
* call out the credential is open to anyone, or the principal holds any level
|
|
32
|
+
* ```
|
|
33
|
+
* A `viewer` never writes, under any mode. A `member` who was never granted
|
|
34
|
+
* anything holds no level at all.
|
|
35
|
+
*
|
|
36
|
+
* ## Where each check runs
|
|
37
|
+
* A write is refused inside `apply()`: {@link members} registers a
|
|
38
|
+
* `precondition` hook, which runs inside the transaction before any row has
|
|
39
|
+
* moved, so a refused batch rolls back whole ({@link Denied}). A read
|
|
40
|
+
* never reaches `apply()`, so the HTTP layer checks first —
|
|
41
|
+
* {@link policy}`(storage).canRead(who, app)`.
|
|
42
|
+
*
|
|
43
|
+
* ```ts
|
|
44
|
+
* import { loadVocab } from '@yaks/vocab'
|
|
45
|
+
* import { graph, mint } from '@yaks/graph'
|
|
46
|
+
* import { ram } from '@yaks/ram'
|
|
47
|
+
* import { memberDoc, members, policy } from '@yaks/member'
|
|
48
|
+
*
|
|
49
|
+
* let vocab = loadVocab([memberDoc])
|
|
50
|
+
* let storage = ram(vocab)
|
|
51
|
+
* let g = graph({ storage, vocab })
|
|
52
|
+
* let club = mint()
|
|
53
|
+
* let dana = mint()
|
|
54
|
+
* let list = mint()
|
|
55
|
+
* await g.apply([
|
|
56
|
+
* { entity: { eid: list }, access: { mode: 'private' } },
|
|
57
|
+
* {
|
|
58
|
+
* entity: { eid: 'm1' },
|
|
59
|
+
* member: { space: club, person: dana, role: 'owner' },
|
|
60
|
+
* },
|
|
61
|
+
* ])
|
|
62
|
+
* g.use(members({ app: list, space: club, vocab }))
|
|
63
|
+
*
|
|
64
|
+
* let may = policy(storage, { space: club })
|
|
65
|
+
* await may.canRead(dana, list) // true
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* ## Only an owner edits the access rows
|
|
69
|
+
* Writing a `member`, a `grant` or an `access` requires `owner` — an editor
|
|
70
|
+
* writes the app's data and does not hand out permissions. That matters most on
|
|
71
|
+
* an `open` app, where anyone may write: without the rule, a visitor invited to
|
|
72
|
+
* sign the guest book could rewrite the roster. The first owner is therefore
|
|
73
|
+
* written before the guard is installed; see {@link members}.
|
|
74
|
+
*
|
|
75
|
+
* ## A component says who writes it, and how often
|
|
76
|
+
* A component may declare a `floor`, the least its writer holds whatever the
|
|
77
|
+
* mode — `person` (anyone signed in), `viewer`, `editor` or `owner` — and a
|
|
78
|
+
* `pace`, how often each writer may write it. The guard refuses the rest:
|
|
79
|
+
* {@link Denied} naming the component, or {@link Paced} saying when to try
|
|
80
|
+
* again. Load the vocabulary with {@link memberKeywords} so `floor` is kept.
|
|
81
|
+
*
|
|
82
|
+
* ## Share links
|
|
83
|
+
* A grant may name a `token` instead of a person. Whoever opens that link acts
|
|
84
|
+
* as the grant, so the HTTP layer signs their bundles with the grant's own
|
|
85
|
+
* entity id and everything above works unchanged — no account, no roster row,
|
|
86
|
+
* one revocable row.
|
|
87
|
+
*
|
|
88
|
+
* ## What is deliberately not here
|
|
89
|
+
* Authentication: establishing who someone is belongs to the HTTP layer.
|
|
90
|
+
* Invitations: a new `member` row usually means messaging somebody, which is a
|
|
91
|
+
* `created('member')` handler on
|
|
92
|
+
* {@link https://jsr.io/@yaks/effects | @yaks/effects} — the hook this package
|
|
93
|
+
* leaves for `@yaks/mail` to register. And per-grant filters (a grant good for
|
|
94
|
+
* only part of an app's data) — one level per app, on purpose.
|
|
95
|
+
*
|
|
96
|
+
* It imports no platform API, so the same rules run on a server, in a worker,
|
|
97
|
+
* and in a browser tab.
|
|
98
|
+
*
|
|
99
|
+
* @module
|
|
100
|
+
*/
|
|
101
|
+
|
|
102
|
+
export * from './words.js'
|
|
103
|
+
export * from './comp.js'
|
|
104
|
+
export * from './keywords.js'
|
|
105
|
+
export * from './policy.js'
|
|
106
|
+
export * from './deny.js'
|
|
107
|
+
export * from './pace.js'
|
|
108
|
+
export * from './guard.js'
|
|
109
|
+
export * from './plugin.js'
|
|
110
|
+
export * from './personal.js'
|
|
111
|
+
export * from './privacy.js'
|
|
112
|
+
export * from './owns.js'
|
|
113
|
+
export * from './roles.js'
|
package/owns.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** A verified person and instrument, or the ownership recorded for a row. */ /** A bylined row belongs to its person; an unbylined row belongs to its
|
|
2
|
+
* instrument, including after that browser signs in. Instruments never grant
|
|
3
|
+
* access to a row bound to another person.
|
|
4
|
+
*
|
|
5
|
+
* ```ts
|
|
6
|
+
* import { owns } from '@yaks/member'
|
|
7
|
+
* owns({ by: 'ana', via: 'browser' }, { by: 'bo', via: 'browser' }) // false
|
|
8
|
+
* owns({ via: 'browser' }, { by: 'ana', via: 'browser' }) // true
|
|
9
|
+
* owns({}, {}) // false
|
|
10
|
+
* ```
|
|
11
|
+
*/ export let owns = (owner, who)=>owner.by ? owner.by == who.by : !!owner.via && (who.by && who.browsers !== undefined ? who.browsers.includes(owner.via) : owner.via == who.via);
|
|
12
|
+
//# sourceMappingURL=owns.js.map
|
package/owns.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"owns.js","sources":["./owns.ts"],"names":[],"mappings":"AAAA,2EAA2E,GAG3E;;;;;;;;;;CAUC,GACD,OAAO,IAAI,OAAO,CAChB,OACA,MAEA,MAAM,EAAE,GACJ,MAAM,EAAE,IAAI,IAAI,EAAE,GAClB,CAAC,CAAC,MAAM,GAAG,IAAI,CAAC,IAAI,EAAE,IAAI,IAAI,QAAQ,KAAK,YACzC,IAAI,QAAQ,CAAC,QAAQ,CAAC,MAAM,GAAG,IAC/B,MAAM,GAAG,IAAI,IAAI,GAAG,EAAC"}
|
package/owns.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** A verified person and instrument, or the ownership recorded for a row. */
|
|
2
|
+
export type Owner = { by?: string | null; via?: string | null }
|
|
3
|
+
|
|
4
|
+
/** A bylined row belongs to its person; an unbylined row belongs to its
|
|
5
|
+
* instrument, including after that browser signs in. Instruments never grant
|
|
6
|
+
* access to a row bound to another person.
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { owns } from '@yaks/member'
|
|
10
|
+
* owns({ by: 'ana', via: 'browser' }, { by: 'bo', via: 'browser' }) // false
|
|
11
|
+
* owns({ via: 'browser' }, { by: 'ana', via: 'browser' }) // true
|
|
12
|
+
* owns({}, {}) // false
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export let owns = (
|
|
16
|
+
owner: Owner,
|
|
17
|
+
who: Owner & { browsers?: readonly string[] },
|
|
18
|
+
): boolean =>
|
|
19
|
+
owner.by
|
|
20
|
+
? owner.by == who.by
|
|
21
|
+
: !!owner.via && (who.by && who.browsers !== undefined
|
|
22
|
+
? who.browsers.includes(owner.via)
|
|
23
|
+
: owner.via == who.via)
|