generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 96
/
permissions-grant.ts
178 lines (156 loc) · 7.46 KB
/
permissions-grant.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import type { DelegatedGrantMessage } from '../types/delegated-grant-message.js';
import type { PermissionsGrantMessage } from '../types/permissions-types.js';
import type { PermissionsRequest } from './permissions-request.js';
import type { Signer } from '../types/signer.js';
import type { PermissionConditions, PermissionScope, PermissionsGrantDescriptor, RecordsPermissionScope } from '../types/permissions-grant-descriptor.js';
import { AbstractMessage } from '../core/abstract-message.js';
import { Message } from '../core/message.js';
import { removeUndefinedProperties } from '../utils/object.js';
import { Time } from '../utils/time.js';
import { DwnError, DwnErrorCode } from '../core/dwn-error.js';
import { DwnInterfaceName, DwnMethodName } from '../enums/dwn-interface-method.js';
import { normalizeProtocolUrl, normalizeSchemaUrl } from '../utils/url.js';
export type PermissionsGrantOptions = {
messageTimestamp?: string;
/**
* Expire time in UTC ISO-8601 format with microsecond precision.
*/
dateExpires: string;
description?: string;
grantedTo: string;
grantedBy: string;
grantedFor: string;
delegated?: boolean;
permissionsRequestId?: string;
scope: PermissionScope;
conditions?: PermissionConditions;
signer: Signer;
};
export type CreateFromPermissionsRequestOverrides = {
dateExpires: string;
description?: string;
grantedTo?: string;
grantedBy?: string;
grantedFor?: string;
scope?: PermissionScope;
conditions?: PermissionConditions;
};
export class PermissionsGrant extends AbstractMessage<PermissionsGrantMessage> {
public static async parse(message: PermissionsGrantMessage): Promise<PermissionsGrant> {
await Message.validateSignatureStructure(message.authorization.signature, message.descriptor);
PermissionsGrant.validateScope(message);
Time.validateTimestamp(message.descriptor.messageTimestamp);
Time.validateTimestamp(message.descriptor.dateExpires);
return new PermissionsGrant(message);
}
static async create(options: PermissionsGrantOptions): Promise<PermissionsGrant> {
const scope = { ...options.scope } as RecordsPermissionScope;
scope.protocol = scope.protocol !== undefined ? normalizeProtocolUrl(scope.protocol) : undefined;
scope.schema = scope.schema !== undefined ? normalizeSchemaUrl(scope.schema) : undefined;
const descriptor: PermissionsGrantDescriptor = {
interface : DwnInterfaceName.Permissions,
method : DwnMethodName.Grant,
messageTimestamp : options.messageTimestamp ?? Time.getCurrentTimestamp(),
dateExpires : options.dateExpires,
description : options.description,
grantedTo : options.grantedTo,
grantedBy : options.grantedBy,
grantedFor : options.grantedFor,
delegated : options.delegated,
permissionsRequestId : options.permissionsRequestId,
scope : scope,
conditions : options.conditions,
};
// delete all descriptor properties that are `undefined` else the code will encounter the following IPLD issue when attempting to generate CID:
// Error: `undefined` is not supported by the IPLD Data Model and cannot be encoded
removeUndefinedProperties(descriptor);
const authorization = await Message.createAuthorization({ descriptor, signer: options.signer });
const message: PermissionsGrantMessage = { descriptor, authorization };
Message.validateJsonSchema(message);
PermissionsGrant.validateScope(message);
return new PermissionsGrant(message);
}
/**
* A convenience method for casting a PermissionsGrantMessage to a DelegatedGrantMessage if the `delegated` property is `true`.
* @throws {DwnError} if the `delegated` property is not `true`.
*/
public asDelegatedGrant(): DelegatedGrantMessage {
return PermissionsGrant.asDelegatedGrant(this.message);
}
/**
* A convenience method for casting a PermissionsGrantMessage to a DelegatedGrantMessage if the `delegated` property is `true`.
* @throws {DwnError} if the `delegated` property is not `true`.
*/
public static asDelegatedGrant(message: PermissionsGrantMessage): DelegatedGrantMessage {
if (!message.descriptor.delegated) {
throw new DwnError(
DwnErrorCode.PermissionsGrantNotADelegatedGrant,
`PermissionsGrant given is not a delegated grant. Descriptor: ${message.descriptor}`
);
}
return message as DelegatedGrantMessage;
}
/**
* generates a PermissionsGrant using the provided PermissionsRequest
* @param permissionsRequest
* @param signer - the private key and additional signature material of the grantor
* @param overrides - overrides that will be used instead of the properties in `permissionsRequest`
*/
public static async createFromPermissionsRequest(
permissionsRequest: PermissionsRequest,
signer: Signer,
overrides: CreateFromPermissionsRequestOverrides,
): Promise<PermissionsGrant> {
const descriptor = permissionsRequest.message.descriptor;
return PermissionsGrant.create({
dateExpires : overrides.dateExpires,
description : overrides.description ?? descriptor.description,
grantedBy : overrides.grantedBy ?? descriptor.grantedBy,
grantedTo : overrides.grantedTo ?? descriptor.grantedTo,
grantedFor : overrides.grantedFor ?? descriptor.grantedFor,
permissionsRequestId : await Message.getCid(permissionsRequest.message),
scope : overrides.scope ?? descriptor.scope,
conditions : overrides.conditions ?? descriptor.conditions,
signer,
});
}
/**
* Current implementation only allows the DWN owner to store grants they created.
*/
public authorize(): void {
const { grantedBy, grantedFor } = this.message.descriptor;
if (this.author !== grantedBy) {
throw new DwnError(DwnErrorCode.PermissionsGrantGrantedByMismatch, 'Message author must match grantedBy property');
} else if (grantedBy !== grantedFor) {
// Without delegation, only the DWN owner may grant access to their own DWN.
throw new DwnError(
DwnErrorCode.PermissionsGrantUnauthorizedGrant,
`${grantedBy} is not authorized to give access to the DWN belonging to ${grantedFor}`
);
}
}
/**
* Validates scope structure for properties beyond `interface` and `method`.
*/
public static validateScope(permissionsGrantMessage: PermissionsGrantMessage): void {
const recordsScope = permissionsGrantMessage.descriptor.scope as RecordsPermissionScope;
// `schema` scopes may not have protocol-related fields
if (recordsScope.schema !== undefined) {
if (recordsScope.protocol !== undefined || recordsScope.contextId !== undefined || recordsScope.protocolPath) {
throw new DwnError(
DwnErrorCode.PermissionsGrantScopeSchemaProhibitedFields,
'PermissionsGrants for RecordsRead and RecordsWrite that have `schema` present may not also have protocol-related properties present'
);
}
}
if (recordsScope.protocol !== undefined) {
// `contextId` and `protocolPath` are mutually exclusive
if (recordsScope.contextId !== undefined && recordsScope.protocolPath !== undefined) {
throw new DwnError(
DwnErrorCode.PermissionsGrantScopeContextIdAndProtocolPath,
'PermissionsGrants for RecordsRead and RecordsWrite may not have both `contextId` and `protocolPath` present'
);
}
}
}
}