Prepping /document.info for public docs
This commit is contained in:
@@ -30,26 +30,27 @@ export default function auth({ require = true } = {}) {
|
|||||||
throw httpErrors.Unauthorized('Authentication required');
|
throw httpErrors.Unauthorized('Authentication required');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get user without verifying payload signature
|
if (token && require) {
|
||||||
let payload;
|
// Get user without verifying payload signature
|
||||||
try {
|
let payload;
|
||||||
payload = JWT.decode(token);
|
try {
|
||||||
} catch(_e) {
|
payload = JWT.decode(token);
|
||||||
throw httpErrors.Unauthorized('Unable to decode JWT token');
|
} catch(_e) {
|
||||||
}
|
throw httpErrors.Unauthorized('Unable to decode JWT token');
|
||||||
console.log(payload)
|
}
|
||||||
const user = await User.findOne({
|
const user = await User.findOne({
|
||||||
where: { id: payload.id },
|
where: { id: payload.id },
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
JWT.verify(token, user.jwtSecret);
|
JWT.verify(token, user.jwtSecret);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
throw httpErrors.Unauthorized('Invalid token');
|
throw httpErrors.Unauthorized('Invalid token');
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.state.token = token;
|
ctx.state.token = token;
|
||||||
ctx.state.user = user;
|
ctx.state.user = user;
|
||||||
|
}
|
||||||
|
|
||||||
return next();
|
return next();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -8,23 +8,35 @@ import { Document, Atlas } from '../models';
|
|||||||
|
|
||||||
const router = new Router();
|
const router = new Router();
|
||||||
|
|
||||||
router.post('documents.info', auth(), async (ctx) => {
|
router.post('documents.info', auth({ require: false }), async (ctx) => {
|
||||||
let { id } = ctx.request.body;
|
let { id } = ctx.request.body;
|
||||||
ctx.assertPresent(id, 'id is required');
|
ctx.assertPresent(id, 'id is required');
|
||||||
|
|
||||||
const team = await ctx.state.user.getTeam();
|
|
||||||
const document = await Document.findOne({
|
const document = await Document.findOne({
|
||||||
where: {
|
where: {
|
||||||
id: id,
|
id: id,
|
||||||
teamId: team.id,
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!document) throw httpErrors.NotFound();
|
// Don't expose private documents outside the team
|
||||||
|
if (document.private) {
|
||||||
|
if (!ctx.state.user) throw httpErrors.NotFound();
|
||||||
|
|
||||||
ctx.body = {
|
const team = await ctx.state.user.getTeam();
|
||||||
data: await presentDocument(document, true),
|
if (document.teamId !== team.id) {
|
||||||
};
|
if (!document) throw httpErrors.NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.body = {
|
||||||
|
data: await presentDocument(document, true),
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
ctx.body = {
|
||||||
|
data: await presentDocument(document),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!document) throw httpErrors.NotFound();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,13 +3,13 @@ import Document from './models/Document';
|
|||||||
|
|
||||||
export function presentUser(user) {
|
export function presentUser(user) {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
resolve({
|
const data = {
|
||||||
id: user.id,
|
id: user.id,
|
||||||
name: user.name,
|
name: user.name,
|
||||||
username: user.username,
|
username: user.username,
|
||||||
email: user.email,
|
|
||||||
avatarUrl: user.slackData.image_192,
|
avatarUrl: user.slackData.image_192,
|
||||||
});
|
};
|
||||||
|
resolve(data);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,6 +62,7 @@ export async function presentDocument(document, includeAtlas=false) {
|
|||||||
text: document.text,
|
text: document.text,
|
||||||
html: document.html,
|
html: document.html,
|
||||||
preview: document.preview,
|
preview: document.preview,
|
||||||
|
private: document.private,
|
||||||
createdAt: document.createdAt,
|
createdAt: document.createdAt,
|
||||||
updatedAt: document.updatedAt,
|
updatedAt: document.updatedAt,
|
||||||
atlas: document.atlaId,
|
atlas: document.atlaId,
|
||||||
@@ -71,10 +72,10 @@ export async function presentDocument(document, includeAtlas=false) {
|
|||||||
if (includeAtlas) {
|
if (includeAtlas) {
|
||||||
const atlas = await document.getAtlas();
|
const atlas = await document.getAtlas();
|
||||||
data.atlas = await presentAtlas(atlas, false);
|
data.atlas = await presentAtlas(atlas, false);
|
||||||
|
|
||||||
const user = await document.getUser();
|
|
||||||
data.user = await presentUser(user, false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const user = await document.getUser();
|
||||||
|
data.user = await presentUser(user);
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user