Skip to content

Commit

Permalink
Authn/authz for POLIS convert route (#110)
Browse files Browse the repository at this point in the history
Co-authored-by: avenmia <mavend2@gmail.com>
  • Loading branch information
dinoboy197 and avenmia committed Apr 6, 2023
1 parent 16a27e0 commit 9cd1e37
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ touch .env
4. EMAIL_FROM={the email to send the verification link}
5. NEXT_PUBLIC_POLIS_SURVEYS='[{"id": "{yourSurveyID1}", "title": "{yourSurveyTitle1}", "description", "{yourSurveyDescription1}"}, ...]'
6. NEXT_PUBLIC_SEARCH_API='{your ArcGIS Search Api Key}'
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}

```
npx prisma db push
Expand Down
20 changes: 18 additions & 2 deletions src/pages/api/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import multiparty from "multiparty";
import ObjectsToCsv from "objects-to-csv";
import csv from "csv-parser";
import fs from "fs";
import { authOptions } from './../../server/auth'
import { getServerSession } from "next-auth/next"

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

Expand All @@ -15,9 +17,12 @@ export const config = {
},
};

export const authorizedEmails = fs.readFileSync(process.env.AUTHORIZED_POLIS_CONVERT_EMAILS_FILE, 'utf8').split(/\r?\n/);
console.log("Emails authorized to export POLIS data: " + authorizedEmails);

function handleError(error, res) {
console.error(error.stack);
res.status(500).end("Sorry, an error occured while processing a Pol.is export. The error has been logged for admistrators.d");
res.status(500).end("Sorry, an error occured while processing a Pol.is export. The error has been logged for admistrators.");
}

const handler = nc({
Expand All @@ -26,7 +31,18 @@ const handler = nc({
res.status(404).end("Page is not found");
},
}).post(async (req, res) => {
// TODO - add authentication / authorization so that only admins can access this, as it extracts census tract and zip code data for users

const sessionData = await getServerSession(req, res, authOptions);

if (!sessionData) {
res.status(401).end("Not authenticated; please log in on homepage.");
return;
}
const email = sessionData.user.email;
if (!authorizedEmails.includes(email)) {
res.status(403).end(email + ", you are not authorized to export Pol.is data.");
return;
}

const form = new multiparty.Form();

Expand Down
7 changes: 7 additions & 0 deletions src/pages/polisconvert.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
// 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
import { type NextPage } from "next";

import { useSession } from "next-auth/react";

const PolisConvert: NextPage = () => {

const { data: sessionData } = useSession();

return (
<div>
<h2>Polis Data Conversion</h2>
{sessionData ? "You are signed in" : "You are not signed in"}<br />
Please select a participant-votes.csv file to upload, then click submit.<br />
<form method="post" action="/api/export" encType="multipart/form-data">
<input type="file" id="polisdata" name="polisdata" />
<input type="submit" />
Expand Down

1 comment on commit 9cd1e37

@vercel
Copy link

@vercel vercel bot commented on 9cd1e37 Apr 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

hierr – ./

hierr-git-main-codeforhawaii.vercel.app
hierr-codeforhawaii.vercel.app
hierr.vercel.app

Please sign in to comment.