Add authentication based on study IDs
This commit is contained in:
parent
4c0255bb25
commit
bf7d9c2c1c
12
src/auth.js
12
src/auth.js
@ -5,23 +5,15 @@ export async function authSubject(request, env, ctx) {
|
|||||||
const headers = request.headers;
|
const headers = request.headers;
|
||||||
const studyId = headers.get('x-study-id');
|
const studyId = headers.get('x-study-id');
|
||||||
const subjectId = headers.get('x-subject-id');
|
const subjectId = headers.get('x-subject-id');
|
||||||
const prolificApiKey = env.PROLIFIC_API_KEY;
|
|
||||||
|
|
||||||
if (studyId === env.TEST_STUDY_ID && subjectId === env.TEST_SUBJECT_ID)
|
if (studyId === env.TEST_STUDY_ID && subjectId === env.TEST_SUBJECT_ID)
|
||||||
return true;
|
return true;
|
||||||
if (!subjectId.match(/^[a-f0-9]{1,32}$/) || !studyId.match(/^[a-f0-9]{1,32}$/))
|
if (!subjectId.match(/^[a-f0-9]{1,32}$/) || !studyId.match(/^[a-f0-9]{1,32}$/))
|
||||||
return false;
|
return false;
|
||||||
|
if (!(await env.EXP_SETTINGS.get('studyId', 'json')).includes(studyId))
|
||||||
|
return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
// try {
|
|
||||||
// const apiUrl = `https://api.prolific.com/api/v1/studies/${studyId}/submissions/`;
|
|
||||||
// const res = await fetch(apiUrl, {
|
|
||||||
// headers: { Authorization: `Token ${prolificApiKey}` }
|
|
||||||
// });
|
|
||||||
// let data = await res.json();
|
|
||||||
// return Array.from(data.results).some(p => p.participant_id === subjectId);
|
|
||||||
|
|
||||||
// } catch (e) { console.log(e); return false; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function authDispatcher(request, env, ctx) {
|
export async function authDispatcher(request, env, ctx) {
|
||||||
|
@ -70,9 +70,9 @@ TEST_SUBJECT_ID = "test"
|
|||||||
|
|
||||||
# Bind a KV Namespace. Use KV as persistent storage for small key-value pairs.
|
# Bind a KV Namespace. Use KV as persistent storage for small key-value pairs.
|
||||||
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#kv-namespaces
|
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#kv-namespaces
|
||||||
# [[kv_namespaces]]
|
[[kv_namespaces]]
|
||||||
# binding = "MY_KV_NAMESPACE"
|
binding = "EXP_SETTINGS"
|
||||||
# id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
id = "c36ae6bde4234f1399409866f799df49"
|
||||||
|
|
||||||
# Bind an mTLS certificate. Use to present a client certificate when communicating with another service.
|
# Bind an mTLS certificate. Use to present a client certificate when communicating with another service.
|
||||||
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#mtls-certificates
|
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#mtls-certificates
|
||||||
|
Loading…
Reference in New Issue
Block a user