Skip to content

Commit ec7abe3

Browse files
committed
feat: Add webhook API definitions
1 parent 7139176 commit ec7abe3

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

packages/api/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export * from './participants'
66
export * from './permissions'
77
export * from './schemas/encodings'
88
export * from './schemas/identity'
9+
export * from './server/webhook'
910
export * from './sharedKey'
1011
export * from './signup'
1112
export * from './utils/time'

packages/api/src/server/webhook.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { z } from 'zod'
2+
import { publicKeyAuthHeaders } from '../headers'
3+
import { postKeychainItemRequestBody } from '../keychain'
4+
import { identitySchema } from '../schemas/identity'
5+
import { postSharedKeyBody } from '../sharedKey'
6+
7+
export const webhookHeaders = publicKeyAuthHeaders.extend({
8+
'x-e2esdk-server-pubkey': identitySchema.shape.signaturePublicKey,
9+
'x-e2esdk-request-id': z.string(),
10+
origin: z.string().url(),
11+
})
12+
13+
export type WebhookHeaders = z.infer<typeof webhookHeaders>
14+
15+
// --
16+
17+
export const webhookRoutes = {
18+
// Authorizations
19+
'/authorize/signup': {
20+
body: identitySchema.pick({ userId: true }),
21+
},
22+
'/authorize/key-share': {
23+
body: postSharedKeyBody,
24+
},
25+
// Notifications
26+
'/notify/signup': {
27+
body: identitySchema,
28+
},
29+
'/notify/key-added': {
30+
body: postKeychainItemRequestBody.omit({
31+
name: true,
32+
payload: true,
33+
subkeyIndex: true,
34+
signature: true,
35+
}),
36+
},
37+
} as const
38+
39+
export type WebhookRoutes = {
40+
[Path in keyof typeof webhookRoutes]: {
41+
body: z.infer<typeof webhookRoutes[Path]['body']>
42+
}
43+
}

packages/server/src/plugins/webhook.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
Identity,
33
PostKeychainItemRequestBody,
44
PostSharedKeyBody,
5+
WebhookRoutes,
56
} from '@socialgouv/e2esdk-api'
67
import { signAuth } from '@socialgouv/e2esdk-crypto'
78
import type { FastifyPluginAsync, FastifyRequest } from 'fastify'
@@ -36,11 +37,13 @@ declare module 'fastify' {
3637

3738
// --
3839

39-
type WebhookAPICallArgs = {
40+
type WebhookPaths = keyof WebhookRoutes
41+
42+
type WebhookAPICallArgs<Path extends WebhookPaths> = {
4043
name: keyof Decoration
4144
req: FastifyRequest
42-
path: string
43-
body?: any
45+
path: Path
46+
body: WebhookRoutes[Path]['body']
4447
}
4548

4649
// --
@@ -63,12 +66,12 @@ const webhookPlugin: FastifyPluginAsync = async (app: App) => {
6366
}
6467
app.log.info({ msg: 'Setting up webhook', url: baseURL })
6568

66-
async function webhookApiCall({
69+
async function webhookApiCall<Path extends WebhookPaths>({
6770
req,
6871
path,
6972
name,
7073
body: payload,
71-
}: WebhookAPICallArgs) {
74+
}: WebhookAPICallArgs<Path>) {
7275
try {
7376
const method = 'POST'
7477
const url = baseURL + path
@@ -110,7 +113,7 @@ const webhookPlugin: FastifyPluginAsync = async (app: App) => {
110113
referrer,
111114
body,
112115
headers: {
113-
...(body ? { 'content-type': 'application/json' } : {}),
116+
'content-type': 'application/json',
114117
origin: env.DEPLOYMENT_URL,
115118
'user-agent': `${app.pkg.name}@${app.pkg.version} Webhook`,
116119
'x-e2esdk-user-id': userId,

0 commit comments

Comments
 (0)