Skip to content

Commit deb2b82

Browse files
authored
Finish PermissionController docstrings, remove redundant type (#141)
Adds missing permission controller docstrings and removes a redundant caveat-related type. Also makes some miscellaneous documentation touch-ups.
1 parent 4f09182 commit deb2b82

2 files changed

Lines changed: 57 additions & 49 deletions

File tree

packages/controllers/src/permissions/Caveat.ts

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { Json } from 'json-rpc-engine';
2-
32
import { UnrecognizedCaveatTypeError } from './errors';
43
import {
54
AsyncRestrictedMethod,
65
RestrictedMethod,
76
PermissionConstraint,
87
RestrictedMethodParameters,
98
} from './Permission';
9+
// This is used in a docstring, but ESLint doesn't notice it.
10+
/* eslint-disable @typescript-eslint/no-unused-vars */
11+
import type { PermissionController } from './PermissionController';
12+
/* eslint-enable @typescript-eslint/no-unused-vars */
1013

1114
export type CaveatConstraint = {
1215
/**
@@ -26,7 +29,11 @@ export type CaveatConstraint = {
2629
};
2730

2831
/**
29-
* TODO:docs
32+
* A `ZCAP-LD`-like caveat object. A caveat is associated with a particular
33+
* permission, and stored in its `caveats` array. Conceptually, a caveat is
34+
* an arbitrary attenuation of the authority granted by its associated
35+
* permission. It is the responsibility of the host to interpret and apply
36+
* the restriction represented by a caveat.
3037
*
3138
* @template Type - The type of the caveat.
3239
* @template Value - The value associated with the caveat.
@@ -60,15 +67,18 @@ export type Caveat<Type extends string, Value extends Json> = {
6067
* @param decorated - The restricted method implementation to be decorated.
6168
* The method may have already been decorated with other caveats.
6269
* @param caveat - The caveat object.
63-
* @returns The decorate restricted method implementation.
70+
* @returns The decorated restricted method implementation.
6471
*/
6572
export type CaveatDecorator<ParentCaveat extends CaveatConstraint> = (
6673
decorated: AsyncRestrictedMethod<RestrictedMethodParameters, Json>,
6774
caveat: ParentCaveat,
6875
) => AsyncRestrictedMethod<RestrictedMethodParameters, Json>;
6976

7077
/**
71-
* TODO:docs
78+
* Extracts a caveat value type from a caveat decorator.
79+
*
80+
* @template Decorator - The {@link CaveatDecorator} to extract a caveat value
81+
* type from.
7282
*/
7383
type ExtractCaveatValueFromDecorator<Decorator extends CaveatDecorator<any>> =
7484
Decorator extends (
@@ -81,7 +91,12 @@ type ExtractCaveatValueFromDecorator<Decorator extends CaveatDecorator<any>> =
8191
: never;
8292

8393
/**
84-
* TODO:docs
94+
* A function for validating caveats of a particular type.
95+
*
96+
* @template ParentCaveat - The caveat type associated with this validator.
97+
* @param caveat - The caveat object to validate.
98+
* @param origin - The origin associated with the parent permission.
99+
* @param target - The target of the parent permission.
85100
*/
86101
export type CaveatValidator<ParentCaveat extends CaveatConstraint> = (
87102
caveat: { type: ParentCaveat['type']; value: unknown },
@@ -90,7 +105,14 @@ export type CaveatValidator<ParentCaveat extends CaveatConstraint> = (
90105
) => void;
91106

92107
/**
93-
* TODO:docs
108+
* The constraint for caveat specification objects. Every {@link Caveat}
109+
* supported by a {@link PermissionController} must have an associated
110+
* specification, which is the source of truth for all caveat-related types.
111+
* In addition, a caveat specification includes the decorator function used
112+
* to apply the caveat's attenuation to a restricted method, and any validator
113+
* function specified by the consumer.
114+
*
115+
* See the README for more details.
94116
*/
95117
export type CaveatSpecificationConstraint = {
96118
/**
@@ -119,51 +141,28 @@ export type CaveatSpecificationConstraint = {
119141
};
120142

121143
/**
122-
* TODO:docs
123-
*/
124-
export type CaveatSpecification<SpecifiedCaveat extends CaveatConstraint> = {
125-
/**
126-
* The string type of the caveat.
127-
*/
128-
type: SpecifiedCaveat['type'];
129-
130-
/**
131-
* The decorator function used to apply the caveat to restricted method
132-
* requests.
133-
*/
134-
decorator: CaveatDecorator<SpecifiedCaveat>;
135-
136-
/**
137-
* The validator function used to validate caveats of the associated type
138-
* whenever they are instantiated. Caveat are instantiated whenever they are
139-
* created or mutated.
140-
*
141-
* The validator should throw an appropriate JSON-RPC error if validation fails.
142-
*
143-
* If no validator is specified, no validation of caveat values will be
144-
* performed. Although caveats can also be validated by permission validators,
145-
* validating caveat values separately is strongly recommended.
146-
*/
147-
validator?: CaveatValidator<SpecifiedCaveat>;
148-
};
149-
150-
/**
151-
* TODO:docs
144+
* The specifications for all caveats supported by a particular
145+
* {@link PermissionController}.
146+
*
147+
* @template Specifications - The union of all {@link CaveatSpecificationConstraint} types.
152148
*/
153149
export type CaveatSpecificationMap<
154-
Specification extends CaveatSpecificationConstraint,
155-
> = Record<Specification['type'], Specification>;
150+
CaveatSpecification extends CaveatSpecificationConstraint,
151+
> = Record<CaveatSpecification['type'], CaveatSpecification>;
156152

157-
// TODO: Figure out why we have to extend "any" in this type.
158153
/**
159-
* TODO:docs
154+
* Extracts the union of all caveat types specified by the given
155+
* {@link CaveatSpecificationConstraint} type.
156+
*
157+
* @template CaveatSpecification - The {@link CaveatSpecificationConstraint} to extract a
158+
* caveat type union from.
160159
*/
161160
export type ExtractCaveats<
162-
Specification extends CaveatSpecification<CaveatConstraint>,
163-
> = Specification extends any
161+
CaveatSpecification extends CaveatSpecificationConstraint,
162+
> = CaveatSpecification extends any
164163
? Caveat<
165-
Specification['type'],
166-
ExtractCaveatValueFromDecorator<Specification['decorator']>
164+
CaveatSpecification['type'],
165+
ExtractCaveatValueFromDecorator<CaveatSpecification['decorator']>
167166
>
168167
: never;
169168

@@ -175,7 +174,7 @@ export type ExtractCaveats<
175174
* @template CaveatType - The type of the caveat to extract.
176175
*/
177176
export type ExtractCaveat<
178-
CaveatSpecifications extends CaveatSpecification<CaveatConstraint>,
177+
CaveatSpecifications extends CaveatSpecificationConstraint,
179178
CaveatType extends string,
180179
> = Extract<ExtractCaveats<CaveatSpecifications>, { type: CaveatType }>;
181180

@@ -187,7 +186,7 @@ export type ExtractCaveat<
187186
* @template CaveatType - The type of the caveat whose value to extract.
188187
*/
189188
export type ExtractCaveatValue<
190-
CaveatSpecifications extends CaveatSpecification<CaveatConstraint>,
189+
CaveatSpecifications extends CaveatSpecificationConstraint,
191190
CaveatType extends string,
192191
> = ExtractCaveat<CaveatSpecifications, CaveatType>['value'];
193192

@@ -198,7 +197,7 @@ export type ExtractCaveatValue<
198197
* decorator) must be awaited.
199198
*/
200199
export function decorateWithCaveats<
201-
CaveatSpecifications extends CaveatSpecification<CaveatConstraint>,
200+
CaveatSpecifications extends CaveatSpecificationConstraint,
202201
>(
203202
methodImplementation: RestrictedMethod<RestrictedMethodParameters, Json>,
204203
permission: Readonly<PermissionConstraint>, // bound to the requesting origin

packages/controllers/src/permissions/Permission.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,16 @@ type ValidTargetKey<Key extends string> = Key extends `${string}_*`
350350
? never
351351
: Key;
352352

353+
/**
354+
* The constraint for permission specification objects. Every {@link Permission}
355+
* supported by a {@link PermissionController} must have an associated
356+
* specification, which is the source of truth for all permission-related types.
357+
* In addition, a permission specification includes the actual implementation
358+
* of restricted methods, a list of permitted caveats, and any factory and
359+
* validation functions specified by the consumer.
360+
*
361+
* See the README for more details.
362+
*/
353363
export type PermissionSpecificationConstraint = {
354364
/**
355365
* The target resource of the permission. At the time of, this is a full
@@ -414,8 +424,7 @@ export type ValidPermissionSpecification<
414424
* The specifications for all permissions supported by a particular
415425
* {@link PermissionController}.
416426
*
417-
* @template Specifications - A union of all {@link PermissionSpecificationConstraint}
418-
* types.
427+
* @template Specifications - The union of all {@link PermissionSpecificationConstraint} types.
419428
*/
420429
export type PermissionSpecificationMap<
421430
Specification extends PermissionSpecificationConstraint,

0 commit comments

Comments
 (0)