protect request

This commit is contained in:
tdurieux
2021-04-20 17:55:02 +02:00
parent 39d1d387d7
commit 67311ae535
+13 -6
View File
@@ -52,6 +52,7 @@ router.post("/:repoId/", async (req, res) => {
return res.status(500).send({ error: "invalid_terms_format" }); return res.status(500).send({ error: "invalid_terms_format" });
} }
try {
const details = await repoUtils.getRepoDetails({ const details = await repoUtils.getRepoDetails({
repoConfig, repoConfig,
force: true, force: true,
@@ -64,7 +65,6 @@ router.post("/:repoId/", async (req, res) => {
} }
} }
try {
if (repoUpdate.commit != repoConfig.commit) { if (repoUpdate.commit != repoConfig.commit) {
repoUpdate.anonymizeDate = new Date(); repoUpdate.anonymizeDate = new Date();
await repoUtils.removeRepository(repoConfig); await repoUtils.removeRepository(repoConfig);
@@ -127,6 +127,7 @@ router.post("/:repoId/", async (req, res) => {
// refresh a repository // refresh a repository
router.post("/:repoId/refresh", async (req, res) => { router.post("/:repoId/refresh", async (req, res) => {
try {
const repoConfig = await repoUtils.getConfig(req.params.repoId); const repoConfig = await repoUtils.getConfig(req.params.repoId);
if (repoConfig == null) { if (repoConfig == null) {
return res.status(500).json({ error: "repo_not_found" }); return res.status(500).json({ error: "repo_not_found" });
@@ -134,7 +135,7 @@ router.post("/:repoId/refresh", async (req, res) => {
if (repoConfig.owner != req.user.username) { if (repoConfig.owner != req.user.username) {
return res.status(401).json({ error: "not_authorized" }); return res.status(401).json({ error: "not_authorized" });
} }
try {
await repoUtils.updateAnonymizedRepository(repoConfig); await repoUtils.updateAnonymizedRepository(repoConfig);
return res.send("ok"); return res.send("ok");
} catch (error) { } catch (error) {
@@ -144,6 +145,7 @@ router.post("/:repoId/refresh", async (req, res) => {
// delete a repository // delete a repository
router.delete("/:repoId/", async (req, res) => { router.delete("/:repoId/", async (req, res) => {
try {
const repoConfig = await repoUtils.getConfig(req.params.repoId); const repoConfig = await repoUtils.getConfig(req.params.repoId);
if (repoConfig == null) { if (repoConfig == null) {
return res.status(500).json({ error: "repo_not_found" }); return res.status(500).json({ error: "repo_not_found" });
@@ -151,7 +153,7 @@ router.delete("/:repoId/", async (req, res) => {
if (repoConfig.owner != req.user.username) { if (repoConfig.owner != req.user.username) {
return res.status(401).json({ error: "not_authorized" }); return res.status(401).json({ error: "not_authorized" });
} }
try {
await repoUtils.updateStatus(repoConfig, "removed"); await repoUtils.updateStatus(repoConfig, "removed");
await repoUtils.removeRepository(repoConfig); await repoUtils.removeRepository(repoConfig);
console.log(`${req.params.repoId} is removed`); console.log(`${req.params.repoId} is removed`);
@@ -163,6 +165,7 @@ router.delete("/:repoId/", async (req, res) => {
// claim a repository // claim a repository
router.post("/claim", async (req, res) => { router.post("/claim", async (req, res) => {
try {
if (!req.body.repoId) { if (!req.body.repoId) {
return res.status(500).json({ error: "repoId_not_defined" }); return res.status(500).json({ error: "repoId_not_defined" });
} }
@@ -181,7 +184,7 @@ router.post("/claim", async (req, res) => {
} }
console.log(`${req.user.username} claims ${repoConfig.fullName}.`); console.log(`${req.user.username} claims ${repoConfig.fullName}.`);
try {
await db await db
.get("anonymized_repositories") .get("anonymized_repositories")
.updateOne( .updateOne(
@@ -248,6 +251,7 @@ router.get("/:owner/:repo/readme", async (req, res) => {
router.post("/", async (req, res) => { router.post("/", async (req, res) => {
const repoConfig = req.body; const repoConfig = req.body;
try {
const repository = await repoUtils.getConfig(repoConfig.repoId); const repository = await repoUtils.getConfig(repoConfig.repoId);
const cacheExist = ofs.existsSync( const cacheExist = ofs.existsSync(
repoUtils.getOriginalPath(repoConfig.repoId) repoUtils.getOriginalPath(repoConfig.repoId)
@@ -272,7 +276,10 @@ router.post("/", async (req, res) => {
return res.status(500).send({ error: "invalid_terms_format" }); return res.status(500).send({ error: "invalid_terms_format" });
} }
await repoUtils.getRepoBranches({ repoConfig, token: req.user.accessToken }); await repoUtils.getRepoBranches({
repoConfig,
token: req.user.accessToken,
});
const details = await repoUtils.getRepoDetails({ const details = await repoUtils.getRepoDetails({
repoConfig, repoConfig,
token: req.user.accessToken, token: req.user.accessToken,
@@ -326,7 +333,7 @@ router.post("/", async (req, res) => {
{ upsert: true } { upsert: true }
); );
res.send("ok"); res.send("ok");
try {
await githubUtils.downloadRepoAndAnonymize(data); await githubUtils.downloadRepoAndAnonymize(data);
await repoUtils.updateStatus(repoConfig, "ready"); await repoUtils.updateStatus(repoConfig, "ready");
} catch (error) { } catch (error) {