|
| 1 | +import { |
| 2 | + Identity, |
| 3 | + PostKeychainItemRequestBody, |
| 4 | + PostSharedKeyBody, |
| 5 | +} from '@socialgouv/e2esdk-api' |
| 6 | +import { signAuth } from '@socialgouv/e2esdk-crypto' |
| 7 | +import type { FastifyPluginAsync, FastifyRequest } from 'fastify' |
| 8 | +import fp from 'fastify-plugin' |
| 9 | +import { env } from 'process' |
| 10 | +import type { App } from '../types' |
| 11 | + |
| 12 | +type Decoration = { |
| 13 | + // Authorization |
| 14 | + authorizeSignup(req: FastifyRequest): Promise<boolean> |
| 15 | + authorizeKeyShare( |
| 16 | + req: FastifyRequest, |
| 17 | + sharedKey: PostSharedKeyBody |
| 18 | + ): Promise<boolean> |
| 19 | + |
| 20 | + // Notifications |
| 21 | + notifySignup(req: FastifyRequest, identity: Identity): void |
| 22 | + notifyKeyAdded( |
| 23 | + req: FastifyRequest, |
| 24 | + item: Omit< |
| 25 | + PostKeychainItemRequestBody, |
| 26 | + 'name' | 'payload' | 'subkeyIndex' | 'signature' |
| 27 | + > |
| 28 | + ): void |
| 29 | +} |
| 30 | + |
| 31 | +declare module 'fastify' { |
| 32 | + interface FastifyInstance { |
| 33 | + webhook: Decoration |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// -- |
| 38 | + |
| 39 | +type WebhookAPICallArgs = { |
| 40 | + name: keyof Decoration |
| 41 | + req: FastifyRequest |
| 42 | + path: string |
| 43 | + body?: any |
| 44 | +} |
| 45 | + |
| 46 | +// -- |
| 47 | + |
| 48 | +const webhookPlugin: FastifyPluginAsync = async (app: App) => { |
| 49 | + const baseURL = env.WEBHOOK_URL |
| 50 | + if (!baseURL) { |
| 51 | + const decoration: Decoration = { |
| 52 | + authorizeSignup() { |
| 53 | + return Promise.resolve(true) |
| 54 | + }, |
| 55 | + authorizeKeyShare() { |
| 56 | + return Promise.resolve(true) |
| 57 | + }, |
| 58 | + notifySignup() {}, |
| 59 | + notifyKeyAdded() {}, |
| 60 | + } |
| 61 | + app.decorate('webhook', decoration) |
| 62 | + return |
| 63 | + } |
| 64 | + app.log.info({ msg: 'Setting up webhook', url: baseURL }) |
| 65 | + |
| 66 | + async function webhookApiCall({ |
| 67 | + req, |
| 68 | + path, |
| 69 | + name, |
| 70 | + body: payload, |
| 71 | + }: WebhookAPICallArgs) { |
| 72 | + try { |
| 73 | + const method = 'POST' |
| 74 | + const url = baseURL + path |
| 75 | + const timestamp = new Date().toISOString() |
| 76 | + const userId = encodeURIComponent(req.identity.userId) |
| 77 | + const body = payload ? JSON.stringify(payload) : undefined |
| 78 | + const referrer = |
| 79 | + typeof req.headers.referrer === 'string' |
| 80 | + ? req.headers.referrer |
| 81 | + : undefined |
| 82 | + const signature = signAuth( |
| 83 | + app.sodium, |
| 84 | + app.sodium.from_base64(env.SIGNATURE_PRIVATE_KEY), |
| 85 | + { |
| 86 | + clientId: req.clientId, |
| 87 | + method, |
| 88 | + userId, |
| 89 | + url, |
| 90 | + timestamp, |
| 91 | + body, |
| 92 | + recipientPublicKey: 'none', |
| 93 | + } |
| 94 | + ) |
| 95 | + req.auditLog.trace({ |
| 96 | + msg: `webhook:${name}:fetch:request`, |
| 97 | + method, |
| 98 | + url, |
| 99 | + referrer, |
| 100 | + timestamp, |
| 101 | + signature, |
| 102 | + body, |
| 103 | + }) |
| 104 | + const response = await fetch(url, { |
| 105 | + method, |
| 106 | + mode: 'no-cors', |
| 107 | + cache: 'no-store', |
| 108 | + credentials: 'omit', |
| 109 | + redirect: 'error', |
| 110 | + referrer, |
| 111 | + body, |
| 112 | + headers: { |
| 113 | + ...(body ? { 'content-type': 'application/json' } : {}), |
| 114 | + origin: env.DEPLOYMENT_URL, |
| 115 | + 'user-agent': `${app.pkg.name}@${app.pkg.version} Webhook`, |
| 116 | + 'x-e2esdk-user-id': userId, |
| 117 | + 'x-e2esdk-request-id': req.id, |
| 118 | + 'x-e2esdk-client-id': req.clientId, |
| 119 | + 'x-e2esdk-timestamp': timestamp, |
| 120 | + 'x-e2esdk-signature': signature, |
| 121 | + 'x-e2esdk-server-pubkey': env.SIGNATURE_PUBLIC_KEY, |
| 122 | + }, |
| 123 | + }) |
| 124 | + req.auditLog.trace({ |
| 125 | + msg: `webhook:${name}:fetch:response`, |
| 126 | + status: response.status, |
| 127 | + }) |
| 128 | + return response |
| 129 | + } catch (error) { |
| 130 | + req.auditLog.trace({ |
| 131 | + msg: `webhook:${name}:error`, |
| 132 | + error, |
| 133 | + }) |
| 134 | + return null |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + const decoration: Decoration = { |
| 139 | + async authorizeSignup(req) { |
| 140 | + const userId = encodeURIComponent(req.identity.userId) |
| 141 | + const response = await webhookApiCall({ |
| 142 | + req, |
| 143 | + name: 'authorizeSignup', |
| 144 | + path: '/authorize/signup', |
| 145 | + body: { |
| 146 | + userId, |
| 147 | + }, |
| 148 | + }) |
| 149 | + return response?.status === 200 |
| 150 | + }, |
| 151 | + async authorizeKeyShare(req, sharedKey) { |
| 152 | + const response = await webhookApiCall({ |
| 153 | + req, |
| 154 | + name: 'authorizeKeyShare', |
| 155 | + path: '/authorize/key-share', |
| 156 | + body: sharedKey, |
| 157 | + }) |
| 158 | + return response?.status === 200 |
| 159 | + }, |
| 160 | + notifySignup(req, identity) { |
| 161 | + webhookApiCall({ |
| 162 | + req, |
| 163 | + name: 'notifySignup', |
| 164 | + path: '/notify/signup', |
| 165 | + body: identity, |
| 166 | + }) |
| 167 | + }, |
| 168 | + notifyKeyAdded(req, item) { |
| 169 | + webhookApiCall({ |
| 170 | + req, |
| 171 | + name: 'notifyKeyAdded', |
| 172 | + path: '/notify/key-added', |
| 173 | + body: item, |
| 174 | + }) |
| 175 | + }, |
| 176 | + } |
| 177 | + app.decorate('webhook', decoration) |
| 178 | +} |
| 179 | + |
| 180 | +export default fp(webhookPlugin, { |
| 181 | + fastify: '4.x', |
| 182 | + name: 'webhook', |
| 183 | + dependencies: ['pkg'], |
| 184 | + decorators: { |
| 185 | + fastify: ['pkg'], |
| 186 | + }, |
| 187 | +}) |
0 commit comments