@@ -17,6 +17,7 @@ import { ParsedQs } from "qs";
17
17
import { env } from "process" ;
18
18
import bodyParser = require( "body-parser" ) ;
19
19
import { type } from "os" ;
20
+ const axios = require ( 'axios' )
20
21
21
22
const crypto_algorithm = "aes-192-cbc" ;
22
23
//PLACEHOLDER VALUES FOR CRYPTO. DO NOT USE FOR PRODUCTION. Replace "researcherpassword" with researcher"s password.
@@ -131,6 +132,14 @@ app.get("/s/", async (req, res) => {
131
132
getsurvey ( req . query , req , res ) ;
132
133
} ) ;
133
134
135
+ app . get ( "/se/:encrypted" , async ( req , res ) => {
136
+ // encryption handled by python backend services at endpoint in internal docs (would)
137
+ let result = await Db_Wrapper . find ( { 'alias' : req . params . encrypted } , "survey_links" )
138
+ result = result [ 0 ]
139
+ const parsed = { "url" : result . SurveyUrl , "WorkerId" : result . WorkerId }
140
+ getsurvey ( parsed , req , res )
141
+ } ) ;
142
+
134
143
app . get ( "/e/:data" , async ( req , res ) => {
135
144
//in the future, private_key and iv will be obtained through researcher database
136
145
try {
@@ -179,8 +188,33 @@ app.get("/results", async (req, res) => {
179
188
180
189
// This needs to be encrypted to only give results to someone who is authenticated to read them
181
190
app . get ( "/results/json" , async ( req , res ) => {
182
- await Db_Wrapper . find ( { } , "responses" )
183
- . then ( all_responses => { res . send ( all_responses ) } ) ;
191
+ let rID = req . header ( 'rID' ) ;
192
+ let clientKey = req . header ( 'clientKey' ) ;
193
+
194
+ clientKey == null ? 'default' : clientKey ; //DO NOT USE IN PRODUCTION DELETE THIS LINE
195
+
196
+ /*
197
+ * Model: rID leads to researcher database in the future
198
+ * Researcher database outline:
199
+ * rID --> researcherID that points to the specific researcher
200
+ * clientKey --> client key that the researcher uses to access data
201
+ * privateKey --> server key that we use to verify the clientKey
202
+ */
203
+
204
+ try {
205
+ const decipher = crypto . createDecipheriv ( crypto_algorithm , private_key_example , iv_example ) ;
206
+ let decrypted = decipher . update ( clientKey , "hex" , "utf8" ) ;
207
+ decrypted += decipher . final ( "utf8" ) ;
208
+ if ( decrypted === rID || clientKey == 'default' ) {
209
+ await Db_Wrapper . find ( { } , "responses" )
210
+ . then ( all_responses => { res . send ( all_responses ) } ) ;
211
+ } else {
212
+ throw new Error ( 'ID + Key incorrect' ) ;
213
+ }
214
+ } catch ( error ) {
215
+ console . error ( error ) ;
216
+ res . redirect ( "/" ) ;
217
+ }
184
218
} ) ;
185
219
186
220
/* THIS NEEDS TO BE AUTHENTICATED TO ADMIN USER
0 commit comments