Skip to content

Commit 9cd1e37

Browse files
dinoboy197avenmia
andauthored
Authn/authz for POLIS convert route (#110)
Co-authored-by: avenmia <[email protected]>
1 parent 16a27e0 commit 9cd1e37

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

src/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ touch .env
1414
4. EMAIL_FROM={the email to send the verification link}
1515
5. NEXT_PUBLIC_POLIS_SURVEYS='[{"id": "{yourSurveyID1}", "title": "{yourSurveyTitle1}", "description", "{yourSurveyDescription1}"}, ...]'
1616
6. NEXT_PUBLIC_SEARCH_API='{your ArcGIS Search Api Key}'
17+
7. AUTHORIZED_POLIS_CONVERT_EMAILS_FILE={path to file that contains a list of email addresses (one per line) whose users are authorized to export POLIS data}
1718

1819
```
1920
npx prisma db push

src/pages/api/export.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import multiparty from "multiparty";
55
import ObjectsToCsv from "objects-to-csv";
66
import csv from "csv-parser";
77
import fs from "fs";
8+
import { authOptions } from './../../server/auth'
9+
import { getServerSession } from "next-auth/next"
810

911
import { prisma } from "../../server/db";
1012

@@ -15,9 +17,12 @@ export const config = {
1517
},
1618
};
1719

20+
export const authorizedEmails = fs.readFileSync(process.env.AUTHORIZED_POLIS_CONVERT_EMAILS_FILE, 'utf8').split(/\r?\n/);
21+
console.log("Emails authorized to export POLIS data: " + authorizedEmails);
22+
1823
function handleError(error, res) {
1924
console.error(error.stack);
20-
res.status(500).end("Sorry, an error occured while processing a Pol.is export. The error has been logged for admistrators.d");
25+
res.status(500).end("Sorry, an error occured while processing a Pol.is export. The error has been logged for admistrators.");
2126
}
2227

2328
const handler = nc({
@@ -26,7 +31,18 @@ const handler = nc({
2631
res.status(404).end("Page is not found");
2732
},
2833
}).post(async (req, res) => {
29-
// TODO - add authentication / authorization so that only admins can access this, as it extracts census tract and zip code data for users
34+
35+
const sessionData = await getServerSession(req, res, authOptions);
36+
37+
if (!sessionData) {
38+
res.status(401).end("Not authenticated; please log in on homepage.");
39+
return;
40+
}
41+
const email = sessionData.user.email;
42+
if (!authorizedEmails.includes(email)) {
43+
res.status(403).end(email + ", you are not authorized to export Pol.is data.");
44+
return;
45+
}
3046

3147
const form = new multiparty.Form();
3248

src/pages/polisconvert.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
// page for serving form for accepting raw Pol.is participant votes data and returning the data augmented with user zip code and census tract data
22
import { type NextPage } from "next";
33

4+
import { useSession } from "next-auth/react";
5+
46
const PolisConvert: NextPage = () => {
7+
8+
const { data: sessionData } = useSession();
9+
510
return (
611
<div>
712
<h2>Polis Data Conversion</h2>
13+
{sessionData ? "You are signed in" : "You are not signed in"}<br />
14+
Please select a participant-votes.csv file to upload, then click submit.<br />
815
<form method="post" action="/api/export" encType="multipart/form-data">
916
<input type="file" id="polisdata" name="polisdata" />
1017
<input type="submit" />

0 commit comments

Comments
 (0)