@phis/contracts 0.2.0
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 +201 -0
- package/NOTICE +4 -0
- package/README.md +102 -0
- package/dist/access.d.ts +115 -0
- package/dist/access.js +157 -0
- package/dist/addon-index.d.ts +6 -0
- package/dist/addon-index.js +6 -0
- package/dist/addon.d.ts +259 -0
- package/dist/addon.js +15 -0
- package/dist/capabilities.d.ts +240 -0
- package/dist/capabilities.js +37 -0
- package/dist/core.d.ts +66 -0
- package/dist/core.js +70 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/media-storage.d.ts +160 -0
- package/dist/media-storage.js +9 -0
- package/dist/queries.d.ts +171 -0
- package/dist/queries.js +16 -0
- package/dist/schema.d.ts +173 -0
- package/dist/schema.js +29 -0
- package/dist/signals.d.ts +36 -0
- package/dist/signals.js +85 -0
- package/package.json +52 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Phisys Ltd
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# @phis/contracts
|
|
2
|
+
|
|
3
|
+
The agreements phi-server keeps with what surrounds it. Two of them, under two subpaths, because
|
|
4
|
+
they hold between different parties and freeze at different moments.
|
|
5
|
+
|
|
6
|
+
```
|
|
7
|
+
@phis/contracts/addon what phi-server and a separately shipped Add-on promise each other
|
|
8
|
+
@phis/contracts/access the authorization vocabulary phi-server and @phis/ui both evaluate
|
|
9
|
+
@phis/contracts/signals the signal vocabulary the UI declares against and phi-server validates
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
There is deliberately no root export. A package you can import from the top invites everything that
|
|
13
|
+
two of our packages happen to share; a subpath makes you say which agreement you mean.
|
|
14
|
+
|
|
15
|
+
## `/addon`
|
|
16
|
+
|
|
17
|
+
The types and constants a phi-server Add-on declares or implements. Types and constants only — no
|
|
18
|
+
runtime code, no `server-only`, no React, no Next.
|
|
19
|
+
|
|
20
|
+
An Add-on is compiled to a single ESM artifact and installed into a running phi-server through the
|
|
21
|
+
`phis` CLI. It is never built into the server. This subpath is the only thing an Add-on compiles
|
|
22
|
+
against; it must not depend on phi-server itself.
|
|
23
|
+
|
|
24
|
+
```sh
|
|
25
|
+
npm install --save-dev @phis/contracts
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
For an Add-on author it is a build-time dependency: everything `/addon` exports is erased at compile
|
|
29
|
+
time, apart from a handful of constants.
|
|
30
|
+
|
|
31
|
+
## `/access`
|
|
32
|
+
|
|
33
|
+
Claim shapes, policy shapes, and the evaluator that decides them. Unlike `/addon`, this one **is**
|
|
34
|
+
runtime code, and it is a real dependency of both packages that use it.
|
|
35
|
+
|
|
36
|
+
Two processes decide the same question. phi-server decides it in its guards and API routes; the site
|
|
37
|
+
decides it while rendering — per navigation entry, per tree node, and in the browser, where
|
|
38
|
+
phi-server is not reachable without a round trip. Neither can defer to the other, so both evaluate,
|
|
39
|
+
and one compiled source is the only way both can agree.
|
|
40
|
+
|
|
41
|
+
They did not agree before this subpath existed. A stored claim with `flags: -1` admitted everything
|
|
42
|
+
on the server and nothing in the UI: one side normalised the value, the other did not. Neither copy
|
|
43
|
+
looked wrong on its own, which is why it went unnoticed.
|
|
44
|
+
|
|
45
|
+
Each side keeps its own viewer type and passes a projection onto `PhiAccessSubject`, so neither
|
|
46
|
+
package has to adopt the other's shape.
|
|
47
|
+
|
|
48
|
+
## `/signals`
|
|
49
|
+
|
|
50
|
+
The closed vocabularies a widget's wiring is written in -- scopes, actions, value types -- and the shape
|
|
51
|
+
of a value schema name.
|
|
52
|
+
|
|
53
|
+
A widget declares what it emits and listens for, the Builder stores that, and phi-server validates it on
|
|
54
|
+
the way in. Two lists, one meaning; and when they were two lists they drifted. `@phis/ui` had grown
|
|
55
|
+
`date`, `time` and `length`; phi-server had not. A length control's change signal -- from a widget the
|
|
56
|
+
same release shipped -- was refused on save with "valueType is invalid".
|
|
57
|
+
|
|
58
|
+
Which schemas exist stays with the UI. phi-server checks that a JSON signal names *a* schema, never
|
|
59
|
+
which one, and `isPhiSignalValueSchemaShape` is exactly that much: shape, not membership. The UI asks
|
|
60
|
+
the stricter question, because it holds the module registry; phi-server must not, or it would refuse
|
|
61
|
+
every third party's schema for never having heard of it.
|
|
62
|
+
|
|
63
|
+
## What `/addon` covers
|
|
64
|
+
|
|
65
|
+
- **Manifest** — `PhiServerAddonManifestV1`: identity, version, required core capabilities,
|
|
66
|
+
the routes an Add-on claims, the services it provides.
|
|
67
|
+
- **Runtime** — `PhiServerAddonRuntimeV1`, `PhiServerAddonHandler`,
|
|
68
|
+
`PhiServerAddonRequestContext`: the shape the artifact's default export must have.
|
|
69
|
+
- **Schema** — `PhiServerAddonSchemaDescriptor` and its column, index, and constraint
|
|
70
|
+
descriptors. Tables are declared, not migrated by hand; phi-server applies them.
|
|
71
|
+
- **Service kinds** — the interfaces an Add-on may provide or consume, currently the media
|
|
72
|
+
storage adapter (`PhisMediaStorageAdapter`, upload plans, object I/O).
|
|
73
|
+
- **What Core offers** — `PHI_SERVER_CORE_CAPABILITIES`, the capability ids an Add-on may
|
|
74
|
+
require; `PHI_SERVER_SERVICE_KINDS`, the service kinds Core owns; and
|
|
75
|
+
`PHI_SERVER_SERVICE_INTERFACE_DIGESTS`, the digest of each kind's current interface. Declare
|
|
76
|
+
against these names rather than spelling them out: Core refuses an Add-on whose digest is not
|
|
77
|
+
the one this release offers, and a literal cannot be checked before you ship it.
|
|
78
|
+
|
|
79
|
+
This list is a promise, not a plan. A capability appears here when Core delivers it, never
|
|
80
|
+
before.
|
|
81
|
+
|
|
82
|
+
`PHI_SERVER_ADDON_ABI_VERSION` is the single number a server checks an Add-on against.
|
|
83
|
+
|
|
84
|
+
## Admission rule
|
|
85
|
+
|
|
86
|
+
Per subpath, and both halves are narrow on purpose.
|
|
87
|
+
|
|
88
|
+
Into `/addon`: only what **phi-server and a separately shipped Add-on must agree on**. Declarations,
|
|
89
|
+
never implementations.
|
|
90
|
+
|
|
91
|
+
Into `/access`: only what **phi-server and `@phis/ui` must both evaluate**, because each decides it
|
|
92
|
+
in its own process. Not what both happen to use — a date formatter used on both sides is shared
|
|
93
|
+
convenience, not a contract, and belongs to neither.
|
|
94
|
+
|
|
95
|
+
The test for either is the same: name the two parties and the sentence they are promising each
|
|
96
|
+
other. If that sentence cannot be written down, it does not go in here.
|
|
97
|
+
|
|
98
|
+
## Documentation
|
|
99
|
+
|
|
100
|
+
The full walkthrough — how an Add-on is laid out, compiled, installed, activated, and
|
|
101
|
+
upgraded — lives in phi-server's `ADDON_HOWTO.md`, with the design rationale in
|
|
102
|
+
`SERVER_ADDONS.md`.
|
package/dist/access.d.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The authorization vocabulary phi-server and the site UI share.
|
|
3
|
+
*
|
|
4
|
+
* Two processes decide the same question. phi-server decides it in its guards, its Add-on capabilities
|
|
5
|
+
* and its API routes; the site decides it while rendering -- per navigation entry, per tree node, per
|
|
6
|
+
* menu item, and in the browser, where phi-server is not reachable without a round trip. Neither can
|
|
7
|
+
* defer to the other, so both evaluate, and the only way both can agree is that both compile the same
|
|
8
|
+
* source.
|
|
9
|
+
*
|
|
10
|
+
* They did not agree before this file existed. `flags: -1` on a stored claim admitted everything on the
|
|
11
|
+
* server and nothing in the UI, because one side normalised the value and the other did not. Nothing was
|
|
12
|
+
* wrong with either copy on its own, which is exactly why it went unnoticed.
|
|
13
|
+
*
|
|
14
|
+
* What belongs here is what two ends of one agreement must both know. Not what both happen to use.
|
|
15
|
+
*/
|
|
16
|
+
export declare const PHI_CORE_ROLE_PROVIDER_ID: "@phis/server/core";
|
|
17
|
+
/**
|
|
18
|
+
* The Site roles Core itself grants, as bit flags.
|
|
19
|
+
*
|
|
20
|
+
* Flags rather than names, because these are Core's own and finite, they are stored on a membership row,
|
|
21
|
+
* and a viewer is checked against several at once. An Add-on's roles are the opposite case and are
|
|
22
|
+
* carried by name.
|
|
23
|
+
*/
|
|
24
|
+
export declare const PhiBaseRole: {
|
|
25
|
+
readonly Admin: number;
|
|
26
|
+
readonly Builder: number;
|
|
27
|
+
readonly Publisher: number;
|
|
28
|
+
readonly Author: number;
|
|
29
|
+
readonly Developer: number;
|
|
30
|
+
readonly Supporter: number;
|
|
31
|
+
readonly Accountant: number;
|
|
32
|
+
};
|
|
33
|
+
export type PhiRoleProviderId = `@${string}/${string}`;
|
|
34
|
+
export type PhiGroupProviderId = `@${string}/${string}`;
|
|
35
|
+
export type PhiViewerRoleClaim = {
|
|
36
|
+
providerId: PhiRoleProviderId;
|
|
37
|
+
flags: number;
|
|
38
|
+
};
|
|
39
|
+
export type PhiViewerGroupClaim = {
|
|
40
|
+
providerId: PhiGroupProviderId;
|
|
41
|
+
key: string;
|
|
42
|
+
flags: number;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* The roles a Server Add-on declared, held by this viewer, by name.
|
|
46
|
+
*
|
|
47
|
+
* An Add-on's roles are frozen from the first assignment and declared in its own manifest, so a bit
|
|
48
|
+
* position would have to be handed out and kept forever -- reordering the manifest would silently change
|
|
49
|
+
* what a policy means, and thirty-two would be the ceiling. Core never interprets these names.
|
|
50
|
+
*/
|
|
51
|
+
export type PhiViewerAddonRoleClaim = {
|
|
52
|
+
providerId: PhiRoleProviderId;
|
|
53
|
+
roles: readonly string[];
|
|
54
|
+
};
|
|
55
|
+
export type PhiViewerAccessPolicy = {
|
|
56
|
+
access: "anyone";
|
|
57
|
+
} | {
|
|
58
|
+
access: "anonymous";
|
|
59
|
+
} | {
|
|
60
|
+
access: "authenticated";
|
|
61
|
+
} | {
|
|
62
|
+
access: "roles";
|
|
63
|
+
providerId: PhiRoleProviderId;
|
|
64
|
+
allowedRoleFlags: number;
|
|
65
|
+
} | {
|
|
66
|
+
access: "groups";
|
|
67
|
+
providerId: PhiGroupProviderId;
|
|
68
|
+
allowedGroupKeys: readonly string[];
|
|
69
|
+
} | {
|
|
70
|
+
access: "addon-roles";
|
|
71
|
+
providerId: PhiRoleProviderId;
|
|
72
|
+
allowedRoles: readonly string[];
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* The least a viewer has to be for a policy to be decided about them.
|
|
76
|
+
*
|
|
77
|
+
* Deliberately smaller than either side's own viewer. phi-server carries `authenticated: boolean` and
|
|
78
|
+
* the UI carries an `access` that also drives rendering; making one of those shapes the shared one would
|
|
79
|
+
* have pushed a foreign concern into the other package. Each side keeps its own and passes a projection.
|
|
80
|
+
*/
|
|
81
|
+
export type PhiAccessSubject = {
|
|
82
|
+
authenticated: boolean;
|
|
83
|
+
roleClaims?: readonly PhiViewerRoleClaim[];
|
|
84
|
+
groupClaims?: readonly PhiViewerGroupClaim[];
|
|
85
|
+
/** Absent means none are known here, not that none are held: an `addon-roles` policy then denies. */
|
|
86
|
+
addonRoleClaims?: readonly PhiViewerAddonRoleClaim[];
|
|
87
|
+
};
|
|
88
|
+
export declare function getPhiViewerRoleFlags(subject: Pick<PhiAccessSubject, "roleClaims">, providerId: PhiRoleProviderId): number;
|
|
89
|
+
export declare function hasProviderRole(subject: Pick<PhiAccessSubject, "roleClaims">, providerId: PhiRoleProviderId, roleFlag: number): boolean;
|
|
90
|
+
export declare function hasPhiBaseRole(subject: Pick<PhiAccessSubject, "roleClaims">, roleFlag: number): boolean;
|
|
91
|
+
export declare function hasProviderGroup(subject: Pick<PhiAccessSubject, "groupClaims">, providerId: PhiGroupProviderId, groupKey: string): boolean;
|
|
92
|
+
export declare function hasProviderAddonRole(subject: Pick<PhiAccessSubject, "addonRoleClaims">, providerId: PhiRoleProviderId, role: string): boolean;
|
|
93
|
+
/**
|
|
94
|
+
* Whether this subject may reach something guarded by this policy.
|
|
95
|
+
*
|
|
96
|
+
* A missing policy is `anyone`: a surface that states no requirement has none. Site Admin passes
|
|
97
|
+
* everything, which is the one shortcut in here and the reason a locked-out Site can always be reopened.
|
|
98
|
+
*/
|
|
99
|
+
export declare function canPhiAccessSubjectReach(subject: PhiAccessSubject, policy: PhiViewerAccessPolicy | null | undefined): boolean;
|
|
100
|
+
/**
|
|
101
|
+
* Whether a policy names a provider its owner is allowed to name.
|
|
102
|
+
*
|
|
103
|
+
* A Module may gate on its own Add-on's roles and on Core's, and not on a stranger's -- otherwise one
|
|
104
|
+
* package could make its surface depend on another's vocabulary, which nobody would be responsible for
|
|
105
|
+
* keeping stable.
|
|
106
|
+
*/
|
|
107
|
+
export declare function isPhiAccessPolicyProviderOwned(policy: PhiViewerAccessPolicy, ownerProviderId?: PhiRoleProviderId | null): boolean;
|
|
108
|
+
/**
|
|
109
|
+
* Reads a stored descriptor back into a policy, or null.
|
|
110
|
+
*
|
|
111
|
+
* Whatever does not read as a policy is refused rather than repaired: a half-understood requirement
|
|
112
|
+
* would decide what somebody is shown, and guessing at it is how a stricter rule quietly becomes a
|
|
113
|
+
* looser one.
|
|
114
|
+
*/
|
|
115
|
+
export declare function readPhiViewerAccessPolicy(value: unknown): PhiViewerAccessPolicy | null;
|
package/dist/access.js
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The authorization vocabulary phi-server and the site UI share.
|
|
3
|
+
*
|
|
4
|
+
* Two processes decide the same question. phi-server decides it in its guards, its Add-on capabilities
|
|
5
|
+
* and its API routes; the site decides it while rendering -- per navigation entry, per tree node, per
|
|
6
|
+
* menu item, and in the browser, where phi-server is not reachable without a round trip. Neither can
|
|
7
|
+
* defer to the other, so both evaluate, and the only way both can agree is that both compile the same
|
|
8
|
+
* source.
|
|
9
|
+
*
|
|
10
|
+
* They did not agree before this file existed. `flags: -1` on a stored claim admitted everything on the
|
|
11
|
+
* server and nothing in the UI, because one side normalised the value and the other did not. Nothing was
|
|
12
|
+
* wrong with either copy on its own, which is exactly why it went unnoticed.
|
|
13
|
+
*
|
|
14
|
+
* What belongs here is what two ends of one agreement must both know. Not what both happen to use.
|
|
15
|
+
*/
|
|
16
|
+
export const PHI_CORE_ROLE_PROVIDER_ID = "@phis/server/core";
|
|
17
|
+
/**
|
|
18
|
+
* The Site roles Core itself grants, as bit flags.
|
|
19
|
+
*
|
|
20
|
+
* Flags rather than names, because these are Core's own and finite, they are stored on a membership row,
|
|
21
|
+
* and a viewer is checked against several at once. An Add-on's roles are the opposite case and are
|
|
22
|
+
* carried by name.
|
|
23
|
+
*/
|
|
24
|
+
export const PhiBaseRole = {
|
|
25
|
+
Admin: 1 << 0,
|
|
26
|
+
Builder: 1 << 1,
|
|
27
|
+
Publisher: 1 << 2,
|
|
28
|
+
Author: 1 << 3,
|
|
29
|
+
Developer: 1 << 4,
|
|
30
|
+
Supporter: 1 << 5,
|
|
31
|
+
Accountant: 1 << 6,
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Flags as they may be trusted.
|
|
35
|
+
*
|
|
36
|
+
* A stored claim is data, and data can be wrong. A negative value passes a bitwise test against every
|
|
37
|
+
* mask there is, so reading it as written turns one bad row into every permission. Anything that is not
|
|
38
|
+
* a non-negative integer is read as no flags at all.
|
|
39
|
+
*/
|
|
40
|
+
function readRoleFlags(value) {
|
|
41
|
+
return typeof value === "number" && Number.isInteger(value) && value >= 0 ? value : 0;
|
|
42
|
+
}
|
|
43
|
+
export function getPhiViewerRoleFlags(subject, providerId) {
|
|
44
|
+
return readRoleFlags(subject.roleClaims?.find((claim) => claim.providerId === providerId)?.flags);
|
|
45
|
+
}
|
|
46
|
+
export function hasProviderRole(subject, providerId, roleFlag) {
|
|
47
|
+
const wanted = readRoleFlags(roleFlag);
|
|
48
|
+
return wanted !== 0 && (getPhiViewerRoleFlags(subject, providerId) & wanted) !== 0;
|
|
49
|
+
}
|
|
50
|
+
export function hasPhiBaseRole(subject, roleFlag) {
|
|
51
|
+
return hasProviderRole(subject, PHI_CORE_ROLE_PROVIDER_ID, roleFlag);
|
|
52
|
+
}
|
|
53
|
+
export function hasProviderGroup(subject, providerId, groupKey) {
|
|
54
|
+
return subject.groupClaims?.some((claim) => claim.providerId === providerId && claim.key === groupKey) === true;
|
|
55
|
+
}
|
|
56
|
+
export function hasProviderAddonRole(subject, providerId, role) {
|
|
57
|
+
return subject.addonRoleClaims?.some((claim) => claim.providerId === providerId && claim.roles.includes(role)) === true;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Whether this subject may reach something guarded by this policy.
|
|
61
|
+
*
|
|
62
|
+
* A missing policy is `anyone`: a surface that states no requirement has none. Site Admin passes
|
|
63
|
+
* everything, which is the one shortcut in here and the reason a locked-out Site can always be reopened.
|
|
64
|
+
*/
|
|
65
|
+
export function canPhiAccessSubjectReach(subject, policy) {
|
|
66
|
+
const resolved = policy ?? { access: "anyone" };
|
|
67
|
+
if (resolved.access === "anyone") {
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
if (resolved.access === "anonymous") {
|
|
71
|
+
return !subject.authenticated;
|
|
72
|
+
}
|
|
73
|
+
if (!subject.authenticated) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
if (resolved.access === "authenticated") {
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
if (hasPhiBaseRole(subject, PhiBaseRole.Admin)) {
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
if (resolved.access === "groups") {
|
|
83
|
+
return resolved.allowedGroupKeys.some((key) => hasProviderGroup(subject, resolved.providerId, key));
|
|
84
|
+
}
|
|
85
|
+
if (resolved.access === "addon-roles") {
|
|
86
|
+
return resolved.allowedRoles.some((role) => hasProviderAddonRole(subject, resolved.providerId, role));
|
|
87
|
+
}
|
|
88
|
+
return resolved.allowedRoleFlags > 0 &&
|
|
89
|
+
(getPhiViewerRoleFlags(subject, resolved.providerId) & resolved.allowedRoleFlags) !== 0;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Whether a policy names a provider its owner is allowed to name.
|
|
93
|
+
*
|
|
94
|
+
* A Module may gate on its own Add-on's roles and on Core's, and not on a stranger's -- otherwise one
|
|
95
|
+
* package could make its surface depend on another's vocabulary, which nobody would be responsible for
|
|
96
|
+
* keeping stable.
|
|
97
|
+
*/
|
|
98
|
+
export function isPhiAccessPolicyProviderOwned(policy, ownerProviderId) {
|
|
99
|
+
return (policy.access !== "roles" &&
|
|
100
|
+
policy.access !== "groups" &&
|
|
101
|
+
policy.access !== "addon-roles") ||
|
|
102
|
+
policy.providerId === PHI_CORE_ROLE_PROVIDER_ID ||
|
|
103
|
+
(ownerProviderId != null && policy.providerId === ownerProviderId);
|
|
104
|
+
}
|
|
105
|
+
const PROVIDER_ID_PATTERN = /^@[^/]+\/[^/]+(?:\/[^/]+)*$/;
|
|
106
|
+
const GROUP_KEY_PATTERN = /^[a-z0-9][a-z0-9._-]{0,159}$/u;
|
|
107
|
+
/** The same shape an Add-on manifest may declare a role under. */
|
|
108
|
+
const ADDON_ROLE_NAME_PATTERN = /^[a-z][a-z0-9-]{0,63}$/;
|
|
109
|
+
/**
|
|
110
|
+
* Reads a stored descriptor back into a policy, or null.
|
|
111
|
+
*
|
|
112
|
+
* Whatever does not read as a policy is refused rather than repaired: a half-understood requirement
|
|
113
|
+
* would decide what somebody is shown, and guessing at it is how a stricter rule quietly becomes a
|
|
114
|
+
* looser one.
|
|
115
|
+
*/
|
|
116
|
+
export function readPhiViewerAccessPolicy(value) {
|
|
117
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
const record = value;
|
|
121
|
+
if (record.access === "anyone" ||
|
|
122
|
+
record.access === "anonymous" ||
|
|
123
|
+
record.access === "authenticated") {
|
|
124
|
+
return { access: record.access };
|
|
125
|
+
}
|
|
126
|
+
if (typeof record.providerId !== "string" || !PROVIDER_ID_PATTERN.test(record.providerId)) {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
const providerId = record.providerId;
|
|
130
|
+
if (record.access === "roles" &&
|
|
131
|
+
typeof record.allowedRoleFlags === "number" &&
|
|
132
|
+
Number.isInteger(record.allowedRoleFlags) &&
|
|
133
|
+
record.allowedRoleFlags > 0) {
|
|
134
|
+
return { access: "roles", providerId, allowedRoleFlags: record.allowedRoleFlags };
|
|
135
|
+
}
|
|
136
|
+
if (record.access === "groups" &&
|
|
137
|
+
Array.isArray(record.allowedGroupKeys) &&
|
|
138
|
+
record.allowedGroupKeys.length > 0 &&
|
|
139
|
+
record.allowedGroupKeys.every((key) => typeof key === "string" && GROUP_KEY_PATTERN.test(key))) {
|
|
140
|
+
return {
|
|
141
|
+
access: "groups",
|
|
142
|
+
providerId,
|
|
143
|
+
allowedGroupKeys: [...new Set(record.allowedGroupKeys)],
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
if (record.access === "addon-roles" &&
|
|
147
|
+
Array.isArray(record.allowedRoles) &&
|
|
148
|
+
record.allowedRoles.length > 0 &&
|
|
149
|
+
record.allowedRoles.every((role) => typeof role === "string" && ADDON_ROLE_NAME_PATTERN.test(role))) {
|
|
150
|
+
return {
|
|
151
|
+
access: "addon-roles",
|
|
152
|
+
providerId,
|
|
153
|
+
allowedRoles: [...new Set(record.allowedRoles)],
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
return null;
|
|
157
|
+
}
|