-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathinbox.js
More file actions
247 lines (213 loc) · 6.51 KB
/
Copy pathinbox.js
File metadata and controls
247 lines (213 loc) · 6.51 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/**
* Inbox endpoint handler
* Receives and processes incoming ActivityPub activities
*/
import { auth, outbox } from 'microfed'
import {
saveActivity,
addFollower,
removeFollower,
acceptFollowing,
cacheActor,
getCachedActor
} from '../store.js'
import { getKeyId } from '../keys.js'
/**
* Fetch remote actor (with caching)
* @param {string} id - Actor URL
* @param {object} log - Logger instance (optional)
* @returns {Promise<object|null>} Actor object or null
*/
async function fetchActor(id, log) {
// Strip fragment for fetching
const fetchUrl = id.replace(/#.*$/, '')
const cached = getCachedActor(id)
if (cached) return cached
try {
const response = await fetch(fetchUrl, {
headers: {
'Accept': 'application/activity+json',
'User-Agent': 'JSS/1.0 (+https://github.com/JavaScriptSolidServer/JavaScriptSolidServer)'
}
})
if (!response.ok) {
if (log) log.warn(`Actor fetch failed: ${response.status} ${response.statusText} for ${fetchUrl}`)
return null
}
const actor = await response.json()
cacheActor(actor)
return actor
} catch (err) {
if (log) log.error(`Actor fetch error for ${fetchUrl}: ${err.message}`)
return null
}
}
/**
* Verify HTTP signature on incoming request
* @param {object} request - Fastify request
* @param {string} body - Request body
* @returns {Promise<{valid: boolean, actor?: object, reason?: string}>}
*/
async function verifySignature(request, body) {
const signature = request.headers['signature']
if (!signature) {
return { valid: false, reason: 'No signature header' }
}
// Parse signature header
const sigParts = auth.parseSignatureHeader(signature)
if (!sigParts) {
return { valid: false, reason: 'Invalid signature format' }
}
const keyId = sigParts.keyId
if (!keyId) {
return { valid: false, reason: 'No keyId in signature' }
}
// Extract actor URL from keyId (strip fragment like #main-key)
const actorUrl = keyId.replace(/#.*$/, '')
// Fetch the actor to get their public key
const remoteActor = await fetchActor(actorUrl, request.log)
if (!remoteActor) {
return { valid: false, reason: `Could not fetch actor: ${actorUrl}` }
}
const publicKeyPem = remoteActor.publicKey?.publicKeyPem
if (!publicKeyPem) {
return { valid: false, reason: 'Actor has no public key' }
}
// Verify digest if present
const digestHeader = request.headers['digest']
if (digestHeader && !auth.verifyDigest(body, digestHeader)) {
return { valid: false, reason: 'Digest mismatch' }
}
// Build path from URL
const url = new URL(request.url, `http://${request.hostname}`)
// Verify signature
try {
const valid = auth.verify({
publicKey: publicKeyPem,
signature,
method: request.method,
path: url.pathname,
headers: request.headers
})
return { valid, actor: remoteActor }
} catch (err) {
return { valid: false, reason: `Verification error: ${err.message}` }
}
}
/**
* Create inbox handler
* @param {object} config - AP configuration
* @param {object} keypair - RSA keypair
* @returns {Function} Fastify handler
*/
export function createInboxHandler(config, keypair) {
return async (request, reply) => {
// Parse body
let activity
let body
try {
body = typeof request.body === 'string'
? request.body
: request.body.toString()
activity = JSON.parse(body)
} catch {
return reply.code(400).send({ error: 'Invalid JSON' })
}
// Verify signature (log but don't reject for now - many servers have issues)
const sigResult = await verifySignature(request, body)
if (!sigResult.valid) {
request.log.warn(`Signature verification failed: ${sigResult.reason}`)
} else {
request.log.info(`Signature verified for ${activity.actor}`)
}
// Validate activity
if (!activity.type) {
return reply.code(400).send({ error: 'Missing activity type' })
}
// Save activity
if (activity.id) {
saveActivity(activity)
}
// Handle activity by type
const protocol = request.headers['x-forwarded-proto'] || request.protocol
const host = request.headers['x-forwarded-host'] || request.hostname
const baseUrl = `${protocol}://${host}`
const profileUrl = `${baseUrl}/profile/card.jsonld`
const actorId = `${profileUrl}#me`
request.log.info(`Received ${activity.type} from ${activity.actor}`)
switch (activity.type) {
case 'Follow':
await handleFollow(activity, actorId, profileUrl, keypair, request.log)
break
case 'Undo':
await handleUndo(activity, request.log)
break
case 'Accept':
handleAccept(activity, request.log)
break
case 'Create':
request.log.info(`New post: ${activity.object?.content?.slice(0, 50)}...`)
break
case 'Like':
request.log.info(`Liked: ${activity.object}`)
break
case 'Announce':
request.log.info(`Boosted: ${activity.object}`)
break
default:
request.log.info(`Unhandled activity type: ${activity.type}`)
}
// Accept the activity
return reply.code(202).send()
}
}
/**
* Handle Follow activity
*/
async function handleFollow(activity, actorId, profileUrl, keypair, log) {
const followerActor = await fetchActor(activity.actor, log)
if (!followerActor) {
log.warn('Could not fetch follower actor')
return
}
// Add to followers
addFollower(activity.actor, followerActor.inbox)
log.info(`New follower: ${followerActor.preferredUsername || activity.actor}`)
// Send Accept
const accept = outbox.createAccept(actorId, activity)
try {
await outbox.send({
activity: accept,
inbox: followerActor.inbox,
privateKey: keypair.privateKey,
keyId: `${profileUrl}#main-key`
})
log.info(`Sent Accept to ${followerActor.inbox}`)
} catch (err) {
log.error(`Failed to send Accept: ${err.message}`)
}
}
/**
* Handle Undo activity
*/
async function handleUndo(activity, log) {
if (activity.object?.type === 'Follow') {
removeFollower(activity.actor)
log.info(`Unfollowed by ${activity.actor}`)
}
}
/**
* Handle Accept activity (our follow was accepted)
*/
function handleAccept(activity, log) {
if (activity.object?.type === 'Follow') {
const target = typeof activity.object.object === 'string'
? activity.object.object
: activity.object.object?.id
if (target) {
acceptFollowing(target)
log.info('Follow accepted!')
}
}
}
export default { createInboxHandler }