@assemora/auth 0.1.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 +202 -0
- package/README.md +69 -0
- package/dist/actors.d.ts +20 -0
- package/dist/actors.d.ts.map +1 -0
- package/dist/actors.js +37 -0
- package/dist/actors.js.map +1 -0
- package/dist/authorization.d.ts +36 -0
- package/dist/authorization.d.ts.map +1 -0
- package/dist/authorization.js +93 -0
- package/dist/authorization.js.map +1 -0
- package/dist/commands.d.ts +232 -0
- package/dist/commands.d.ts.map +1 -0
- package/dist/commands.js +390 -0
- package/dist/commands.js.map +1 -0
- package/dist/credentials.d.ts +16 -0
- package/dist/credentials.d.ts.map +1 -0
- package/dist/credentials.js +43 -0
- package/dist/credentials.js.map +1 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +35 -0
- package/dist/index.js.map +1 -0
- package/dist/models.d.ts +136 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.js +92 -0
- package/dist/models.js.map +1 -0
- package/dist/module.d.ts +39 -0
- package/dist/module.d.ts.map +1 -0
- package/dist/module.js +95 -0
- package/dist/module.js.map +1 -0
- package/dist/ownership.d.ts +67 -0
- package/dist/ownership.d.ts.map +1 -0
- package/dist/ownership.js +51 -0
- package/dist/ownership.js.map +1 -0
- package/dist/permissions.d.ts +40 -0
- package/dist/permissions.d.ts.map +1 -0
- package/dist/permissions.js +86 -0
- package/dist/permissions.js.map +1 -0
- package/dist/policies.d.ts +120 -0
- package/dist/policies.d.ts.map +1 -0
- package/dist/policies.js +92 -0
- package/dist/policies.js.map +1 -0
- package/dist/queries.d.ts +166 -0
- package/dist/queries.d.ts.map +1 -0
- package/dist/queries.js +236 -0
- package/dist/queries.js.map +1 -0
- package/dist/sessions.d.ts +40 -0
- package/dist/sessions.d.ts.map +1 -0
- package/dist/sessions.js +39 -0
- package/dist/sessions.js.map +1 -0
- package/dist/tokens.d.ts +40 -0
- package/dist/tokens.d.ts.map +1 -0
- package/dist/tokens.js +60 -0
- package/dist/tokens.js.map +1 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
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 Assemora
|
|
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.
|
|
202
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# `@assemora/auth`
|
|
2
|
+
|
|
3
|
+
Users, sessions, roles, permissions, policies, tokens.
|
|
4
|
+
|
|
5
|
+
**Implementation phase:** 6 — implemented.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
export const ArticlePolicy = policy('articles', {
|
|
9
|
+
read: () => true,
|
|
10
|
+
update: ({ actor, record }) => actor?.id === record.authorId,
|
|
11
|
+
delete: ({ can }) => can('articles.delete'),
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
const app = createApplication({
|
|
15
|
+
modules: [auth({ policies: [ArticlePolicy] }), blog()],
|
|
16
|
+
authorization: policies(),
|
|
17
|
+
})
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Registering this module is what makes `permitAll()` unnecessary: the authorization
|
|
21
|
+
port core has declared since phase 1 finally has an implementation, and every command
|
|
22
|
+
and query starts passing roles, permissions and policies.
|
|
23
|
+
|
|
24
|
+
The check happens twice, because a rule about a record cannot be answered before the
|
|
25
|
+
record is read: permissions before the write, the policy rule once the row is loaded
|
|
26
|
+
(ADR-0015). Both are inside the command pipeline, so no caller can be the one that
|
|
27
|
+
forgets.
|
|
28
|
+
|
|
29
|
+
Nothing sensitive is stored as written. Passwords are Argon2id with OWASP's
|
|
30
|
+
parameters; tokens are SHA-256 digests of 256 random bits, and a token's plaintext
|
|
31
|
+
exists exactly once — when it is issued. An unknown email costs the same time as a
|
|
32
|
+
wrong password, so the timing does not say which addresses are registered.
|
|
33
|
+
|
|
34
|
+
`resolveActor` is what `@assemora/http` accepts to turn a bearer token or a session
|
|
35
|
+
cookie into an actor; neither package depends on the other.
|
|
36
|
+
|
|
37
|
+
## What makes an actor still an actor
|
|
38
|
+
|
|
39
|
+
`permissionsOf` answers what an actor may do **now**, so it asks first whether this is
|
|
40
|
+
still an actor at all:
|
|
41
|
+
|
|
42
|
+
| Actor | Holds nothing once |
|
|
43
|
+
| --- | --- |
|
|
44
|
+
| `user` | the `User` row says `active: false` |
|
|
45
|
+
| `api` | the token is gone, its expiry has passed, or the person it was issued for is deactivated |
|
|
46
|
+
| `agent` | the agent is gone or `enabled: false` |
|
|
47
|
+
|
|
48
|
+
That question belongs here rather than at the credential boundary, because not every
|
|
49
|
+
path presents a credential. A session is checked when the cookie is read and a bearer
|
|
50
|
+
token when the header is — but a **job** carries an actor sealed into an envelope and
|
|
51
|
+
is replayed by a worker hours later with nothing to present (ADR-0023), and so does
|
|
52
|
+
anything else that stores an identity and acts on it afterwards. `permissionsOf` is
|
|
53
|
+
the one place all of them go through.
|
|
54
|
+
|
|
55
|
+
It costs a row read: resolving a user's permissions is four queries rather than three,
|
|
56
|
+
and the command pipeline resolves them twice — once for the permission check and once
|
|
57
|
+
for the policy rule. It buys the only revocation the framework has, so it is paid on
|
|
58
|
+
every command. A deactivated actor short-circuits to a single query.
|
|
59
|
+
|
|
60
|
+
An **absent** `User` row is deliberately not a revocation. There is no user deletion in
|
|
61
|
+
Assemora — `active: false` is the whole of how a person is cut off — so an absence is
|
|
62
|
+
never something this system decided; it is an application whose identities live in an
|
|
63
|
+
SSO directory and whose roles are assigned here.
|
|
64
|
+
|
|
65
|
+
## Workspace dependencies
|
|
66
|
+
|
|
67
|
+
- `@assemora/schema`
|
|
68
|
+
- `@assemora/core`
|
|
69
|
+
- `@assemora/data`
|
package/dist/actors.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turning credentials into an actor (SPEC.md §12, §49).
|
|
3
|
+
*
|
|
4
|
+
* This is what `@assemora/http` accepts as `resolveActor`: the HTTP layer knows about
|
|
5
|
+
* headers and cookies, this package knows what a credential means, and neither
|
|
6
|
+
* depends on the other (SPEC.md §8).
|
|
7
|
+
*/
|
|
8
|
+
import type { Actor } from '@assemora/core';
|
|
9
|
+
export declare const SESSION_COOKIE = "assemora_session";
|
|
10
|
+
/**
|
|
11
|
+
* A bearer token first, then the Studio session cookie. An unrecognised credential
|
|
12
|
+
* is nobody, never an error: what a route does about that is the route's decision.
|
|
13
|
+
*
|
|
14
|
+
* A session token is accepted as a bearer credential too, which is how the SDK and
|
|
15
|
+
* the CLI act as a person rather than as an integration. The browser still gets the
|
|
16
|
+
* cookie, because a token JavaScript can read is a token an injected script can take
|
|
17
|
+
* (SPEC.md §85).
|
|
18
|
+
*/
|
|
19
|
+
export declare const resolveActor: (headers: Readonly<Record<string, string>>) => Promise<Actor | undefined>;
|
|
20
|
+
//# sourceMappingURL=actors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"actors.d.ts","sourceRoot":"","sources":["../src/actors.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAA;AAK3C,eAAO,MAAM,cAAc,qBAAqB,CAAA;AAsBhD;;;;;;;;GAQG;AACH,eAAO,MAAM,YAAY,YACd,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,KACxC,OAAO,CAAC,KAAK,GAAG,SAAS,CAU3B,CAAA"}
|
package/dist/actors.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { SESSION_PREFIX, sessionActor } from './sessions.js';
|
|
2
|
+
import { tokenActor } from './tokens.js';
|
|
3
|
+
export const SESSION_COOKIE = 'assemora_session';
|
|
4
|
+
const cookieValue = (header, name) => {
|
|
5
|
+
if (header === undefined)
|
|
6
|
+
return undefined;
|
|
7
|
+
for (const part of header.split(';')) {
|
|
8
|
+
const [key, ...rest] = part.trim().split('=');
|
|
9
|
+
if (key === name)
|
|
10
|
+
return rest.join('=');
|
|
11
|
+
}
|
|
12
|
+
return undefined;
|
|
13
|
+
};
|
|
14
|
+
const bearer = (header) => {
|
|
15
|
+
if (header === undefined)
|
|
16
|
+
return undefined;
|
|
17
|
+
const [scheme, ...rest] = header.split(' ');
|
|
18
|
+
return scheme?.toLowerCase() === 'bearer' && rest.length > 0 ? rest.join(' ') : undefined;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* A bearer token first, then the Studio session cookie. An unrecognised credential
|
|
22
|
+
* is nobody, never an error: what a route does about that is the route's decision.
|
|
23
|
+
*
|
|
24
|
+
* A session token is accepted as a bearer credential too, which is how the SDK and
|
|
25
|
+
* the CLI act as a person rather than as an integration. The browser still gets the
|
|
26
|
+
* cookie, because a token JavaScript can read is a token an injected script can take
|
|
27
|
+
* (SPEC.md §85).
|
|
28
|
+
*/
|
|
29
|
+
export const resolveActor = async (headers) => {
|
|
30
|
+
const token = bearer(headers.authorization);
|
|
31
|
+
if (token !== undefined) {
|
|
32
|
+
return token.startsWith(`${SESSION_PREFIX}_`) ? sessionActor(token) : tokenActor(token);
|
|
33
|
+
}
|
|
34
|
+
const session = cookieValue(headers.cookie, SESSION_COOKIE);
|
|
35
|
+
return session === undefined ? undefined : sessionActor(session);
|
|
36
|
+
};
|
|
37
|
+
//# sourceMappingURL=actors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"actors.js","sourceRoot":"","sources":["../src/actors.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAC5D,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;A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AAA;IACzF,CAAC;IAED,MAAM,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,MAAM,EAAE,cAAc,CAAC,CAAA;IAE3D,OAAO,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,YAAY,CAAC,OAAO,CAAC,CAAA;AAClE,CAAC,CAAA"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The authorization provider (SPEC.md §50, §51, ADR-0008).
|
|
3
|
+
*
|
|
4
|
+
* `@assemora/core` declares the port and calls it inside the command pipeline; this
|
|
5
|
+
* is the implementation that consults roles, permissions and policies. Registering
|
|
6
|
+
* it is what makes `permitAll()` unnecessary.
|
|
7
|
+
*
|
|
8
|
+
* The check happens in two stages, because a rule about a record cannot be answered
|
|
9
|
+
* before the record is read:
|
|
10
|
+
*
|
|
11
|
+
* 1. `authorize` — permissions. Does this actor hold `articles.update` at all?
|
|
12
|
+
* 2. `authorizeRecord` — the policy rule, with the row in hand.
|
|
13
|
+
*
|
|
14
|
+
* Actions that act on an existing row defer to the second stage; the rest are decided
|
|
15
|
+
* at the first, where `record` is `undefined` by definition.
|
|
16
|
+
*/
|
|
17
|
+
import type { AuthorizationPort, AuthorizationRequest } from '@assemora/core';
|
|
18
|
+
export type CommandSubject = {
|
|
19
|
+
readonly subject: string;
|
|
20
|
+
readonly action: string;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* A command name is already a permission name.
|
|
24
|
+
*
|
|
25
|
+
* `pages.publish` is `publish` on `pages`, so the permission is `pages.publish` —
|
|
26
|
+
* which is exactly how SPEC.md §51 spells `actor.has('articles.delete')`. The
|
|
27
|
+
* `entries.*` commands are the exception: they name their subject in the input, so
|
|
28
|
+
* `entries.update` on `{ resource: 'articles' }` is `articles.update`.
|
|
29
|
+
*
|
|
30
|
+
* `list` and `get` both mean `read`, whatever the subject. A policy that grants
|
|
31
|
+
* reading should not have to say it twice, and `pages.read` reads better in a role
|
|
32
|
+
* than `pages.list` plus `pages.get` (SPEC.md §51).
|
|
33
|
+
*/
|
|
34
|
+
export declare const subjectOf: (request: AuthorizationRequest) => CommandSubject;
|
|
35
|
+
export declare const policies: () => AuthorizationPort;
|
|
36
|
+
//# sourceMappingURL=authorization.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authorization.d.ts","sourceRoot":"","sources":["../src/authorization.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,KAAK,EACV,iBAAiB,EACjB,oBAAoB,EAErB,MAAM,gBAAgB,CAAA;AASvB,MAAM,MAAM,cAAc,GAAG;IAC3B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CACxB,CAAA;AAcD;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,SAAS,YAAa,oBAAoB,KAAG,cAgBzD,CAAA;AAED,eAAO,MAAM,QAAQ,QAAO,iBA6D1B,CAAA"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { ForbiddenError } from '@assemora/core';
|
|
2
|
+
import { holds, permissionsOf } from './permissions.js';
|
|
3
|
+
import { policyFor } from './policies.js';
|
|
4
|
+
/** Actions whose decision needs the row itself. */
|
|
5
|
+
const RECORD_SCOPED = new Set(['update', 'delete', 'restore', 'publish']);
|
|
6
|
+
/** Listing and fetching are the same right, and a policy says so once. */
|
|
7
|
+
/**
|
|
8
|
+
* The actions that are reads, whatever the subject.
|
|
9
|
+
*
|
|
10
|
+
* `translations` is one: it answers which languages an entry is written in, and a role
|
|
11
|
+
* that may read an entry should not need a second grant to be told that (SPEC.md §131).
|
|
12
|
+
* Adding it here rather than granting `entries.translations` separately is what keeps
|
|
13
|
+
* "may read this" one permission instead of a list that grows with every read anybody
|
|
14
|
+
* adds.
|
|
15
|
+
*/
|
|
16
|
+
const READING = new Set(['list', 'get', 'translations']);
|
|
17
|
+
/**
|
|
18
|
+
* A command name is already a permission name.
|
|
19
|
+
*
|
|
20
|
+
* `pages.publish` is `publish` on `pages`, so the permission is `pages.publish` —
|
|
21
|
+
* which is exactly how SPEC.md §51 spells `actor.has('articles.delete')`. The
|
|
22
|
+
* `entries.*` commands are the exception: they name their subject in the input, so
|
|
23
|
+
* `entries.update` on `{ resource: 'articles' }` is `articles.update`.
|
|
24
|
+
*
|
|
25
|
+
* `list` and `get` both mean `read`, whatever the subject. A policy that grants
|
|
26
|
+
* reading should not have to say it twice, and `pages.read` reads better in a role
|
|
27
|
+
* than `pages.list` plus `pages.get` (SPEC.md §51).
|
|
28
|
+
*/
|
|
29
|
+
export const subjectOf = (request) => {
|
|
30
|
+
const input = request.input;
|
|
31
|
+
const resource = typeof input?.resource === 'string' ? input.resource : undefined;
|
|
32
|
+
const separator = request.command.lastIndexOf('.');
|
|
33
|
+
const group = request.command.slice(0, Math.max(separator, 0));
|
|
34
|
+
const action = separator === -1 ? 'execute' : request.command.slice(separator + 1);
|
|
35
|
+
const read = (subject) => ({ subject, action: READING.has(action) ? 'read' : action });
|
|
36
|
+
// A command that names what it acts on has the last word: `blocks.update` edits a
|
|
37
|
+
// page, so `pages.update` is the one permission it takes, at both stages.
|
|
38
|
+
if (request.subject !== undefined)
|
|
39
|
+
return read(request.subject);
|
|
40
|
+
if (group === 'entries' && resource !== undefined)
|
|
41
|
+
return read(resource);
|
|
42
|
+
return read(group === '' ? request.command : group);
|
|
43
|
+
};
|
|
44
|
+
export const policies = () => ({
|
|
45
|
+
async authorize(request) {
|
|
46
|
+
const { subject, action } = subjectOf(request);
|
|
47
|
+
const permissions = await permissionsOf(request.context.actor);
|
|
48
|
+
// Held outright. Nothing is deferred, so nothing is owed at the second stage.
|
|
49
|
+
if (holds(permissions, `${subject}.${action}`))
|
|
50
|
+
return undefined;
|
|
51
|
+
const rules = policyFor(subject)?.rules;
|
|
52
|
+
if (rules === undefined) {
|
|
53
|
+
throw new ForbiddenError(`No permission and no policy allow ${action} on ${subject}`);
|
|
54
|
+
}
|
|
55
|
+
// The row decides, and it has not been read yet. The second stage will ask — and
|
|
56
|
+
// saying so is what makes that a guarantee rather than a convention: the bus
|
|
57
|
+
// refuses to commit a command that was let through here and never asked again.
|
|
58
|
+
if (RECORD_SCOPED.has(action))
|
|
59
|
+
return { deferredTo: { subject, action } };
|
|
60
|
+
const rule = rules[action];
|
|
61
|
+
if (rule === undefined) {
|
|
62
|
+
throw new ForbiddenError(`The policy for ${subject} says nothing about ${action}`);
|
|
63
|
+
}
|
|
64
|
+
const allowed = await rule({
|
|
65
|
+
actor: request.context.actor,
|
|
66
|
+
record: undefined,
|
|
67
|
+
context: request.context,
|
|
68
|
+
can: (permission) => holds(permissions, permission),
|
|
69
|
+
});
|
|
70
|
+
if (!allowed)
|
|
71
|
+
throw new ForbiddenError(`${action} on ${subject} is not allowed`);
|
|
72
|
+
return undefined;
|
|
73
|
+
},
|
|
74
|
+
async authorizeRecord(request) {
|
|
75
|
+
const permissions = await permissionsOf(request.context.actor);
|
|
76
|
+
if (holds(permissions, `${request.subject}.${request.action}`))
|
|
77
|
+
return;
|
|
78
|
+
const rule = policyFor(request.subject)?.rules[request.action];
|
|
79
|
+
if (rule === undefined) {
|
|
80
|
+
throw new ForbiddenError(`No permission and no policy allow ${request.action} on ${request.subject}`);
|
|
81
|
+
}
|
|
82
|
+
const allowed = await rule({
|
|
83
|
+
actor: request.context.actor,
|
|
84
|
+
record: request.record,
|
|
85
|
+
context: request.context,
|
|
86
|
+
can: (permission) => holds(permissions, permission),
|
|
87
|
+
});
|
|
88
|
+
if (!allowed) {
|
|
89
|
+
throw new ForbiddenError(`${request.action} on this ${request.subject} is not allowed`);
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
//# sourceMappingURL=authorization.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authorization.js","sourceRoot":"","sources":["../src/authorization.ts"],"names":[],"mappings":"AAqBA,OAAO,EAAE,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