@andco/protocol 0.0.6 → 0.0.7

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 CHANGED
@@ -66,7 +66,9 @@ parseAndcoGrantRequest({
66
66
  browser packages.
67
67
  - `@andco/protocol/webhooks` contains `andcoWebhooks.verifier()`, which verifies
68
68
  Andco's Ed25519-signed webhook deliveries against Andco's public JWKS (cached
69
- by `kid`), and its header and error constants.
69
+ by `kid`), and its header and error constants. Grant lifecycle events
70
+ (`grant.revoked`, `grant.expired`, `grant.updated`) come back typed with a
71
+ checked payload; narrow with `andcoWebhooks.isGrantEvent(event)`.
70
72
  - `@andco/protocol/server` contains server-only helpers for a confidential
71
73
  client authenticated with `client_secret_basic`.
72
74
 
package/dist/errors.d.ts CHANGED
@@ -106,6 +106,11 @@ export declare const ANDCO_ERROR_CODES: {
106
106
  readonly DEVICE_EXCHANGE_FAILED: "device_exchange_failed";
107
107
  /** Exchanging a refresh token for a new session failed. The Grant may have been revoked. */
108
108
  readonly REFRESH_FAILED: "refresh_failed";
109
+ /**
110
+ * `POST /oauth/revoke` (RFC 7009) did not answer 200: the token belongs to another client, the
111
+ * client could not authenticate, or the request never arrived. The token is kept, so retry.
112
+ */
113
+ readonly REVOCATION_FAILED: "revocation_failed";
109
114
  /** Fetching the OpenID Connect UserInfo endpoint failed. */
110
115
  readonly USERINFO_FAILED: "userinfo_failed";
111
116
  /** A callback could not be correlated with the transaction that started it, so it is not trusted. */
@@ -1 +1 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,eAAO,MAAM,iBAAiB;IAE5B,uGAAuG;;IAEvG,6EAA6E;;IAE7E,uGAAuG;;IAEvG,6DAA6D;;IAE7D,wFAAwF;;IAMxF,+GAA+G;;IAE/G,wEAAwE;;IAExE,6DAA6D;;IAE7D,0DAA0D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EAAE,YAAY;IAM3D,+DAA+D;WACjD,EAAE,CAAC,CAAC,SAAS,eAAe,EAAE,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,GAAG,KAAK,IAAI,CAAC;CAG/G;AAED,oEAAoE;AACpE,qBAAa,aAAc,SAAQ,eAAe;IAChD,SAAgB,IAAI,EAAE,cAAc,CAAC;IACrC,SAAgB,OAAO,EAAE,OAAO,CAAC;gBAEd,IAAI,EAAE,cAAc,EAAE,OAAO,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,YAAY;CAKnF"}
package/dist/errors.js CHANGED
@@ -112,6 +112,11 @@ export const ANDCO_ERROR_CODES = {
112
112
  DEVICE_EXCHANGE_FAILED: "device_exchange_failed",
113
113
  /** Exchanging a refresh token for a new session failed. The Grant may have been revoked. */
114
114
  REFRESH_FAILED: "refresh_failed",
115
+ /**
116
+ * `POST /oauth/revoke` (RFC 7009) did not answer 200: the token belongs to another client, the
117
+ * client could not authenticate, or the request never arrived. The token is kept, so retry.
118
+ */
119
+ REVOCATION_FAILED: "revocation_failed",
115
120
  /** Fetching the OpenID Connect UserInfo endpoint failed. */
116
121
  USERINFO_FAILED: "userinfo_failed",
117
122
  /** A callback could not be correlated with the transaction that started it, so it is not trusted. */
@@ -19,7 +19,8 @@ export declare const ServiceGrantSchema: v.ObjectSchema<{
19
19
  }, undefined>;
20
20
  readonly resource: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.UrlAction<string, undefined>]>;
21
21
  readonly createdAt: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.CheckAction<string, undefined>]>;
22
- readonly status: v.PicklistSchema<["active", "revoked"], undefined>;
22
+ /** `expired`: its expiry passed (ADR 0034); like `revoked`, it stays in the authorizations history. */
23
+ readonly status: v.PicklistSchema<["active", "revoked", "expired"], undefined>;
23
24
  readonly version: v.StringSchema<undefined>;
24
25
  readonly scopes: v.ArraySchema<v.StringSchema<undefined>, undefined>;
25
26
  readonly authorizationDetails: v.ArraySchema<v.RecordSchema<v.StringSchema<undefined>, v.UnknownSchema, undefined>, undefined>;
@@ -1 +1 @@
1
- {"version":3,"file":"service-grants.d.ts","sourceRoot":"","sources":["../src/service-grants.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,SAAS,CAAC;AAC7B,OAAO,EAAE,gCAAgC,EAAE,MAAM,kBAAkB,CAAC;AAEpE;;;;;;;;;GASG;AACH,eAAO,MAAM,kBAAkB;;;;;;;;;;;;;aAa7B,CAAC;AACH,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,WAAW,CAAC,OAAO,kBAAkB,CAAC,CAAC;AACpE,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,SAAS,YAAY,EAAE,CAE1E;AACD;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,OAAO,GAAG,CAAC,CAAC,WAAW,CAAC,OAAO,gCAAgC,CAAC,CAE1G;AACD,eAAO,MAAM,kBAAkB;;;;;aAK7B,CAAC;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,CAAC,CAAC,WAAW,CAAC,OAAO,kBAAkB,CAAC,CAE1F"}
1
+ {"version":3,"file":"service-grants.d.ts","sourceRoot":"","sources":["../src/service-grants.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,SAAS,CAAC;AAC7B,OAAO,EAAE,gCAAgC,EAAE,MAAM,kBAAkB,CAAC;AAEpE;;;;;;;;;GASG;AACH,eAAO,MAAM,kBAAkB;;;;;;;;;IAS7B,uGAAuG;;;;;aAKvG,CAAC;AACH,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,WAAW,CAAC,OAAO,kBAAkB,CAAC,CAAC;AACpE,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,SAAS,YAAY,EAAE,CAE1E;AACD;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,OAAO,GAAG,CAAC,CAAC,WAAW,CAAC,OAAO,gCAAgC,CAAC,CAE1G;AACD,eAAO,MAAM,kBAAkB;;;;;aAK7B,CAAC;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,CAAC,CAAC,WAAW,CAAC,OAAO,kBAAkB,CAAC,CAE1F"}
@@ -16,7 +16,8 @@ export const ServiceGrantSchema = /* @__PURE__ */ v.object({
16
16
  subject: GrantedOrganizationSubjectSchema,
17
17
  resource: v.pipe(v.string(), v.url()),
18
18
  createdAt: v.pipe(v.string(), v.check((date) => Number.isFinite(Date.parse(date)))),
19
- status: v.picklist(["active", "revoked"]),
19
+ /** `expired`: its expiry passed (ADR 0034); like `revoked`, it stays in the authorizations history. */
20
+ status: v.picklist(["active", "revoked", "expired"]),
20
21
  version: v.string(),
21
22
  scopes: v.array(v.string()),
22
23
  authorizationDetails: v.array(v.record(v.string(), v.unknown())),
@@ -41,7 +41,7 @@ export declare const ANDCO_WEBHOOK_ERROR_CODES: {
41
41
  readonly UNKNOWN_KEY: "webhook_unknown_key";
42
42
  /** The signature does not match the signed content. */
43
43
  readonly INVALID_SIGNATURE: "webhook_invalid_signature";
44
- /** The body is not an event, or its `id` differs from `andco-event-id`. */
44
+ /** The body is not an event, its `id` differs from `andco-event-id`, or a `grant.*` payload is malformed. */
45
45
  readonly INVALID_PAYLOAD: "webhook_invalid_payload";
46
46
  /** The JWKS could not be fetched or is not a JWKS. */
47
47
  readonly JWKS_UNAVAILABLE: "webhook_jwks_unavailable";
@@ -67,14 +67,18 @@ export declare class AndcoWebhookError extends Error {
67
67
  });
68
68
  }
69
69
  /**
70
- * One Andco event as delivered to a Project's webhook endpoint. Every event of every client of the
71
- * Project arrives at the same endpoint: filter by `client_id`, `grant_id` or `type` yourself.
70
+ * The envelope every Andco webhook event shares, with its `type` and `payload`.
71
+ *
72
+ * @example
73
+ * ```ts
74
+ * type Paid = AndcoWebhookEnvelope<"deposit.completed", { intent_id: string }>;
75
+ * ```
72
76
  */
73
- export type AndcoWebhookEvent = {
77
+ export type AndcoWebhookEnvelope<T extends string, P> = {
74
78
  /** Stable event id, equal to `andco-event-id`. Deduplicate durable effects by it. */
75
79
  id: string;
76
- /** Event type, such as `deposit.completed` or `withdrawal.posted`. */
77
- type: string;
80
+ /** Event type, such as `deposit.completed` or `grant.revoked`. */
81
+ type: T;
78
82
  version: 1;
79
83
  /** ISO 8601 instant of the fact. */
80
84
  occurred_at: string;
@@ -85,8 +89,59 @@ export type AndcoWebhookEvent = {
85
89
  /** Opaque position of the event in its subject's replay feed. */
86
90
  cursor: string;
87
91
  /** Event-specific facts. Money values are strings. */
88
- payload: Record<string, unknown>;
92
+ payload: P;
89
93
  };
94
+ /** Why a Grant was revoked: by the person (Platform, the native Host, Andco) or by the client (`/oauth/revoke`). */
95
+ export type AndcoGrantRevocationReason = "revoked_by_person" | "revoked_by_client";
96
+ /**
97
+ * A Grant was revoked (ADR 0034): its tokens are useless now; delete the ones you keep and ask for
98
+ * consent again.
99
+ */
100
+ export type AndcoGrantRevokedEvent = AndcoWebhookEnvelope<"grant.revoked", {
101
+ grant_id: string;
102
+ client_id: string;
103
+ resource: string;
104
+ reason: AndcoGrantRevocationReason;
105
+ revoked_at: string;
106
+ }>;
107
+ /** A Grant reached the expiry the person chose: its tokens are useless now; ask for consent again. */
108
+ export type AndcoGrantExpiredEvent = AndcoWebhookEnvelope<"grant.expired", {
109
+ grant_id: string;
110
+ client_id: string;
111
+ resource: string;
112
+ expires_at: string;
113
+ }>;
114
+ /** The person changed a Grant's expiry; `expires_at` is `null` when they removed it. */
115
+ export type AndcoGrantUpdatedEvent = AndcoWebhookEnvelope<"grant.updated", {
116
+ grant_id: string;
117
+ client_id: string;
118
+ resource: string;
119
+ expires_at: string | null;
120
+ }>;
121
+ /**
122
+ * A Grant lifecycle event: the only events delivered although their Grant is no longer active.
123
+ * Narrow to it with `andcoWebhooks.isGrantEvent(event)`, then on `type`.
124
+ */
125
+ export type AndcoGrantWebhookEvent = AndcoGrantRevokedEvent | AndcoGrantExpiredEvent | AndcoGrantUpdatedEvent;
126
+ /** The Grant lifecycle event types the verifier checks and types. */
127
+ export declare const ANDCO_WEBHOOK_GRANT_EVENT_TYPES: readonly ["grant.revoked", "grant.expired", "grant.updated"];
128
+ /**
129
+ * One Andco event as delivered to a Project's webhook endpoint. Every event of every client of the
130
+ * Project arrives at the same endpoint: filter by `client_id`, `grant_id` or `type` yourself.
131
+ *
132
+ * Grant lifecycle events arrive typed and their payload is checked; narrow with
133
+ * `andcoWebhooks.isGrantEvent(event)`, since a plain `type` comparison cannot rule out the untyped
134
+ * member. Every other event carries its payload as a record.
135
+ *
136
+ * @example
137
+ * ```ts
138
+ * const event = await verify(request);
139
+ * if (andcoWebhooks.isGrantEvent(event) && event.type === "grant.revoked") {
140
+ * await forgetTokens(event.payload.grant_id, event.payload.reason);
141
+ * }
142
+ * ```
143
+ */
144
+ export type AndcoWebhookEvent = AndcoGrantWebhookEvent | AndcoWebhookEnvelope<string, Record<string, unknown>>;
90
145
  /** A raw delivery, for runtimes that do not hand the handler a Fetch `Request`. */
91
146
  export type AndcoWebhookDelivery = {
92
147
  /** The exact request body, before any JSON parsing. Re-serialized JSON does not verify. */
@@ -106,6 +161,18 @@ export type AndcoWebhookVerifierOptions = {
106
161
  };
107
162
  /** Verifies one delivery and returns its event, or throws {@link AndcoWebhookError}. */
108
163
  export type AndcoWebhookVerify = (delivery: Request | AndcoWebhookDelivery) => Promise<AndcoWebhookEvent>;
164
+ /**
165
+ * Whether an event is a Grant lifecycle event (`grant.revoked`, `grant.expired`, `grant.updated`),
166
+ * whose payload the verifier already checked. Narrows the event so `type` then picks the payload.
167
+ *
168
+ * @example
169
+ * ```ts
170
+ * if (andcoWebhooks.isGrantEvent(event)) {
171
+ * if (event.type === "grant.updated") console.log(event.payload.expires_at); // string | null
172
+ * }
173
+ * ```
174
+ */
175
+ declare function isGrantEvent(event: AndcoWebhookEvent): event is AndcoGrantWebhookEvent;
109
176
  /**
110
177
  * Creates a verifier for Andco webhook deliveries.
111
178
  *
@@ -129,6 +196,7 @@ export type AndcoWebhookVerify = (delivery: Request | AndcoWebhookDelivery) => P
129
196
  * export async function POST(request: Request) {
130
197
  * const event = await verify(request);
131
198
  * if (event.type === "deposit.completed") await markPaid(event.payload["intent_id"] as string);
199
+ * if (andcoWebhooks.isGrantEvent(event) && event.type !== "grant.updated") await forgetTokens(event.payload.grant_id);
132
200
  * return new Response(null, { status: 204 });
133
201
  * }
134
202
  * ```
@@ -142,13 +210,14 @@ export type AndcoWebhookVerify = (delivery: Request | AndcoWebhookDelivery) => P
142
210
  */
143
211
  declare function verifier(options?: AndcoWebhookVerifierOptions): AndcoWebhookVerify;
144
212
  /**
145
- * Receiving Andco webhooks. See {@link verifier}.
213
+ * Receiving Andco webhooks. See {@link verifier} and {@link isGrantEvent}.
146
214
  *
147
215
  * @example
148
216
  * const verify = andcoWebhooks.verifier();
149
217
  */
150
218
  export declare const andcoWebhooks: {
151
219
  readonly verifier: typeof verifier;
220
+ readonly isGrantEvent: typeof isGrantEvent;
152
221
  };
153
222
  export {};
154
223
  //# sourceMappingURL=webhooks.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"webhooks.d.ts","sourceRoot":"","sources":["../src/webhooks.ts"],"names":[],"mappings":"AAEA,uDAAuD;AACvD,eAAO,MAAM,qBAAqB;;;;CAIxB,CAAC;AAEX,wEAAwE;AACxE,eAAO,MAAM,uBAAuB,EAAG,SAAkB,CAAC;AAE1D,oFAAoF;AACpF,eAAO,MAAM,+BAA+B,MAAM,CAAC;AAEnD,wFAAwF;AACxF,eAAO,MAAM,sBAAsB,uDAA+D,CAAC;AAEnG;;;;;;;;;GASG;AACH,wBAAgB,4BAA4B,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAExG;AAED;;;;;;GAMG;AACH,wBAAgB,8BAA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oBAAoB,KAAK,OAAO,CAAC,iBAAiB,CAAC,CAAC;AA+C1G;;;;;;;;;;GAUG;AACH,iBAAS,YAAY,CAAC,KAAK,EAAE,iBAAiB,GAAG,KAAK,IAAI,sBAAsB,CAE/E;AAuBD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,iBAAS,QAAQ,CAAC,OAAO,GAAE,2BAAgC,GAAG,kBAAkB,CAiG/E;AAED;;;;;GAKG;AACH,eAAO,MAAM,aAAa;;;CAAsC,CAAC"}
package/dist/webhooks.js CHANGED
@@ -46,7 +46,7 @@ export const ANDCO_WEBHOOK_ERROR_CODES = {
46
46
  UNKNOWN_KEY: "webhook_unknown_key",
47
47
  /** The signature does not match the signed content. */
48
48
  INVALID_SIGNATURE: "webhook_invalid_signature",
49
- /** The body is not an event, or its `id` differs from `andco-event-id`. */
49
+ /** The body is not an event, its `id` differs from `andco-event-id`, or a `grant.*` payload is malformed. */
50
50
  INVALID_PAYLOAD: "webhook_invalid_payload",
51
51
  /** The JWKS could not be fetched or is not a JWKS. */
52
52
  JWKS_UNAVAILABLE: "webhook_jwks_unavailable",
@@ -71,6 +71,8 @@ export class AndcoWebhookError extends Error {
71
71
  this.code = code;
72
72
  }
73
73
  }
74
+ /** The Grant lifecycle event types the verifier checks and types. */
75
+ export const ANDCO_WEBHOOK_GRANT_EVENT_TYPES = ["grant.revoked", "grant.expired", "grant.updated"];
74
76
  const SIGNATURE_HEADER = /^\s*kid=([A-Za-z0-9._:-]{1,128})\s*,\s*alg=([A-Za-z0-9]+)\s*,\s*sig=([A-Za-z0-9_-]+)\s*$/;
75
77
  /** A new `kid` triggers at most one JWKS refresh per this interval, so forged kids cannot flood Andco. */
76
78
  const UNKNOWN_KID_REFRESH_MS = 60_000;
@@ -112,6 +114,41 @@ function isEvent(value) {
112
114
  typeof event["payload"] === "object" &&
113
115
  !Array.isArray(event["payload"]));
114
116
  }
117
+ /**
118
+ * Whether an event is a Grant lifecycle event (`grant.revoked`, `grant.expired`, `grant.updated`),
119
+ * whose payload the verifier already checked. Narrows the event so `type` then picks the payload.
120
+ *
121
+ * @example
122
+ * ```ts
123
+ * if (andcoWebhooks.isGrantEvent(event)) {
124
+ * if (event.type === "grant.updated") console.log(event.payload.expires_at); // string | null
125
+ * }
126
+ * ```
127
+ */
128
+ function isGrantEvent(event) {
129
+ return ANDCO_WEBHOOK_GRANT_EVENT_TYPES.includes(event.type);
130
+ }
131
+ /** Whether a lifecycle event's payload has the shape its type promises; other events pass. */
132
+ function hasLifecyclePayload(event) {
133
+ if (!isGrantEvent(event))
134
+ return true;
135
+ const payload = event.payload;
136
+ const named = typeof payload["grant_id"] === "string" &&
137
+ typeof payload["client_id"] === "string" &&
138
+ typeof payload["resource"] === "string";
139
+ if (!named)
140
+ return false;
141
+ if (event.type === "grant.revoked") {
142
+ return ((payload["reason"] === "revoked_by_person" || payload["reason"] === "revoked_by_client") &&
143
+ typeof payload["revoked_at"] === "string");
144
+ }
145
+ else if (event.type === "grant.expired") {
146
+ return typeof payload["expires_at"] === "string";
147
+ }
148
+ else {
149
+ return typeof payload["expires_at"] === "string" || payload["expires_at"] === null;
150
+ }
151
+ }
115
152
  /**
116
153
  * Creates a verifier for Andco webhook deliveries.
117
154
  *
@@ -135,6 +172,7 @@ function isEvent(value) {
135
172
  * export async function POST(request: Request) {
136
173
  * const event = await verify(request);
137
174
  * if (event.type === "deposit.completed") await markPaid(event.payload["intent_id"] as string);
175
+ * if (andcoWebhooks.isGrantEvent(event) && event.type !== "grant.updated") await forgetTokens(event.payload.grant_id);
138
176
  * return new Response(null, { status: 204 });
139
177
  * }
140
178
  * ```
@@ -237,13 +275,17 @@ function verifier(options = {}) {
237
275
  }
238
276
  if (!isEvent(event) || event.id !== eventId)
239
277
  throw new AndcoWebhookError(ANDCO_WEBHOOK_ERROR_CODES.INVALID_PAYLOAD);
278
+ if (!hasLifecyclePayload(event)) {
279
+ console.debug("[andcoWebhooks.verify] %s %s has a payload that does not match its type", event.type, eventId);
280
+ throw new AndcoWebhookError(ANDCO_WEBHOOK_ERROR_CODES.INVALID_PAYLOAD);
281
+ }
240
282
  return event;
241
283
  };
242
284
  }
243
285
  /**
244
- * Receiving Andco webhooks. See {@link verifier}.
286
+ * Receiving Andco webhooks. See {@link verifier} and {@link isGrantEvent}.
245
287
  *
246
288
  * @example
247
289
  * const verify = andcoWebhooks.verifier();
248
290
  */
249
- export const andcoWebhooks = { verifier };
291
+ export const andcoWebhooks = { verifier, isGrantEvent };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andco/protocol",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "andco": {
5
5
  "release": 1780358400
6
6
  },