Skip to content

Commit

Permalink
Merge pull request #1238 from assemblee-virtuelle/fix-mastodon-compat…
Browse files Browse the repository at this point in the history
…ibility

Fix Mastodon compatibility
  • Loading branch information
srosset81 authored Mar 1, 2024
2 parents 6a0c1b7 + 8f61a9a commit 9780af4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ const InboxService = {
}

if (!ctx.meta.skipSignatureValidation) {
if (!ctx.meta.rawBody || !ctx.meta.headers)
throw new Error(`Cannot validate HTTP signature because of missing meta (rawBody or headers)`);
if (!ctx.meta.rawBody || !ctx.meta.originalHeaders)
throw new Error(`Cannot validate HTTP signature because of missing meta (rawBody or originalHeaders)`);

const validDigest = await ctx.call('signature.verifyDigest', {
body: ctx.meta.rawBody, // Stored by parseJson middleware
headers: ctx.meta.headers
headers: ctx.meta.originalHeaders
});

const { isValid: validSignature } = await ctx.call('signature.verifyHttpSignature', {
url: collectionUri,
method: 'POST',
headers: ctx.meta.headers
headers: ctx.meta.originalHeaders
});

if (!validDigest || !validSignature) {
Expand Down
9 changes: 7 additions & 2 deletions src/middleware/packages/ldp/services/api/actions/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ module.exports = async function get(ctx) {
jsonContext: parseJson(ctx.meta.headers?.jsonldcontext)
})
);

ctx.meta.$responseType = ctx.meta.$responseType || accept;
if (ctx.meta.$responseType === 'application/ld+json')
ctx.meta.$responseType = `application/ld+json; profile="https://www.w3.org/ns/activitystreams"`;
} else if (types.includes('https://www.w3.org/ns/activitystreams#Collection')) {
/*
* AS COLLECTION
Expand All @@ -45,7 +48,7 @@ module.exports = async function get(ctx) {
jsonContext: parseJson(ctx.meta.headers?.jsonldcontext)
})
);
ctx.meta.$responseType = 'application/ld+json';
ctx.meta.$responseType = `application/ld+json; profile="https://www.w3.org/ns/activitystreams"`;
} else {
/*
* LDP RESOURCE
Expand All @@ -55,7 +58,7 @@ module.exports = async function get(ctx) {
...ctx.meta.headers
};

if (ctx.meta.accepts && ctx.meta.accepts.includes('text/html') && this.settings.preferredViewForResource) {
if (ctx.meta.originalHeaders?.accept?.includes('text/html') && this.settings.preferredViewForResource) {
const webId = ctx.meta.webId || 'anon';
const resourceExist = await ctx.call('ldp.resource.exist', { resourceUri: uri, webId });
if (resourceExist) {
Expand Down Expand Up @@ -95,6 +98,8 @@ module.exports = async function get(ctx) {
}
} else {
ctx.meta.$responseType = ctx.meta.$responseType || accept;
if (ctx.meta.$responseType === 'application/ld+json')
ctx.meta.$responseType = `application/ld+json; profile="https://www.w3.org/ns/activitystreams"`;
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/middleware/packages/middlewares/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const Busboy = require('busboy');
const streams = require('memory-streams');

const parseHeader = async (req, res, next) => {
req.$ctx.meta.headers = req.headers || {};
req.$ctx.meta.headers = req.headers ? { ...req.headers } : {};
// Also remember original headers (needed for HTTP signatures verification and files type negociation)
req.$ctx.meta.originalHeaders = req.headers ? { ...req.headers } : {};
next();
};

Expand Down Expand Up @@ -37,8 +39,6 @@ const throw500 = msg => {

const negotiateAccept = (req, res, next) => {
if (!req.$ctx.meta.headers) req.$ctx.meta.headers = {};
// we keep the full list for further use
req.$ctx.meta.accepts = req.headers.accept;
if (req.headers.accept === '*/*') {
delete req.headers.accept;
}
Expand Down

0 comments on commit 9780af4

Please sign in to comment.