@nmnmcc/ability 0.2.0 → 1.0.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/README.md +462 -454
- package/dist/Authorizer.d.ts +241 -0
- package/dist/Authorizer.d.ts.map +1 -0
- package/dist/Authorizer.js +340 -0
- package/dist/Authorizer.js.map +1 -0
- package/dist/Condition.d.ts +915 -0
- package/dist/Condition.d.ts.map +1 -0
- package/dist/Condition.js +1123 -0
- package/dist/Condition.js.map +1 -0
- package/dist/Decision.d.ts +131 -0
- package/dist/Decision.d.ts.map +1 -0
- package/dist/Decision.js +83 -0
- package/dist/Decision.js.map +1 -0
- package/dist/Domain.d.ts +158 -0
- package/dist/Domain.d.ts.map +1 -0
- package/dist/Domain.js +76 -0
- package/dist/Domain.js.map +1 -0
- package/dist/HttpApi.d.ts +173 -0
- package/dist/HttpApi.d.ts.map +1 -0
- package/dist/HttpApi.js +185 -0
- package/dist/HttpApi.js.map +1 -0
- package/dist/Policy.d.ts +687 -0
- package/dist/Policy.d.ts.map +1 -0
- package/dist/Policy.js +1339 -0
- package/dist/Policy.js.map +1 -0
- package/dist/Principal.d.ts +35 -0
- package/dist/Principal.d.ts.map +1 -0
- package/dist/Principal.js +37 -0
- package/dist/Principal.js.map +1 -0
- package/dist/Resource.d.ts +364 -0
- package/dist/Resource.d.ts.map +1 -0
- package/dist/Resource.js +844 -0
- package/dist/Resource.js.map +1 -0
- package/dist/Rule.d.ts +275 -0
- package/dist/Rule.d.ts.map +1 -0
- package/dist/Rule.js +257 -0
- package/dist/Rule.js.map +1 -0
- package/dist/_virtual/_rolldown/runtime.js +1 -2
- package/dist/index.d.ts +9 -3
- package/dist/index.js +9 -3
- package/package.json +61 -72
- package/src/Authorizer.ts +894 -0
- package/src/Condition.ts +2997 -0
- package/src/Decision.ts +117 -0
- package/src/Domain.ts +246 -0
- package/src/HttpApi.ts +348 -0
- package/src/Policy.ts +3315 -0
- package/src/Principal.ts +41 -0
- package/src/Resource.ts +1805 -0
- package/src/Rule.ts +683 -0
- package/src/index.ts +57 -0
- package/dist/Ability.d.ts +0 -491
- package/dist/Ability.js +0 -207
- package/dist/AbilityExtra.d.ts +0 -79
- package/dist/AbilityExtra.js +0 -187
- package/dist/internal/ability.d.ts +0 -90
- package/dist/internal/ability.js +0 -550
- package/dist/internal/serializable.js +0 -22
package/dist/Domain.d.ts
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { Action, Any as Any$1, Data, Descriptor, Kind as Kind$1, Resource, Value } from "./Resource.js";
|
|
2
|
+
import { Any as Any$2 } from "./Principal.js";
|
|
3
|
+
import * as Schema from "effect/Schema";
|
|
4
|
+
import * as HashMap from "effect/HashMap";
|
|
5
|
+
declare namespace Domain_d_exports {
|
|
6
|
+
export { ActionOf, Any, AnyAction, AnyValue, CommonAction, DataOf, Domain, Kind, Meta, Principal, ResourceOf, Resources, ValueOf, descriptorOf, isDomain, make };
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* A domain. `P` is the type of the principal, `Resources` is the union of the
|
|
10
|
+
* resources, and `Meta` is the type of the extra data that a rule may keep.
|
|
11
|
+
*
|
|
12
|
+
* @stability stable
|
|
13
|
+
* @category models
|
|
14
|
+
* @since 0.3.0
|
|
15
|
+
*/
|
|
16
|
+
export interface Domain<P extends object, Resources extends Any$1, Meta> {
|
|
17
|
+
readonly principal: Any$2;
|
|
18
|
+
/** The descriptor of each resource, by its kind name. */
|
|
19
|
+
readonly resources: HashMap.HashMap<string, Descriptor>;
|
|
20
|
+
readonly meta: Schema.Top | undefined;
|
|
21
|
+
readonly "~Domain"?: {
|
|
22
|
+
readonly Principal: P;
|
|
23
|
+
readonly Resources: Resources;
|
|
24
|
+
readonly Meta: Meta;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Any domain.
|
|
29
|
+
*
|
|
30
|
+
* @stability stable
|
|
31
|
+
* @category models
|
|
32
|
+
* @since 0.3.0
|
|
33
|
+
*/
|
|
34
|
+
export type Any = Domain<any, any, any>;
|
|
35
|
+
/**
|
|
36
|
+
* The type of the principal of a domain.
|
|
37
|
+
*
|
|
38
|
+
* @stability stable
|
|
39
|
+
* @category utility types
|
|
40
|
+
* @since 0.3.0
|
|
41
|
+
*/
|
|
42
|
+
export type Principal<D> = D extends Domain<infer P, any, any> ? P : never;
|
|
43
|
+
/**
|
|
44
|
+
* The union of the resources of a domain.
|
|
45
|
+
*
|
|
46
|
+
* @stability stable
|
|
47
|
+
* @category utility types
|
|
48
|
+
* @since 0.3.0
|
|
49
|
+
*/
|
|
50
|
+
export type Resources<D> = D extends Domain<any, infer R, any> ? R : never;
|
|
51
|
+
/**
|
|
52
|
+
* The type of the extra data that a rule of the domain may keep.
|
|
53
|
+
*
|
|
54
|
+
* @stability stable
|
|
55
|
+
* @category utility types
|
|
56
|
+
* @since 0.3.0
|
|
57
|
+
*/
|
|
58
|
+
export type Meta<D> = D extends Domain<any, any, infer M> ? M : never;
|
|
59
|
+
/**
|
|
60
|
+
* The kind names of a domain.
|
|
61
|
+
*
|
|
62
|
+
* @stability stable
|
|
63
|
+
* @category utility types
|
|
64
|
+
* @since 0.3.0
|
|
65
|
+
*/
|
|
66
|
+
export type Kind<D> = Kind$1<Resources<D>>;
|
|
67
|
+
/**
|
|
68
|
+
* The resource of a domain with the kind `K`.
|
|
69
|
+
*
|
|
70
|
+
* @stability stable
|
|
71
|
+
* @category utility types
|
|
72
|
+
* @since 0.3.0
|
|
73
|
+
*/
|
|
74
|
+
export type ResourceOf<D, K extends string> = Resources<D> extends (infer R) ? R extends Resource<K, any, any, any> ? R : never : never;
|
|
75
|
+
/**
|
|
76
|
+
* The actions of the resource with the kind `K`.
|
|
77
|
+
*
|
|
78
|
+
* @stability stable
|
|
79
|
+
* @category utility types
|
|
80
|
+
* @since 0.3.0
|
|
81
|
+
*/
|
|
82
|
+
export type ActionOf<D, K extends string> = Action<ResourceOf<D, K>>;
|
|
83
|
+
/**
|
|
84
|
+
* The actions that every kind in `K` has. For one kind, they are its actions.
|
|
85
|
+
* For a union of kinds, an action of only some of them is left out, so a
|
|
86
|
+
* value of the union type can not get an action that its kind does not have.
|
|
87
|
+
*
|
|
88
|
+
* @stability stable
|
|
89
|
+
* @category utility types
|
|
90
|
+
* @since 1.0.0
|
|
91
|
+
*/
|
|
92
|
+
export type CommonAction<D, K extends string> = (K extends unknown ? (action: ActionOf<D, K>) => void : never) extends ((action: infer A) => void) ? A : never;
|
|
93
|
+
/**
|
|
94
|
+
* All the actions of all the resources of a domain.
|
|
95
|
+
*
|
|
96
|
+
* @stability stable
|
|
97
|
+
* @category utility types
|
|
98
|
+
* @since 0.3.0
|
|
99
|
+
*/
|
|
100
|
+
export type AnyAction<D> = Action<Resources<D>>;
|
|
101
|
+
/**
|
|
102
|
+
* The data type of the resource with the kind `K`.
|
|
103
|
+
*
|
|
104
|
+
* @stability stable
|
|
105
|
+
* @category utility types
|
|
106
|
+
* @since 0.3.0
|
|
107
|
+
*/
|
|
108
|
+
export type DataOf<D, K extends string> = Data<ResourceOf<D, K>>;
|
|
109
|
+
/**
|
|
110
|
+
* The value type of the resource with the kind `K`.
|
|
111
|
+
*
|
|
112
|
+
* @stability stable
|
|
113
|
+
* @category utility types
|
|
114
|
+
* @since 0.3.0
|
|
115
|
+
*/
|
|
116
|
+
export type ValueOf<D, K extends string> = Value<ResourceOf<D, K>>;
|
|
117
|
+
/**
|
|
118
|
+
* The union of the value types of all the resources of a domain.
|
|
119
|
+
*
|
|
120
|
+
* @stability stable
|
|
121
|
+
* @category utility types
|
|
122
|
+
* @since 0.3.0
|
|
123
|
+
*/
|
|
124
|
+
export type AnyValue<D> = Value<Resources<D>>;
|
|
125
|
+
/**
|
|
126
|
+
* Makes a domain. Each resource must have its own kind name. Give `meta` when
|
|
127
|
+
* rules must keep extra data, such as the scope of a grant. The `meta` Schema
|
|
128
|
+
* may not need services, because `Policy.schema` gives a Schema that needs
|
|
129
|
+
* none.
|
|
130
|
+
*
|
|
131
|
+
* @stability stable
|
|
132
|
+
* @category constructors
|
|
133
|
+
* @since 0.3.0
|
|
134
|
+
*/
|
|
135
|
+
export declare const make: <P extends Any$2, const Resources extends ReadonlyArray<Any$1>, M extends Schema.Codec<any, any, never, never> = Schema.Never>(options: {
|
|
136
|
+
readonly principal: P;
|
|
137
|
+
readonly resources: Resources;
|
|
138
|
+
readonly meta?: M;
|
|
139
|
+
}) => Domain<P["Type"], Resources[number], M["Type"]>;
|
|
140
|
+
/**
|
|
141
|
+
* Gives the descriptor of the resource with a kind name, or `undefined` when
|
|
142
|
+
* the domain has no such resource.
|
|
143
|
+
*
|
|
144
|
+
* @stability stable
|
|
145
|
+
* @category getters
|
|
146
|
+
* @since 1.0.0
|
|
147
|
+
*/
|
|
148
|
+
export declare const descriptorOf: (domain: Any, kind: string) => Descriptor | undefined;
|
|
149
|
+
/**
|
|
150
|
+
* Tests if a value is a domain.
|
|
151
|
+
*
|
|
152
|
+
* @stability stable
|
|
153
|
+
* @category guards
|
|
154
|
+
* @since 0.3.0
|
|
155
|
+
*/
|
|
156
|
+
export declare const isDomain: (u: unknown) => u is Any;
|
|
157
|
+
export { Domain_d_exports };
|
|
158
|
+
//# sourceMappingURL=Domain.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Domain.d.ts","names":["Resource.Any","PrincipalSchema.Any","Resource.Descriptor","Resource.Kind","Resource.Resource","Resource.Action","Resource.Data","Resource.Value"],"sources":["../src/Domain.ts"],"mappings":";;;;;;;;;;;;;;;iBAgCiB,OAChB,kBACA,kBAAkBA,OAClB;WAES,WAAWC;;WAEX,WAAW,QAAQ,gBAAgBC;WACnC,MAAM,OAAO;WACb;aACC,WAAW;aACX,WAAW;aACX,MAAM;;;;;;;;;;YAWL,MAAM;;;;;;;;YASN,UAAU,KAAK,UAAU,aAAa,eAAe;;;;;;;;YASrD,UAAU,KAAK,UAAU,kBAAkB,UAAU;;;;;;;;YASrD,KAAK,KAAK,UAAU,uBAAuB,KAAK;;;;;;;;YAShD,KAAK,KAAKC,OAAc,UAAU;;;;;;;;YASlC,WAAW,GAAG,oBACzB,UAAU,kBAAiB,KACxB,UAAUC,SAAkB,oBAC3B;;;;;;;;YAWO,SAAS,GAAG,oBAAoBC,OAAgB,WAAW,GAAG;;;;;;;;;;YAW9D,aAAa,GAAG,qBAC3B,qBAAqB,QAAQ,SAAS,GAAG,+BAC/B,cAAc,cACtB;;;;;;;;YAUS,UAAU,KAAKA,OAAgB,UAAU;;;;;;;;YASzC,OAAO,GAAG,oBAAoBC,KAAc,WAAW,GAAG;;;;;;;;YAS1D,QAAQ,GAAG,oBAAoBC,MAAe,WAAW,GAAG;;;;;;;;YAS5D,SAAS,KAAKA,MAAe,UAAU;;;;;;;;;;;qBAqBtC,OACZ,UAAUN,aACJ,kBAAkB,cAAcD,QACtC,UAAU,OAAO,gCAAgC,OAAO,OACvD;WACQ,WAAW;WACX,WAAW;WACX,OAAO;MACX,OAAO,WAAW,mBAAmB;;;;;;;;;qBAsC9B,eAAY,QAChB,KAAG,iBAETE;;;;;;;;qBAUU,WAAQ,eAAiB,KAAK"}
|
package/dist/Domain.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { __exportAll } from "./_virtual/_rolldown/runtime.js";
|
|
2
|
+
import { descriptor, fieldPaths } from "./Resource.js";
|
|
3
|
+
import * as Arr from "effect/Array";
|
|
4
|
+
import * as Option from "effect/Option";
|
|
5
|
+
import * as Data from "effect/Data";
|
|
6
|
+
import * as HashMap from "effect/HashMap";
|
|
7
|
+
// src/Domain.ts
|
|
8
|
+
/**
|
|
9
|
+
* A domain puts together the principal and all the resources of an
|
|
10
|
+
* application. All other modules get their types from the domain. A rule may
|
|
11
|
+
* only use the actions of its resource. A condition may only use the fields of
|
|
12
|
+
* its resource and of the principal.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { Domain } from "@nmnmcc/ability"
|
|
17
|
+
*
|
|
18
|
+
* const App = Domain.make({ principal: Caller, resources: [Bot, Trigger] })
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @stability stable
|
|
22
|
+
* @since 0.3.0
|
|
23
|
+
*/
|
|
24
|
+
var Domain_exports = /* @__PURE__ */ __exportAll({
|
|
25
|
+
descriptorOf: () => descriptorOf,
|
|
26
|
+
isDomain: () => isDomain,
|
|
27
|
+
make: () => make
|
|
28
|
+
});
|
|
29
|
+
/**
|
|
30
|
+
* The class of every domain value. {@link isDomain} uses it to know a domain.
|
|
31
|
+
*/
|
|
32
|
+
var DomainData = class extends Data.Class {};
|
|
33
|
+
/**
|
|
34
|
+
* Makes a domain. Each resource must have its own kind name. Give `meta` when
|
|
35
|
+
* rules must keep extra data, such as the scope of a grant. The `meta` Schema
|
|
36
|
+
* may not need services, because `Policy.schema` gives a Schema that needs
|
|
37
|
+
* none.
|
|
38
|
+
*
|
|
39
|
+
* @stability stable
|
|
40
|
+
* @category constructors
|
|
41
|
+
* @since 0.3.0
|
|
42
|
+
*/
|
|
43
|
+
const make = (options) => {
|
|
44
|
+
const descriptors = Arr.map(options.resources, descriptor);
|
|
45
|
+
const kinds = Arr.map(descriptors, (descriptor) => descriptor.kind);
|
|
46
|
+
const twice = Arr.findFirst(kinds, (kind, index) => kinds.indexOf(kind) !== index);
|
|
47
|
+
if (Option.isSome(twice)) throw new Error(`Two resources have the kind name "${twice.value}"`);
|
|
48
|
+
if (kinds.includes("*")) throw new Error(`A resource may not have the kind name "*"`);
|
|
49
|
+
const resources = HashMap.fromIterable(Arr.map(descriptors, (descriptor) => [descriptor.kind, descriptor]));
|
|
50
|
+
fieldPaths(options.principal);
|
|
51
|
+
return new DomainData({
|
|
52
|
+
principal: options.principal,
|
|
53
|
+
resources,
|
|
54
|
+
meta: options.meta
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Gives the descriptor of the resource with a kind name, or `undefined` when
|
|
59
|
+
* the domain has no such resource.
|
|
60
|
+
*
|
|
61
|
+
* @stability stable
|
|
62
|
+
* @category getters
|
|
63
|
+
* @since 1.0.0
|
|
64
|
+
*/
|
|
65
|
+
const descriptorOf = (domain, kind) => Option.getOrUndefined(HashMap.get(domain.resources, kind));
|
|
66
|
+
/**
|
|
67
|
+
* Tests if a value is a domain.
|
|
68
|
+
*
|
|
69
|
+
* @stability stable
|
|
70
|
+
* @category guards
|
|
71
|
+
* @since 0.3.0
|
|
72
|
+
*/
|
|
73
|
+
const isDomain = (u) => u instanceof DomainData;
|
|
74
|
+
export { Domain_exports, descriptorOf, isDomain, make };
|
|
75
|
+
|
|
76
|
+
//# sourceMappingURL=Domain.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Domain.js","names":["Resource.descriptor"],"sources":["../src/Domain.ts"],"sourcesContent":["/**\n * A domain puts together the principal and all the resources of an\n * application. All other modules get their types from the domain. A rule may\n * only use the actions of its resource. A condition may only use the fields of\n * its resource and of the principal.\n *\n * @example\n * ```ts\n * import { Domain } from \"@nmnmcc/ability\"\n *\n * const App = Domain.make({ principal: Caller, resources: [Bot, Trigger] })\n * ```\n *\n * @stability stable\n * @since 0.3.0\n */\nimport * as Arr from \"effect/Array\"\nimport * as Data from \"effect/Data\"\nimport * as HashMap from \"effect/HashMap\"\nimport * as Option from \"effect/Option\"\nimport type * as Schema from \"effect/Schema\"\nimport type * as PrincipalSchema from \"./Principal.ts\"\nimport * as Resource from \"./Resource.ts\"\n\n/**\n * A domain. `P` is the type of the principal, `Resources` is the union of the\n * resources, and `Meta` is the type of the extra data that a rule may keep.\n *\n * @stability stable\n * @category models\n * @since 0.3.0\n */\nexport interface Domain<\n\tP extends object,\n\tResources extends Resource.Any,\n\tMeta,\n> {\n\treadonly principal: PrincipalSchema.Any\n\t/** The descriptor of each resource, by its kind name. */\n\treadonly resources: HashMap.HashMap<string, Resource.Descriptor>\n\treadonly meta: Schema.Top | undefined\n\treadonly \"~Domain\"?: {\n\t\treadonly Principal: P\n\t\treadonly Resources: Resources\n\t\treadonly Meta: Meta\n\t}\n}\n\n/**\n * Any domain.\n *\n * @stability stable\n * @category models\n * @since 0.3.0\n */\nexport type Any = Domain<any, any, any>\n\n/**\n * The type of the principal of a domain.\n *\n * @stability stable\n * @category utility types\n * @since 0.3.0\n */\nexport type Principal<D> = D extends Domain<infer P, any, any> ? P : never\n\n/**\n * The union of the resources of a domain.\n *\n * @stability stable\n * @category utility types\n * @since 0.3.0\n */\nexport type Resources<D> = D extends Domain<any, infer R, any> ? R : never\n\n/**\n * The type of the extra data that a rule of the domain may keep.\n *\n * @stability stable\n * @category utility types\n * @since 0.3.0\n */\nexport type Meta<D> = D extends Domain<any, any, infer M> ? M : never\n\n/**\n * The kind names of a domain.\n *\n * @stability stable\n * @category utility types\n * @since 0.3.0\n */\nexport type Kind<D> = Resource.Kind<Resources<D>>\n\n/**\n * The resource of a domain with the kind `K`.\n *\n * @stability stable\n * @category utility types\n * @since 0.3.0\n */\nexport type ResourceOf<D, K extends string> =\n\tResources<D> extends infer R\n\t\t? R extends Resource.Resource<K, any, any, any>\n\t\t\t? R\n\t\t\t: never\n\t\t: never\n\n/**\n * The actions of the resource with the kind `K`.\n *\n * @stability stable\n * @category utility types\n * @since 0.3.0\n */\nexport type ActionOf<D, K extends string> = Resource.Action<ResourceOf<D, K>>\n\n/**\n * The actions that every kind in `K` has. For one kind, they are its actions.\n * For a union of kinds, an action of only some of them is left out, so a\n * value of the union type can not get an action that its kind does not have.\n *\n * @stability stable\n * @category utility types\n * @since 1.0.0\n */\nexport type CommonAction<D, K extends string> = (\n\tK extends unknown ? (action: ActionOf<D, K>) => void : never\n) extends (action: infer A) => void\n\t? A\n\t: never\n\n/**\n * All the actions of all the resources of a domain.\n *\n * @stability stable\n * @category utility types\n * @since 0.3.0\n */\nexport type AnyAction<D> = Resource.Action<Resources<D>>\n\n/**\n * The data type of the resource with the kind `K`.\n *\n * @stability stable\n * @category utility types\n * @since 0.3.0\n */\nexport type DataOf<D, K extends string> = Resource.Data<ResourceOf<D, K>>\n\n/**\n * The value type of the resource with the kind `K`.\n *\n * @stability stable\n * @category utility types\n * @since 0.3.0\n */\nexport type ValueOf<D, K extends string> = Resource.Value<ResourceOf<D, K>>\n\n/**\n * The union of the value types of all the resources of a domain.\n *\n * @stability stable\n * @category utility types\n * @since 0.3.0\n */\nexport type AnyValue<D> = Resource.Value<Resources<D>>\n\n/**\n * The class of every domain value. {@link isDomain} uses it to know a domain.\n */\nclass DomainData extends Data.Class<{\n\treadonly principal: PrincipalSchema.Any\n\treadonly resources: HashMap.HashMap<string, Resource.Descriptor>\n\treadonly meta: Schema.Top | undefined\n}> {}\n\n/**\n * Makes a domain. Each resource must have its own kind name. Give `meta` when\n * rules must keep extra data, such as the scope of a grant. The `meta` Schema\n * may not need services, because `Policy.schema` gives a Schema that needs\n * none.\n *\n * @stability stable\n * @category constructors\n * @since 0.3.0\n */\nexport const make: <\n\tP extends PrincipalSchema.Any,\n\tconst Resources extends ReadonlyArray<Resource.Any>,\n\tM extends Schema.Codec<any, any, never, never> = Schema.Never,\n>(options: {\n\treadonly principal: P\n\treadonly resources: Resources\n\treadonly meta?: M\n}) => Domain<P[\"Type\"], Resources[number], M[\"Type\"]> = (options) => {\n\tconst descriptors = Arr.map(options.resources, Resource.descriptor)\n\tconst kinds = Arr.map(descriptors, (descriptor) => descriptor.kind)\n\t// Two resources with one kind name is a mistake in the code, not in the\n\t// data, so it is a defect.\n\tconst twice = Arr.findFirst(\n\t\tkinds,\n\t\t(kind, index) => kinds.indexOf(kind) !== index,\n\t)\n\tif (Option.isSome(twice)) {\n\t\tthrow new Error(`Two resources have the kind name \"${twice.value}\"`)\n\t}\n\tif (kinds.includes(\"*\")) {\n\t\tthrow new Error(`A resource may not have the kind name \"*\"`)\n\t}\n\tconst resources = HashMap.fromIterable(\n\t\tArr.map(\n\t\t\tdescriptors,\n\t\t\t(descriptor) => [descriptor.kind, descriptor] as const,\n\t\t),\n\t)\n\t// This checks the field names of the principal. See `Resource.fieldPaths`.\n\tResource.fieldPaths(options.principal)\n\treturn new DomainData({\n\t\tprincipal: options.principal,\n\t\tresources,\n\t\tmeta: options.meta,\n\t})\n}\n\n/**\n * Gives the descriptor of the resource with a kind name, or `undefined` when\n * the domain has no such resource.\n *\n * @stability stable\n * @category getters\n * @since 1.0.0\n */\nexport const descriptorOf = (\n\tdomain: Any,\n\tkind: string,\n): Resource.Descriptor | undefined =>\n\tOption.getOrUndefined(HashMap.get(domain.resources, kind))\n\n/**\n * Tests if a value is a domain.\n *\n * @stability stable\n * @category guards\n * @since 0.3.0\n */\nexport const isDomain = (u: unknown): u is Any => u instanceof DomainData\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0KA,IAAM,aAAN,cAAyB,KAAK,MAI3B,CAAC;;;;;;;;;;;AAYJ,MAAa,QAQ4C,YAAY;CACpE,MAAM,cAAc,IAAI,IAAI,QAAQ,WAAWA,UAAmB;CAClE,MAAM,QAAQ,IAAI,IAAI,cAAc,eAAe,WAAW,IAAI;CAGlE,MAAM,QAAQ,IAAI,UACjB,QACC,MAAM,UAAU,MAAM,QAAQ,IAAI,MAAM,KAC1C;CACA,IAAI,OAAO,OAAO,KAAK,GACtB,MAAM,IAAI,MAAM,qCAAqC,MAAM,MAAM,EAAE;CAEpE,IAAI,MAAM,SAAS,GAAG,GACrB,MAAM,IAAI,MAAM,2CAA2C;CAE5D,MAAM,YAAY,QAAQ,aACzB,IAAI,IACH,cACC,eAAe,CAAC,WAAW,MAAM,UAAU,CAC7C,CACD;CAEA,WAAoB,QAAQ,SAAS;CACrC,OAAO,IAAI,WAAW;EACrB,WAAW,QAAQ;EACnB;EACA,MAAM,QAAQ;CACf,CAAC;AACF;;;;;;;;;AAUA,MAAa,gBACZ,QACA,SAEA,OAAO,eAAe,QAAQ,IAAI,OAAO,WAAW,IAAI,CAAC;;;;;;;;AAS1D,MAAa,YAAY,MAAyB,aAAa"}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { Any, CommonAction, Kind, ValueOf } from "./Domain.js";
|
|
2
|
+
import { Scope } from "./Policy.js";
|
|
3
|
+
import { AnyServiceClass, Forbidden, Pending, PolicyUnavailable } from "./Authorizer.js";
|
|
4
|
+
import * as Context from "effect/Context";
|
|
5
|
+
import * as Effect from "effect/Effect";
|
|
6
|
+
import * as Layer from "effect/Layer";
|
|
7
|
+
import * as Schema from "effect/Schema";
|
|
8
|
+
/**
|
|
9
|
+
* The path parameters of a request.
|
|
10
|
+
*
|
|
11
|
+
* @stability unstable
|
|
12
|
+
* @category models
|
|
13
|
+
* @since 1.0.0
|
|
14
|
+
*/
|
|
15
|
+
export type Params = Readonly<Record<string, string | undefined>>;
|
|
16
|
+
/**
|
|
17
|
+
* One check that an endpoint needs: an action on a kind of resource.
|
|
18
|
+
* - With `load`, the middleware loads the resource value from the path
|
|
19
|
+
* parameters and checks the action on that value.
|
|
20
|
+
* - With `within`, it makes a scope from the path parameters and checks the
|
|
21
|
+
* action on every value in the scope.
|
|
22
|
+
* - With `list`, the endpoint gives a list. The handler gets the list query
|
|
23
|
+
* with `Authorizer.filter`, so the middleware checks nothing, unless
|
|
24
|
+
* `nonEmpty` is true: then it fails when the principal may act on no value
|
|
25
|
+
* in the scope.
|
|
26
|
+
* - With none of them, it checks the action on every value of the kind.
|
|
27
|
+
*
|
|
28
|
+
* @stability unstable
|
|
29
|
+
* @category models
|
|
30
|
+
* @since 0.3.0
|
|
31
|
+
*/
|
|
32
|
+
export interface Requirement {
|
|
33
|
+
readonly action: string;
|
|
34
|
+
readonly kind: string;
|
|
35
|
+
readonly load?: (params: Params) => Effect.Effect<object, unknown, unknown>;
|
|
36
|
+
readonly within?: (params: Params) => unknown;
|
|
37
|
+
readonly list?: {
|
|
38
|
+
readonly nonEmpty: boolean;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
declare const Required_base: Context.ServiceClass<Required, "@nmnmcc/ability/HttpApi/Required", readonly Requirement[]>;
|
|
42
|
+
/**
|
|
43
|
+
* The checks that an endpoint needs.
|
|
44
|
+
*
|
|
45
|
+
* @stability unstable
|
|
46
|
+
* @category annotations
|
|
47
|
+
* @since 0.3.0
|
|
48
|
+
*/
|
|
49
|
+
export declare class Required extends Required_base {}
|
|
50
|
+
declare const GroupRequired_base: Context.ServiceClass<GroupRequired, "@nmnmcc/ability/HttpApi/GroupRequired", {
|
|
51
|
+
readonly read: ReadonlyArray<Requirement>;
|
|
52
|
+
readonly write: ReadonlyArray<Requirement>;
|
|
53
|
+
}>;
|
|
54
|
+
/**
|
|
55
|
+
* The checks for every endpoint of a group with no {@link Required}
|
|
56
|
+
* annotation. `read` is for GET requests. `write` is for all other methods.
|
|
57
|
+
*
|
|
58
|
+
* @stability unstable
|
|
59
|
+
* @category annotations
|
|
60
|
+
* @since 0.3.0
|
|
61
|
+
*/
|
|
62
|
+
export declare class GroupRequired extends GroupRequired_base {}
|
|
63
|
+
/**
|
|
64
|
+
* Makes a {@link Requirement} with the types of an authorizer. The action must
|
|
65
|
+
* be an action of the kind, also when the kind has a union type.
|
|
66
|
+
*
|
|
67
|
+
* The last argument is one of:
|
|
68
|
+
* - a `load` function, which must give a value of the kind. It may fail. Its
|
|
69
|
+
* errors must be errors of the middleware, and the services that it needs
|
|
70
|
+
* must be given to {@link layer}.
|
|
71
|
+
* - `{ within }`, a function from the path parameters to a scope. Use it for
|
|
72
|
+
* an act on a part of the kind, such as a new value with no id yet:
|
|
73
|
+
* `{ within: (params) => ($) => $.under("org", params.org ?? null) }`. A path
|
|
74
|
+
* parameter may be `undefined`, and a scope with `undefined` in it is a
|
|
75
|
+
* defect, so give a value for it.
|
|
76
|
+
*
|
|
77
|
+
* @stability unstable
|
|
78
|
+
* @category constructors
|
|
79
|
+
* @since 0.3.0
|
|
80
|
+
*/
|
|
81
|
+
export declare const requires: <A extends {
|
|
82
|
+
readonly domain: Any;
|
|
83
|
+
}, const K extends Kind<A["domain"]>>(authorizer: A, action: CommonAction<A["domain"], K>, kind: K, how?: ((params: Params) => Effect.Effect<ValueOf<A["domain"], K>, unknown, unknown>) | {
|
|
84
|
+
readonly within: (params: Params) => Scope<A["domain"], K>;
|
|
85
|
+
}) => Requirement;
|
|
86
|
+
/**
|
|
87
|
+
* Makes a {@link Requirement} for an endpoint that gives a list. The middleware
|
|
88
|
+
* does not check that the principal may act on every value, because the list
|
|
89
|
+
* shows only the values that it may act on. The handler gets the list query
|
|
90
|
+
* with `Authorizer.filter`, with the same scope.
|
|
91
|
+
*
|
|
92
|
+
* When the principal may act on no value, the list is empty: that is not an
|
|
93
|
+
* error, so a client does not learn that hidden values are there. With
|
|
94
|
+
* `nonEmpty: true`, the middleware fails with `Forbidden` in that case. It
|
|
95
|
+
* also fails when the coverage is `Unknown`, because then it can not say that
|
|
96
|
+
* the list has a value.
|
|
97
|
+
*
|
|
98
|
+
* @stability unstable
|
|
99
|
+
* @category constructors
|
|
100
|
+
* @since 1.0.0
|
|
101
|
+
*/
|
|
102
|
+
export declare const lists: <A extends {
|
|
103
|
+
readonly domain: Any;
|
|
104
|
+
}, const K extends Kind<A["domain"]>>(authorizer: A, action: CommonAction<A["domain"], K>, kind: K, options?: {
|
|
105
|
+
readonly within?: (params: Params) => Scope<A["domain"], K>;
|
|
106
|
+
readonly nonEmpty?: boolean;
|
|
107
|
+
}) => Requirement;
|
|
108
|
+
/**
|
|
109
|
+
* The errors of the middleware, with HTTP status codes: 403 for
|
|
110
|
+
* `Forbidden`, 503 for `PolicyUnavailable`, and 409 for `Pending`.
|
|
111
|
+
*
|
|
112
|
+
* The body of a `PolicyUnavailable` response has only its `_tag`. The cause
|
|
113
|
+
* may have private details, such as an error from the database, so it stays
|
|
114
|
+
* on the server.
|
|
115
|
+
*
|
|
116
|
+
* @stability unstable
|
|
117
|
+
* @category errors
|
|
118
|
+
* @since 0.3.0
|
|
119
|
+
*/
|
|
120
|
+
export declare const errors: readonly [Schema.decodeTo<Schema.declareConstructor<Forbidden, {
|
|
121
|
+
readonly _tag: "Forbidden";
|
|
122
|
+
readonly action: string;
|
|
123
|
+
readonly kind: string;
|
|
124
|
+
readonly field?: string;
|
|
125
|
+
readonly reason?: string;
|
|
126
|
+
}, readonly [Schema.TaggedStruct<"Forbidden", {
|
|
127
|
+
readonly action: Schema.String;
|
|
128
|
+
readonly kind: Schema.String;
|
|
129
|
+
readonly field: Schema.optionalKey<Schema.String>;
|
|
130
|
+
readonly reason: Schema.optionalKey<Schema.String>;
|
|
131
|
+
}>], {
|
|
132
|
+
readonly _tag: "Forbidden";
|
|
133
|
+
readonly action: string;
|
|
134
|
+
readonly kind: string;
|
|
135
|
+
readonly field?: string;
|
|
136
|
+
readonly reason?: string;
|
|
137
|
+
}>, Schema.TaggedStruct<"Forbidden", {
|
|
138
|
+
readonly action: Schema.String;
|
|
139
|
+
readonly kind: Schema.String;
|
|
140
|
+
readonly field: Schema.optionalKey<Schema.String>;
|
|
141
|
+
readonly reason: Schema.optionalKey<Schema.String>;
|
|
142
|
+
}>, never, never>, Schema.decodeTo<Schema.declare<PolicyUnavailable, PolicyUnavailable>, Schema.TaggedStruct<"PolicyUnavailable", {}>, never, never>, Schema.decodeTo<Schema.declareConstructor<Pending, {
|
|
143
|
+
readonly _tag: "Pending";
|
|
144
|
+
readonly ticket: string;
|
|
145
|
+
readonly reason?: string;
|
|
146
|
+
}, readonly [Schema.TaggedStruct<"Pending", {
|
|
147
|
+
readonly ticket: Schema.String;
|
|
148
|
+
readonly reason: Schema.optionalKey<Schema.String>;
|
|
149
|
+
}>], {
|
|
150
|
+
readonly _tag: "Pending";
|
|
151
|
+
readonly ticket: string;
|
|
152
|
+
readonly reason?: string;
|
|
153
|
+
}>, Schema.TaggedStruct<"Pending", {
|
|
154
|
+
readonly ticket: Schema.String;
|
|
155
|
+
readonly reason: Schema.optionalKey<Schema.String>;
|
|
156
|
+
}>, never, never>];
|
|
157
|
+
/**
|
|
158
|
+
* Makes the layer of an authorization middleware. The middleware reads the
|
|
159
|
+
* {@link Required} and {@link GroupRequired} annotations and checks each
|
|
160
|
+
* requirement with the authorizer.
|
|
161
|
+
*
|
|
162
|
+
* The layer takes the authorizer when it is built. The middleware must
|
|
163
|
+
* require the services of the authorizer, such as the identity of the
|
|
164
|
+
* caller. `R` is the type of the services that the `load` functions need. The
|
|
165
|
+
* layer takes them when it is built too.
|
|
166
|
+
*
|
|
167
|
+
* @stability unstable
|
|
168
|
+
* @category layers
|
|
169
|
+
* @since 0.3.0
|
|
170
|
+
*/
|
|
171
|
+
export declare const layer: <Self, Id extends string, A extends AnyServiceClass, R = never>(middleware: Context.ServiceClass<Self, Id, any>, authorizer: A) => Layer.Layer<Self, never, (A extends Context.Key<infer I, any> ? I : never) | R>;
|
|
172
|
+
export {};
|
|
173
|
+
//# sourceMappingURL=HttpApi.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HttpApi.d.ts","names":["Domain.Any","Domain.Kind","Domain.CommonAction","Domain.ValueOf","Policy.Scope","Authorizer.Forbidden","Authorizer.PolicyUnavailable","Authorizer.Pending","Authorizer.AnyServiceClass"],"sources":["../src/HttpApi.ts"],"mappings":";;;;;;;;;;;;;;YAyDY,SAAS,SAAS;;;;;;;;;;;;;;;;;iBAkBb;WACP;WACA;WACA,QAAQ,QAAQ,WAAW,OAAO;WAClC,UAAU,QAAQ;WAClB;aAAkB;;;;;;;;;;;qBAUf,iBAAiB;;WAgBb,MAAA,cAAc;WACb,OAAA,cAAc;;;;;;;;;;qBAJnB,sBAAsB;;;;;;;;;;;;;;;;;;;qBA0BtB,WACZ;WAAqB,QAAQA;SACvB,UAAUC,KAAY,cAE5B,YAAY,GACZ,QAAQC,aAAoB,aAAa,IACzC,MAAM,GACN,QAEG,QAAQ,WACH,OAAO,OAAOC,QAAe,aAAa;WACpC,SAAS,QAAQ,WAAWC,MAAa,aAAa;MAChE;;;;;;;;;;;;;;;;;qBAuBQ,QACZ;WAAqB,QAAQJ;SACvB,UAAUC,KAAY,cAE5B,YAAY,GACZ,QAAQC,aAAoB,aAAa,IACzC,MAAM,GACN;WACU,UAAU,QAAQ,WAAWE,MAAa,aAAa;WACvD;MAEN;;;;;;;;;;;;;qBAuCQ,kBAAM,OAAA,SAAA,OAAA,mBAAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBAyHN,QACZ,MACA,mBACA,UAAUG,iBACV,WAEA,YAAY,QAAQ,aAAa,MAAM,UACvC,YAAY,MACR,MAAM,MACV,cAEC,UAAU,QAAQ,UAAU,UAAU,aAAa"}
|
package/dist/HttpApi.js
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { kindOf } from "./Resource.js";
|
|
2
|
+
import { Forbidden, Pending, PolicyUnavailable } from "./Authorizer.js";
|
|
3
|
+
import * as Context from "effect/Context";
|
|
4
|
+
import * as Effect from "effect/Effect";
|
|
5
|
+
import * as Layer from "effect/Layer";
|
|
6
|
+
import * as Option from "effect/Option";
|
|
7
|
+
import * as Schema from "effect/Schema";
|
|
8
|
+
import * as SchemaTransformation from "effect/SchemaTransformation";
|
|
9
|
+
import { HttpRouter } from "effect/http";
|
|
10
|
+
import { HttpApiSchema } from "effect/http-api";
|
|
11
|
+
// src/HttpApi.ts
|
|
12
|
+
/**
|
|
13
|
+
* Tools to check authorization in an `effect/http-api` server.
|
|
14
|
+
*
|
|
15
|
+
* Each endpoint says what it needs with the {@link Required} annotation. A
|
|
16
|
+
* group can give a default with {@link GroupRequired}: one list for GET
|
|
17
|
+
* requests and one list for all other methods. A middleware checks the
|
|
18
|
+
* requirements before the handler runs, in each group that uses it. An
|
|
19
|
+
* endpoint with no annotation is a defect, so a new endpoint is closed until
|
|
20
|
+
* you say what it needs. An empty list means the endpoint is open. An endpoint
|
|
21
|
+
* in a group without the middleware is not checked at all.
|
|
22
|
+
*
|
|
23
|
+
* ```ts
|
|
24
|
+
* class Authorization extends HttpApiMiddleware.Service<
|
|
25
|
+
* Authorization,
|
|
26
|
+
* { requires: Identity }
|
|
27
|
+
* >()("app/Authorization", { error: HttpApi.errors }) {}
|
|
28
|
+
*
|
|
29
|
+
* const endpoint = HttpApiEndpoint.patch("update", "/bots/:id", { ... })
|
|
30
|
+
* .annotate(HttpApi.Required, [
|
|
31
|
+
* HttpApi.requires(Auth, "update", "Bot", (params) => Bots.find(params.id)),
|
|
32
|
+
* ])
|
|
33
|
+
*
|
|
34
|
+
* const bots = HttpApiGroup.make("bots").add(endpoint).middleware(Authorization)
|
|
35
|
+
*
|
|
36
|
+
* const AuthorizationLive = HttpApi.layer(Authorization, Auth)
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* This module is a separate entry point, `@nmnmcc/ability/HttpApi`, because
|
|
40
|
+
* `effect/http-api` is not stable yet.
|
|
41
|
+
*
|
|
42
|
+
* @stability unstable
|
|
43
|
+
* @since 0.3.0
|
|
44
|
+
*/
|
|
45
|
+
/**
|
|
46
|
+
* The checks that an endpoint needs.
|
|
47
|
+
*
|
|
48
|
+
* @stability unstable
|
|
49
|
+
* @category annotations
|
|
50
|
+
* @since 0.3.0
|
|
51
|
+
*/
|
|
52
|
+
var Required = class extends Context.Service()("@nmnmcc/ability/HttpApi/Required") {};
|
|
53
|
+
/**
|
|
54
|
+
* The checks for every endpoint of a group with no {@link Required}
|
|
55
|
+
* annotation. `read` is for GET requests. `write` is for all other methods.
|
|
56
|
+
*
|
|
57
|
+
* @stability unstable
|
|
58
|
+
* @category annotations
|
|
59
|
+
* @since 0.3.0
|
|
60
|
+
*/
|
|
61
|
+
var GroupRequired = class extends Context.Service()("@nmnmcc/ability/HttpApi/GroupRequired") {};
|
|
62
|
+
/**
|
|
63
|
+
* Makes a {@link Requirement} with the types of an authorizer. The action must
|
|
64
|
+
* be an action of the kind, also when the kind has a union type.
|
|
65
|
+
*
|
|
66
|
+
* The last argument is one of:
|
|
67
|
+
* - a `load` function, which must give a value of the kind. It may fail. Its
|
|
68
|
+
* errors must be errors of the middleware, and the services that it needs
|
|
69
|
+
* must be given to {@link layer}.
|
|
70
|
+
* - `{ within }`, a function from the path parameters to a scope. Use it for
|
|
71
|
+
* an act on a part of the kind, such as a new value with no id yet:
|
|
72
|
+
* `{ within: (params) => ($) => $.under("org", params.org ?? null) }`. A path
|
|
73
|
+
* parameter may be `undefined`, and a scope with `undefined` in it is a
|
|
74
|
+
* defect, so give a value for it.
|
|
75
|
+
*
|
|
76
|
+
* @stability unstable
|
|
77
|
+
* @category constructors
|
|
78
|
+
* @since 0.3.0
|
|
79
|
+
*/
|
|
80
|
+
const requires = (_authorizer, action, kind, how) => how === void 0 ? {
|
|
81
|
+
action,
|
|
82
|
+
kind
|
|
83
|
+
} : typeof how === "function" ? {
|
|
84
|
+
action,
|
|
85
|
+
kind,
|
|
86
|
+
load: how
|
|
87
|
+
} : {
|
|
88
|
+
action,
|
|
89
|
+
kind,
|
|
90
|
+
within: how.within
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Makes a {@link Requirement} for an endpoint that gives a list. The middleware
|
|
94
|
+
* does not check that the principal may act on every value, because the list
|
|
95
|
+
* shows only the values that it may act on. The handler gets the list query
|
|
96
|
+
* with `Authorizer.filter`, with the same scope.
|
|
97
|
+
*
|
|
98
|
+
* When the principal may act on no value, the list is empty: that is not an
|
|
99
|
+
* error, so a client does not learn that hidden values are there. With
|
|
100
|
+
* `nonEmpty: true`, the middleware fails with `Forbidden` in that case. It
|
|
101
|
+
* also fails when the coverage is `Unknown`, because then it can not say that
|
|
102
|
+
* the list has a value.
|
|
103
|
+
*
|
|
104
|
+
* @stability unstable
|
|
105
|
+
* @category constructors
|
|
106
|
+
* @since 1.0.0
|
|
107
|
+
*/
|
|
108
|
+
const lists = (_authorizer, action, kind, options) => ({
|
|
109
|
+
action,
|
|
110
|
+
kind,
|
|
111
|
+
list: { nonEmpty: options?.nonEmpty ?? false },
|
|
112
|
+
...options?.within === void 0 ? {} : { within: options.within }
|
|
113
|
+
});
|
|
114
|
+
/**
|
|
115
|
+
* The Schema of a `PolicyUnavailable` response. It encodes only the `_tag`, so
|
|
116
|
+
* the cause does not go to the client.
|
|
117
|
+
*/
|
|
118
|
+
const PolicyUnavailableResponse = Schema.TaggedStruct("PolicyUnavailable", {}).pipe(Schema.decodeTo(Schema.declare((u) => u instanceof PolicyUnavailable), SchemaTransformation.transform({
|
|
119
|
+
decode: () => PolicyUnavailable.make({ cause: void 0 }),
|
|
120
|
+
encode: () => ({ _tag: "PolicyUnavailable" })
|
|
121
|
+
})));
|
|
122
|
+
/**
|
|
123
|
+
* The errors of the middleware, with HTTP status codes: 403 for
|
|
124
|
+
* `Forbidden`, 503 for `PolicyUnavailable`, and 409 for `Pending`.
|
|
125
|
+
*
|
|
126
|
+
* The body of a `PolicyUnavailable` response has only its `_tag`. The cause
|
|
127
|
+
* may have private details, such as an error from the database, so it stays
|
|
128
|
+
* on the server.
|
|
129
|
+
*
|
|
130
|
+
* @stability unstable
|
|
131
|
+
* @category errors
|
|
132
|
+
* @since 0.3.0
|
|
133
|
+
*/
|
|
134
|
+
const errors = [
|
|
135
|
+
Forbidden.pipe(HttpApiSchema.status(403)),
|
|
136
|
+
PolicyUnavailableResponse.pipe(HttpApiSchema.status(503)),
|
|
137
|
+
Pending.pipe(HttpApiSchema.status(409))
|
|
138
|
+
];
|
|
139
|
+
const makeLayer = (middleware, authorizer) => Layer.effect(middleware, Effect.gen(function* () {
|
|
140
|
+
const service = yield* authorizer;
|
|
141
|
+
const services = yield* Effect.context();
|
|
142
|
+
return Effect.fnUntraced(function* (httpEffect, options) {
|
|
143
|
+
const requirements = Option.orElse(Context.getOption(options.endpoint.annotations, Required), () => Option.map(Context.getOption(options.group.annotations, GroupRequired), (rules) => options.endpoint.method === "GET" ? rules.read : rules.write));
|
|
144
|
+
if (Option.isNone(requirements)) return yield* Effect.die(/* @__PURE__ */ new Error(`The endpoint ${options.group.identifier}.${options.endpoint.name} does not say what it needs. Give it the HttpApi.Required annotation.`));
|
|
145
|
+
const endpoint = `${options.group.identifier}.${options.endpoint.name}`;
|
|
146
|
+
yield* Effect.forEach(requirements.value, ({ action, kind, load, within, list }) => {
|
|
147
|
+
if (load !== void 0) return HttpRouter.params.pipe(Effect.flatMap((params) => Effect.provideContext(load(params), services)), Effect.filterOrElse((value) => kindOf(value) === kind, () => Effect.die(/* @__PURE__ */ new Error(`The load function of ${endpoint} gave a value that is not a ${kind}`))), Effect.flatMap((value) => service.check({
|
|
148
|
+
action,
|
|
149
|
+
resource: value
|
|
150
|
+
})));
|
|
151
|
+
const target = within === void 0 ? Effect.succeed({
|
|
152
|
+
action,
|
|
153
|
+
kind
|
|
154
|
+
}) : Effect.map(HttpRouter.params, (params) => ({
|
|
155
|
+
action,
|
|
156
|
+
kind,
|
|
157
|
+
within: within(params)
|
|
158
|
+
}));
|
|
159
|
+
if (list === void 0) return Effect.flatMap(target, (value) => service.check(value));
|
|
160
|
+
return list.nonEmpty ? target.pipe(Effect.flatMap((value) => service.coverage(value)), Effect.filterOrElse((covered) => covered._tag === "All" || covered._tag === "Some", () => Effect.fail(Forbidden.make({
|
|
161
|
+
action,
|
|
162
|
+
kind
|
|
163
|
+
})))) : Effect.void;
|
|
164
|
+
}, { discard: true });
|
|
165
|
+
return yield* httpEffect;
|
|
166
|
+
});
|
|
167
|
+
}));
|
|
168
|
+
/**
|
|
169
|
+
* Makes the layer of an authorization middleware. The middleware reads the
|
|
170
|
+
* {@link Required} and {@link GroupRequired} annotations and checks each
|
|
171
|
+
* requirement with the authorizer.
|
|
172
|
+
*
|
|
173
|
+
* The layer takes the authorizer when it is built. The middleware must
|
|
174
|
+
* require the services of the authorizer, such as the identity of the
|
|
175
|
+
* caller. `R` is the type of the services that the `load` functions need. The
|
|
176
|
+
* layer takes them when it is built too.
|
|
177
|
+
*
|
|
178
|
+
* @stability unstable
|
|
179
|
+
* @category layers
|
|
180
|
+
* @since 0.3.0
|
|
181
|
+
*/
|
|
182
|
+
const layer = makeLayer;
|
|
183
|
+
export { GroupRequired, Required, errors, layer, lists, requires };
|
|
184
|
+
|
|
185
|
+
//# sourceMappingURL=HttpApi.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HttpApi.js","names":["Authorizer.PolicyUnavailable","Resource.kindOf"],"sources":["../src/HttpApi.ts"],"sourcesContent":["/**\n * Tools to check authorization in an `effect/http-api` server.\n *\n * Each endpoint says what it needs with the {@link Required} annotation. A\n * group can give a default with {@link GroupRequired}: one list for GET\n * requests and one list for all other methods. A middleware checks the\n * requirements before the handler runs, in each group that uses it. An\n * endpoint with no annotation is a defect, so a new endpoint is closed until\n * you say what it needs. An empty list means the endpoint is open. An endpoint\n * in a group without the middleware is not checked at all.\n *\n * ```ts\n * class Authorization extends HttpApiMiddleware.Service<\n * Authorization,\n * { requires: Identity }\n * >()(\"app/Authorization\", { error: HttpApi.errors }) {}\n *\n * const endpoint = HttpApiEndpoint.patch(\"update\", \"/bots/:id\", { ... })\n * .annotate(HttpApi.Required, [\n * HttpApi.requires(Auth, \"update\", \"Bot\", (params) => Bots.find(params.id)),\n * ])\n *\n * const bots = HttpApiGroup.make(\"bots\").add(endpoint).middleware(Authorization)\n *\n * const AuthorizationLive = HttpApi.layer(Authorization, Auth)\n * ```\n *\n * This module is a separate entry point, `@nmnmcc/ability/HttpApi`, because\n * `effect/http-api` is not stable yet.\n *\n * @stability unstable\n * @since 0.3.0\n */\n// A `load` function of a requirement is kept in an annotation, so its error\n// type and its service type are erased to `unknown`. The language service\n// check for `unknown` in an Effect is off here.\n// @effect-diagnostics anyUnknownInErrorContext:off\nimport * as Context from \"effect/Context\"\nimport * as Effect from \"effect/Effect\"\nimport * as Layer from \"effect/Layer\"\nimport * as Option from \"effect/Option\"\nimport * as Schema from \"effect/Schema\"\nimport * as SchemaTransformation from \"effect/SchemaTransformation\"\nimport { HttpRouter } from \"effect/http\"\nimport { HttpApiSchema } from \"effect/http-api\"\nimport * as Authorizer from \"./Authorizer.ts\"\nimport type * as Domain from \"./Domain.ts\"\nimport type * as Policy from \"./Policy.ts\"\nimport * as Resource from \"./Resource.ts\"\n\n/**\n * The path parameters of a request.\n *\n * @stability unstable\n * @category models\n * @since 1.0.0\n */\nexport type Params = Readonly<Record<string, string | undefined>>\n\n/**\n * One check that an endpoint needs: an action on a kind of resource.\n * - With `load`, the middleware loads the resource value from the path\n * parameters and checks the action on that value.\n * - With `within`, it makes a scope from the path parameters and checks the\n * action on every value in the scope.\n * - With `list`, the endpoint gives a list. The handler gets the list query\n * with `Authorizer.filter`, so the middleware checks nothing, unless\n * `nonEmpty` is true: then it fails when the principal may act on no value\n * in the scope.\n * - With none of them, it checks the action on every value of the kind.\n *\n * @stability unstable\n * @category models\n * @since 0.3.0\n */\nexport interface Requirement {\n\treadonly action: string\n\treadonly kind: string\n\treadonly load?: (params: Params) => Effect.Effect<object, unknown, unknown>\n\treadonly within?: (params: Params) => unknown\n\treadonly list?: { readonly nonEmpty: boolean }\n}\n\n/**\n * The checks that an endpoint needs.\n *\n * @stability unstable\n * @category annotations\n * @since 0.3.0\n */\nexport class Required extends Context.Service<\n\tRequired,\n\tReadonlyArray<Requirement>\n>()(\"@nmnmcc/ability/HttpApi/Required\") {}\n\n/**\n * The checks for every endpoint of a group with no {@link Required}\n * annotation. `read` is for GET requests. `write` is for all other methods.\n *\n * @stability unstable\n * @category annotations\n * @since 0.3.0\n */\nexport class GroupRequired extends Context.Service<\n\tGroupRequired,\n\t{\n\t\treadonly read: ReadonlyArray<Requirement>\n\t\treadonly write: ReadonlyArray<Requirement>\n\t}\n>()(\"@nmnmcc/ability/HttpApi/GroupRequired\") {}\n\n/**\n * Makes a {@link Requirement} with the types of an authorizer. The action must\n * be an action of the kind, also when the kind has a union type.\n *\n * The last argument is one of:\n * - a `load` function, which must give a value of the kind. It may fail. Its\n * errors must be errors of the middleware, and the services that it needs\n * must be given to {@link layer}.\n * - `{ within }`, a function from the path parameters to a scope. Use it for\n * an act on a part of the kind, such as a new value with no id yet:\n * `{ within: (params) => ($) => $.under(\"org\", params.org ?? null) }`. A path\n * parameter may be `undefined`, and a scope with `undefined` in it is a\n * defect, so give a value for it.\n *\n * @stability unstable\n * @category constructors\n * @since 0.3.0\n */\nexport const requires: <\n\tA extends { readonly domain: Domain.Any },\n\tconst K extends Domain.Kind<A[\"domain\"]>,\n>(\n\tauthorizer: A,\n\taction: Domain.CommonAction<A[\"domain\"], K>,\n\tkind: K,\n\thow?:\n\t\t| ((\n\t\t\t\tparams: Params,\n\t\t ) => Effect.Effect<Domain.ValueOf<A[\"domain\"], K>, unknown, unknown>)\n\t\t| { readonly within: (params: Params) => Policy.Scope<A[\"domain\"], K> },\n) => Requirement = (_authorizer, action, kind, how) =>\n\thow === undefined\n\t\t? { action: action as string, kind }\n\t\t: typeof how === \"function\"\n\t\t\t? { action: action as string, kind, load: how }\n\t\t\t: { action: action as string, kind, within: how.within }\n\n/**\n * Makes a {@link Requirement} for an endpoint that gives a list. The middleware\n * does not check that the principal may act on every value, because the list\n * shows only the values that it may act on. The handler gets the list query\n * with `Authorizer.filter`, with the same scope.\n *\n * When the principal may act on no value, the list is empty: that is not an\n * error, so a client does not learn that hidden values are there. With\n * `nonEmpty: true`, the middleware fails with `Forbidden` in that case. It\n * also fails when the coverage is `Unknown`, because then it can not say that\n * the list has a value.\n *\n * @stability unstable\n * @category constructors\n * @since 1.0.0\n */\nexport const lists: <\n\tA extends { readonly domain: Domain.Any },\n\tconst K extends Domain.Kind<A[\"domain\"]>,\n>(\n\tauthorizer: A,\n\taction: Domain.CommonAction<A[\"domain\"], K>,\n\tkind: K,\n\toptions?: {\n\t\treadonly within?: (params: Params) => Policy.Scope<A[\"domain\"], K>\n\t\treadonly nonEmpty?: boolean\n\t},\n) => Requirement = (_authorizer, action, kind, options) => ({\n\taction: action as string,\n\tkind,\n\tlist: { nonEmpty: options?.nonEmpty ?? false },\n\t...(options?.within === undefined ? {} : { within: options.within }),\n})\n\n/**\n * The Schema of a `PolicyUnavailable` response. It encodes only the `_tag`, so\n * the cause does not go to the client.\n */\nconst PolicyUnavailableResponse = Schema.TaggedStruct(\n\t\"PolicyUnavailable\",\n\t{},\n).pipe(\n\tSchema.decodeTo(\n\t\tSchema.declare(\n\t\t\t(u): u is Authorizer.PolicyUnavailable =>\n\t\t\t\tu instanceof Authorizer.PolicyUnavailable,\n\t\t),\n\t\tSchemaTransformation.transform({\n\t\t\tdecode: () => Authorizer.PolicyUnavailable.make({ cause: undefined }),\n\t\t\tencode: () => ({ _tag: \"PolicyUnavailable\" as const }),\n\t\t}),\n\t),\n)\n\n/**\n * The errors of the middleware, with HTTP status codes: 403 for\n * `Forbidden`, 503 for `PolicyUnavailable`, and 409 for `Pending`.\n *\n * The body of a `PolicyUnavailable` response has only its `_tag`. The cause\n * may have private details, such as an error from the database, so it stays\n * on the server.\n *\n * @stability unstable\n * @category errors\n * @since 0.3.0\n */\nexport const errors = [\n\tAuthorizer.Forbidden.pipe(HttpApiSchema.status(403)),\n\tPolicyUnavailableResponse.pipe(HttpApiSchema.status(503)),\n\tAuthorizer.Pending.pipe(HttpApiSchema.status(409)),\n] as const\n\nconst makeLayer = (\n\tmiddleware: Context.ServiceClass<any, string, any>,\n\tauthorizer: Authorizer.AnyServiceClass,\n) =>\n\tLayer.effect(\n\t\tmiddleware,\n\t\tEffect.gen(function* () {\n\t\t\tconst service = yield* authorizer\n\t\t\tconst services = yield* Effect.context<never>()\n\t\t\treturn Effect.fnUntraced(function* (\n\t\t\t\thttpEffect: Effect.Effect<unknown, unknown, unknown>,\n\t\t\t\toptions: {\n\t\t\t\t\treadonly endpoint: {\n\t\t\t\t\t\treadonly name: string\n\t\t\t\t\t\treadonly method: string\n\t\t\t\t\t\treadonly annotations: Context.Context<never>\n\t\t\t\t\t}\n\t\t\t\t\treadonly group: {\n\t\t\t\t\t\treadonly identifier: string\n\t\t\t\t\t\treadonly annotations: Context.Context<never>\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t) {\n\t\t\t\tconst requirements = Option.orElse(\n\t\t\t\t\tContext.getOption(options.endpoint.annotations, Required),\n\t\t\t\t\t() =>\n\t\t\t\t\t\tOption.map(\n\t\t\t\t\t\t\tContext.getOption(options.group.annotations, GroupRequired),\n\t\t\t\t\t\t\t(rules) =>\n\t\t\t\t\t\t\t\toptions.endpoint.method === \"GET\" ? rules.read : rules.write,\n\t\t\t\t\t\t),\n\t\t\t\t)\n\t\t\t\tif (Option.isNone(requirements)) {\n\t\t\t\t\treturn yield* Effect.die(\n\t\t\t\t\t\tnew Error(\n\t\t\t\t\t\t\t`The endpoint ${options.group.identifier}.${options.endpoint.name} does not say what it needs. Give it the HttpApi.Required annotation.`,\n\t\t\t\t\t\t),\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tconst endpoint = `${options.group.identifier}.${options.endpoint.name}`\n\t\t\t\t// The requirements are checked in order. The first one that fails\n\t\t\t\t// stops the request.\n\t\t\t\tyield* Effect.forEach(\n\t\t\t\t\trequirements.value,\n\t\t\t\t\t({ action, kind, load, within, list }) => {\n\t\t\t\t\t\tif (load !== undefined) {\n\t\t\t\t\t\t\treturn HttpRouter.params.pipe(\n\t\t\t\t\t\t\t\tEffect.flatMap((params) =>\n\t\t\t\t\t\t\t\t\tEffect.provideContext(load(params), services),\n\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t// The check is for the kind of the requirement. A value of\n\t\t\t\t\t\t\t\t// another kind is a mistake in `load`, and the check must\n\t\t\t\t\t\t\t\t// not pass with the rules of that other kind.\n\t\t\t\t\t\t\t\tEffect.filterOrElse(\n\t\t\t\t\t\t\t\t\t(value) => Resource.kindOf(value) === kind,\n\t\t\t\t\t\t\t\t\t() =>\n\t\t\t\t\t\t\t\t\t\tEffect.die(\n\t\t\t\t\t\t\t\t\t\t\tnew Error(\n\t\t\t\t\t\t\t\t\t\t\t\t`The load function of ${endpoint} gave a value that is not a ${kind}`,\n\t\t\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\tEffect.flatMap((value) =>\n\t\t\t\t\t\t\t\t\tservice.check({ action, resource: value } as never),\n\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tconst target =\n\t\t\t\t\t\t\twithin === undefined\n\t\t\t\t\t\t\t\t? Effect.succeed({ action, kind } as never)\n\t\t\t\t\t\t\t\t: Effect.map(\n\t\t\t\t\t\t\t\t\t\tHttpRouter.params,\n\t\t\t\t\t\t\t\t\t\t(params) =>\n\t\t\t\t\t\t\t\t\t\t\t({ action, kind, within: within(params) }) as never,\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\tif (list === undefined) {\n\t\t\t\t\t\t\treturn Effect.flatMap(target, (value) => service.check(value))\n\t\t\t\t\t\t}\n\t\t\t\t\t\t// A list endpoint may give an empty list, unless it must not.\n\t\t\t\t\t\t// Only `All` and `Some` say that the list has a value. `Unknown`\n\t\t\t\t\t\t// is not a \"yes\".\n\t\t\t\t\t\treturn list.nonEmpty\n\t\t\t\t\t\t\t? target.pipe(\n\t\t\t\t\t\t\t\t\tEffect.flatMap((value) => service.coverage(value)),\n\t\t\t\t\t\t\t\t\tEffect.filterOrElse(\n\t\t\t\t\t\t\t\t\t\t(covered) =>\n\t\t\t\t\t\t\t\t\t\t\t(covered as { readonly _tag: string })._tag === \"All\" ||\n\t\t\t\t\t\t\t\t\t\t\t(covered as { readonly _tag: string })._tag === \"Some\",\n\t\t\t\t\t\t\t\t\t\t() =>\n\t\t\t\t\t\t\t\t\t\t\tEffect.fail(Authorizer.Forbidden.make({ action, kind })),\n\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t: Effect.void\n\t\t\t\t\t},\n\t\t\t\t\t{ discard: true },\n\t\t\t\t)\n\t\t\t\treturn yield* httpEffect\n\t\t\t})\n\t\t}),\n\t)\n\n/**\n * Makes the layer of an authorization middleware. The middleware reads the\n * {@link Required} and {@link GroupRequired} annotations and checks each\n * requirement with the authorizer.\n *\n * The layer takes the authorizer when it is built. The middleware must\n * require the services of the authorizer, such as the identity of the\n * caller. `R` is the type of the services that the `load` functions need. The\n * layer takes them when it is built too.\n *\n * @stability unstable\n * @category layers\n * @since 0.3.0\n */\nexport const layer: <\n\tSelf,\n\tId extends string,\n\tA extends Authorizer.AnyServiceClass,\n\tR = never,\n>(\n\tmiddleware: Context.ServiceClass<Self, Id, any>,\n\tauthorizer: A,\n) => Layer.Layer<\n\tSelf,\n\tnever,\n\t(A extends Context.Key<infer I, any> ? I : never) | R\n> = makeLayer as never\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0FA,IAAa,WAAb,cAA8B,QAAQ,QAGpC,CAAC,CAAC,kCAAkC,CAAC,CAAC,CAAC;;;;;;;;;AAUzC,IAAa,gBAAb,cAAmC,QAAQ,QAMzC,CAAC,CAAC,uCAAuC,CAAC,CAAC,CAAC;;;;;;;;;;;;;;;;;;;AAoB9C,MAAa,YAYO,aAAa,QAAQ,MAAM,QAC9C,QAAQ,KAAA,IACL;CAAU;CAAkB;AAAK,IACjC,OAAO,QAAQ,aACd;CAAU;CAAkB;CAAM,MAAM;AAAI,IAC5C;CAAU;CAAkB;CAAM,QAAQ,IAAI;AAAO;;;;;;;;;;;;;;;;;AAkB1D,MAAa,SAWO,aAAa,QAAQ,MAAM,aAAa;CACnD;CACR;CACA,MAAM,EAAE,UAAU,SAAS,YAAY,MAAM;CAC7C,GAAI,SAAS,WAAW,KAAA,IAAY,CAAC,IAAI,EAAE,QAAQ,QAAQ,OAAO;AACnE;;;;;AAMA,MAAM,4BAA4B,OAAO,aACxC,qBACA,CAAC,CACF,CAAC,CAAC,KACD,OAAO,SACN,OAAO,SACL,MACA,aAAaA,iBACf,GACA,qBAAqB,UAAU;CAC9B,cAAA,kBAA2C,KAAK,EAAE,OAAO,KAAA,EAAU,CAAC;CACpE,eAAe,EAAE,MAAM,oBAA6B;AACrD,CAAC,CACF,CACD;;;;;;;;;;;;;AAcA,MAAa,SAAS;CACA,UAAA,KAAK,cAAc,OAAO,GAAG,CAAC;CACnD,0BAA0B,KAAK,cAAc,OAAO,GAAG,CAAC;CACrC,QAAA,KAAK,cAAc,OAAO,GAAG,CAAC;AAClD;AAEA,MAAM,aACL,YACA,eAEA,MAAM,OACL,YACA,OAAO,IAAI,aAAa;CACvB,MAAM,UAAU,OAAO;CACvB,MAAM,WAAW,OAAO,OAAO,QAAe;CAC9C,OAAO,OAAO,WAAW,WACxB,YACA,SAWC;EACD,MAAM,eAAe,OAAO,OAC3B,QAAQ,UAAU,QAAQ,SAAS,aAAa,QAAQ,SAEvD,OAAO,IACN,QAAQ,UAAU,QAAQ,MAAM,aAAa,aAAa,IACzD,UACA,QAAQ,SAAS,WAAW,QAAQ,MAAM,OAAO,MAAM,KACzD,CACF;EACA,IAAI,OAAO,OAAO,YAAY,GAC7B,OAAO,OAAO,OAAO,oBACpB,IAAI,MACH,gBAAgB,QAAQ,MAAM,WAAW,GAAG,QAAQ,SAAS,KAAK,sEACnE,CACD;EAED,MAAM,WAAW,GAAG,QAAQ,MAAM,WAAW,GAAG,QAAQ,SAAS;EAGjE,OAAO,OAAO,QACb,aAAa,QACZ,EAAE,QAAQ,MAAM,MAAM,QAAQ,WAAW;GACzC,IAAI,SAAS,KAAA,GACZ,OAAO,WAAW,OAAO,KACxB,OAAO,SAAS,WACf,OAAO,eAAe,KAAK,MAAM,GAAG,QAAQ,CAC7C,GAIA,OAAO,cACL,UAAUC,OAAgB,KAAK,MAAM,YAErC,OAAO,oBACN,IAAI,MACH,wBAAwB,SAAS,8BAA8B,MAChE,CACD,CACF,GACA,OAAO,SAAS,UACf,QAAQ,MAAM;IAAE;IAAQ,UAAU;GAAM,CAAU,CACnD,CACD;GAED,MAAM,SACL,WAAW,KAAA,IACR,OAAO,QAAQ;IAAE;IAAQ;GAAK,CAAU,IACxC,OAAO,IACP,WAAW,SACV,YACC;IAAE;IAAQ;IAAM,QAAQ,OAAO,MAAM;GAAE,EAC1C;GACH,IAAI,SAAS,KAAA,GACZ,OAAO,OAAO,QAAQ,SAAS,UAAU,QAAQ,MAAM,KAAK,CAAC;GAK9D,OAAO,KAAK,WACT,OAAO,KACP,OAAO,SAAS,UAAU,QAAQ,SAAS,KAAK,CAAC,GACjD,OAAO,cACL,YACC,QAAsC,SAAS,SAC/C,QAAsC,SAAS,cAEhD,OAAO,KAAA,UAA0B,KAAK;IAAE;IAAQ;GAAK,CAAC,CAAC,CACzD,CACD,IACC,OAAO;EACX,GACA,EAAE,SAAS,KAAK,CACjB;EACA,OAAO,OAAO;CACf,CAAC;AACF,CAAC,CACF;;;;;;;;;;;;;;;AAgBD,MAAa,QAYT"}
|