@permission-protocol/sdk 0.1.0-alpha.3 → 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 +21 -0
- package/README.md +61 -28
- package/dist/index.cjs +274 -0
- package/dist/index.d.cts +81 -0
- package/dist/index.d.ts +70 -286
- package/dist/index.js +208 -325
- package/package.json +30 -35
- package/dist/index.d.mts +0 -297
- package/dist/index.mjs +0 -325
package/dist/index.d.ts
CHANGED
|
@@ -1,297 +1,81 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* HashablePayload v1 - the canonical envelope for computing inputHash.
|
|
7
|
-
* Matches spec/hashable-payload-v1.md exactly.
|
|
8
|
-
*/
|
|
9
|
-
interface HashablePayloadV1 {
|
|
10
|
-
tool: string;
|
|
11
|
-
operation: string;
|
|
12
|
-
input: unknown;
|
|
13
|
-
metadata: Record<string, unknown> | null;
|
|
1
|
+
declare const DEFAULT_BASE_URL = "https://app.permissionprotocol.com";
|
|
2
|
+
interface SDKConfig {
|
|
3
|
+
apiKey: string;
|
|
4
|
+
baseUrl: string;
|
|
14
5
|
}
|
|
15
|
-
|
|
16
|
-
* Canonicalize any JSON value.
|
|
17
|
-
*
|
|
18
|
-
* Rules (from spec/hashable-payload-v1.md):
|
|
19
|
-
* - null → "null"
|
|
20
|
-
* - primitives → JSON.stringify(value)
|
|
21
|
-
* - arrays → "[" + items.map(canonicalize).join(",") + "]"
|
|
22
|
-
* - objects: sort keys lexicographically, omit undefined values
|
|
23
|
-
*/
|
|
24
|
-
declare function canonicalize(obj: unknown): string;
|
|
25
|
-
/**
|
|
26
|
-
* Compute inputHash from HashablePayload v1.
|
|
27
|
-
*
|
|
28
|
-
* Algorithm (from spec/hashable-payload-v1.md):
|
|
29
|
-
* 1. Canonicalize the payload
|
|
30
|
-
* 2. SHA-256 hash (UTF-8 encoded)
|
|
31
|
-
* 3. Lowercase hex encoding
|
|
32
|
-
* 4. Prefix with "sha256:"
|
|
33
|
-
*/
|
|
34
|
-
declare function computeHash(payload: HashablePayloadV1): string;
|
|
35
|
-
/**
|
|
36
|
-
* Build HashablePayload v1 from SDK execute options.
|
|
37
|
-
*/
|
|
38
|
-
declare function buildHashablePayload(opts: {
|
|
39
|
-
tool: string;
|
|
40
|
-
operation: string;
|
|
41
|
-
input: unknown;
|
|
42
|
-
metadata?: Record<string, unknown>;
|
|
43
|
-
}): HashablePayloadV1;
|
|
6
|
+
declare function configure(apiKey?: string, baseUrl?: string): SDKConfig;
|
|
44
7
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
8
|
+
declare class PermissionProtocolError extends Error {
|
|
9
|
+
constructor(message: string);
|
|
10
|
+
}
|
|
11
|
+
declare class PermissionDenied extends PermissionProtocolError {
|
|
12
|
+
constructor(message?: string);
|
|
13
|
+
}
|
|
14
|
+
declare class ApprovalTimeout extends PermissionProtocolError {
|
|
15
|
+
constructor(message?: string);
|
|
16
|
+
}
|
|
50
17
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
* Protocol version to use.
|
|
68
|
-
* Default: 1
|
|
69
|
-
*/
|
|
70
|
-
protocolVersion?: number;
|
|
71
|
-
/**
|
|
72
|
-
* SDK version string (sent in X-PP-SDK-Version header).
|
|
73
|
-
* Default: package.json version
|
|
74
|
-
*/
|
|
75
|
-
sdkVersion?: string;
|
|
76
|
-
/**
|
|
77
|
-
* Optional receipt verification hook.
|
|
78
|
-
* Called after receiving a receipt from hosted PP.
|
|
79
|
-
* Return false to reject the receipt (throws INVALID_RECEIPT).
|
|
80
|
-
* Default: no-op (always returns true)
|
|
81
|
-
*
|
|
82
|
-
* Use this hook for future signature verification.
|
|
83
|
-
*/
|
|
84
|
-
verifyReceipt?: (receipt: PermissionReceipt) => boolean | Promise<boolean>;
|
|
18
|
+
type ReceiptStatus = "APPROVED" | "DENIED" | "PENDING" | "EXPIRED" | string;
|
|
19
|
+
interface ReceiptData {
|
|
20
|
+
id: string;
|
|
21
|
+
status: ReceiptStatus;
|
|
22
|
+
action?: string;
|
|
23
|
+
resource?: string;
|
|
24
|
+
actor?: string;
|
|
25
|
+
approved_by?: string | null;
|
|
26
|
+
policy?: string | null;
|
|
27
|
+
signature?: string | null;
|
|
28
|
+
issuer?: string;
|
|
29
|
+
timestamp?: string;
|
|
30
|
+
expires_at?: string | null;
|
|
31
|
+
url?: string;
|
|
32
|
+
valid?: boolean;
|
|
33
|
+
[key: string]: unknown;
|
|
85
34
|
}
|
|
86
|
-
interface
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
tenantId: string;
|
|
91
|
-
/**
|
|
92
|
-
* Agent identifier making the request.
|
|
93
|
-
*/
|
|
94
|
-
agentId: string;
|
|
95
|
-
/**
|
|
96
|
-
* Tool being invoked (e.g., 'wordpress', 'github', 'slack').
|
|
97
|
-
*/
|
|
98
|
-
tool: string;
|
|
99
|
-
/**
|
|
100
|
-
* Operation on the tool (e.g., 'publish_post', 'create_pr').
|
|
101
|
-
*/
|
|
102
|
-
operation: string;
|
|
103
|
-
/**
|
|
104
|
-
* Input payload for the operation.
|
|
105
|
-
* Can be any JSON-serializable value.
|
|
106
|
-
*/
|
|
107
|
-
input: TInput;
|
|
108
|
-
/**
|
|
109
|
-
* Optional metadata for risk assessment.
|
|
110
|
-
* Included in the input hash.
|
|
111
|
-
*/
|
|
35
|
+
interface AuthorizeParams {
|
|
36
|
+
action: string;
|
|
37
|
+
resource: string;
|
|
38
|
+
actor?: string;
|
|
112
39
|
metadata?: Record<string, unknown>;
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
* Default: 'UNKNOWN' (no policy signal encoded by SDK)
|
|
116
|
-
*/
|
|
117
|
-
reversibility?: 'REVERSIBLE' | 'PARTIALLY_REVERSIBLE' | 'IRREVERSIBLE' | 'UNKNOWN';
|
|
118
|
-
/**
|
|
119
|
-
* Execution mode.
|
|
120
|
-
* - 'hosted': Connect to hosted Permission Protocol (default)
|
|
121
|
-
* - 'dev': Local development mode with auto-approval
|
|
122
|
-
*/
|
|
123
|
-
mode?: 'hosted' | 'dev';
|
|
40
|
+
wait?: boolean;
|
|
41
|
+
timeout?: number;
|
|
124
42
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
receipt
|
|
128
|
-
|
|
129
|
-
} | {
|
|
130
|
-
status: 'REQUIRES_APPROVAL';
|
|
131
|
-
receipt: PermissionReceipt;
|
|
132
|
-
} | {
|
|
133
|
-
status: 'DENIED';
|
|
134
|
-
receipt: PermissionReceipt;
|
|
135
|
-
};
|
|
136
|
-
interface PermissionReceipt {
|
|
137
|
-
/**
|
|
138
|
-
* Unique receipt identifier.
|
|
139
|
-
*/
|
|
140
|
-
receiptId: string;
|
|
141
|
-
/**
|
|
142
|
-
* Tenant/organization identifier.
|
|
143
|
-
*/
|
|
144
|
-
tenantId: string;
|
|
145
|
-
/**
|
|
146
|
-
* Agent that requested authorization.
|
|
147
|
-
*/
|
|
148
|
-
agentId: string;
|
|
149
|
-
/**
|
|
150
|
-
* Tool that was requested.
|
|
151
|
-
*/
|
|
152
|
-
tool: string;
|
|
153
|
-
/**
|
|
154
|
-
* Operation that was requested.
|
|
155
|
-
*/
|
|
156
|
-
operation: string;
|
|
157
|
-
/**
|
|
158
|
-
* SHA-256 hash of the canonical input payload.
|
|
159
|
-
* Format: 'sha256:<hex>'
|
|
160
|
-
*/
|
|
161
|
-
inputHash: string;
|
|
162
|
-
/**
|
|
163
|
-
* The decision from Permission Protocol.
|
|
164
|
-
*/
|
|
165
|
-
decision: 'APPROVED' | 'DENIED' | 'REQUIRES_APPROVAL';
|
|
166
|
-
/**
|
|
167
|
-
* Reason codes explaining the decision.
|
|
168
|
-
* May include: REQUIRES_FOUNDER_VETO, APPROVAL_EXPIRED, EXECUTION_ERROR, etc.
|
|
169
|
-
*/
|
|
170
|
-
reasonCodes: string[];
|
|
171
|
-
/**
|
|
172
|
-
* Who approved the action.
|
|
173
|
-
* - 'policy': Auto-approved by policy
|
|
174
|
-
* - 'human': Approved by a human
|
|
175
|
-
* - 'founder': Approved by founder
|
|
176
|
-
* - 'dev_mock': Dev mode auto-approval (NOT valid for production)
|
|
177
|
-
*/
|
|
178
|
-
approver: 'policy' | 'human' | 'founder' | 'dev_mock';
|
|
179
|
-
/**
|
|
180
|
-
* Policy version that made the decision (if available).
|
|
181
|
-
*/
|
|
182
|
-
policyVersion?: string;
|
|
183
|
-
/**
|
|
184
|
-
* When the receipt was created (ISO 8601).
|
|
185
|
-
*/
|
|
186
|
-
createdAt: string;
|
|
187
|
-
/**
|
|
188
|
-
* Receipt signature (future-proof placeholder).
|
|
189
|
-
* Used for verifying receipt authenticity.
|
|
190
|
-
*/
|
|
191
|
-
receiptSig?: string;
|
|
192
|
-
/**
|
|
193
|
-
* Signature algorithm used (future-proof placeholder).
|
|
194
|
-
*/
|
|
195
|
-
sigAlg?: 'ed25519' | 'hmac-sha256';
|
|
43
|
+
interface VerifyResponse {
|
|
44
|
+
valid: boolean;
|
|
45
|
+
receipt?: ReceiptData;
|
|
46
|
+
[key: string]: unknown;
|
|
196
47
|
}
|
|
197
48
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
* @example
|
|
218
|
-
* ```typescript
|
|
219
|
-
* import { PermissionRouter } from '@permission-protocol/sdk';
|
|
220
|
-
*
|
|
221
|
-
* // Configure once
|
|
222
|
-
* PermissionRouter.configure({
|
|
223
|
-
* endpoint: 'https://api.permissionprotocol.com',
|
|
224
|
-
* apiKey: process.env.PP_API_KEY!,
|
|
225
|
-
* });
|
|
226
|
-
*
|
|
227
|
-
* // Request authorization
|
|
228
|
-
* const decision = await PermissionRouter.execute({
|
|
229
|
-
* tenantId: 'acme',
|
|
230
|
-
* agentId: 'seo-agent',
|
|
231
|
-
* tool: 'wordpress',
|
|
232
|
-
* operation: 'publish_post',
|
|
233
|
-
* input: postPayload,
|
|
234
|
-
* });
|
|
235
|
-
*
|
|
236
|
-
* if (decision.status !== 'APPROVED') {
|
|
237
|
-
* // Handle denial or pending approval
|
|
238
|
-
* process.exit(2);
|
|
239
|
-
* }
|
|
240
|
-
*
|
|
241
|
-
* // Tool execution happens elsewhere - SDK does NOT execute tools
|
|
242
|
-
* ```
|
|
243
|
-
*/
|
|
244
|
-
declare const PermissionRouter: {
|
|
245
|
-
/**
|
|
246
|
-
* Configure the Permission Protocol client.
|
|
247
|
-
*
|
|
248
|
-
* Must be called before execute() in hosted mode.
|
|
249
|
-
*
|
|
250
|
-
* @param opts - Configuration options
|
|
251
|
-
*/
|
|
252
|
-
configure(opts: ConfigureOptions): void;
|
|
253
|
-
/**
|
|
254
|
-
* Request authorization for an action.
|
|
255
|
-
*
|
|
256
|
-
* Returns a decision + receipt. Does NOT execute the action.
|
|
257
|
-
*
|
|
258
|
-
* @param opts - Execute options
|
|
259
|
-
* @returns Promise resolving to decision (APPROVED, REQUIRES_APPROVAL, or DENIED) with receipt
|
|
260
|
-
* @throws PermissionProtocolError on network failure, timeout, or invalid response
|
|
261
|
-
*/
|
|
262
|
-
execute<TInput, TResult = unknown>(opts: ExecuteOptions<TInput>): Promise<ExecuteResult<TResult>>;
|
|
263
|
-
/**
|
|
264
|
-
* Check if the client is configured.
|
|
265
|
-
* Useful for testing and debugging.
|
|
266
|
-
*/
|
|
267
|
-
isConfigured(): boolean;
|
|
268
|
-
/**
|
|
269
|
-
* Reset configuration (for testing).
|
|
270
|
-
* @internal
|
|
271
|
-
*/
|
|
272
|
-
_reset(): void;
|
|
273
|
-
};
|
|
49
|
+
declare class Receipt {
|
|
50
|
+
readonly id: string;
|
|
51
|
+
readonly status: ReceiptStatus;
|
|
52
|
+
readonly action?: string;
|
|
53
|
+
readonly resource?: string;
|
|
54
|
+
readonly actor?: string;
|
|
55
|
+
readonly approvedBy?: string | null;
|
|
56
|
+
readonly policy?: string | null;
|
|
57
|
+
readonly signature?: string | null;
|
|
58
|
+
readonly issuer?: string;
|
|
59
|
+
readonly timestamp?: string;
|
|
60
|
+
readonly expiresAt?: string | null;
|
|
61
|
+
readonly url?: string;
|
|
62
|
+
private readonly payload;
|
|
63
|
+
private readonly isValid;
|
|
64
|
+
constructor(payload: ReceiptData);
|
|
65
|
+
get valid(): boolean;
|
|
66
|
+
json(): ReceiptData;
|
|
67
|
+
}
|
|
274
68
|
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
/**
|
|
285
|
-
* Error thrown by Permission Protocol SDK.
|
|
286
|
-
*
|
|
287
|
-
* The SDK fails closed on all errors - there is no silent success path.
|
|
288
|
-
*/
|
|
289
|
-
declare class PermissionProtocolError extends Error {
|
|
290
|
-
/**
|
|
291
|
-
* Error code for programmatic handling.
|
|
292
|
-
*/
|
|
293
|
-
readonly code: ErrorCode;
|
|
294
|
-
constructor(code: ErrorCode, message: string);
|
|
69
|
+
declare function authorize(action: string, resource: string, actor?: string, metadata?: Record<string, unknown>, wait?: boolean, timeout?: number): Promise<Receipt>;
|
|
70
|
+
declare function verify(receiptId: string): Promise<Receipt>;
|
|
71
|
+
interface RequireApprovalOptions {
|
|
72
|
+
action?: string;
|
|
73
|
+
resource: string;
|
|
74
|
+
actor?: string;
|
|
75
|
+
metadata?: Record<string, unknown>;
|
|
76
|
+
wait?: boolean;
|
|
77
|
+
timeout?: number;
|
|
295
78
|
}
|
|
79
|
+
declare function requireApproval(options: RequireApprovalOptions): <TArgs extends unknown[], TResult>(fn: (...args: TArgs) => TResult | Promise<TResult>) => (...args: TArgs) => Promise<TResult>;
|
|
296
80
|
|
|
297
|
-
export { type
|
|
81
|
+
export { ApprovalTimeout, type AuthorizeParams, DEFAULT_BASE_URL, PermissionDenied, PermissionProtocolError, Receipt, type ReceiptData, type ReceiptStatus, type RequireApprovalOptions, type SDKConfig, type VerifyResponse, authorize, configure, requireApproval, verify };
|