Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/use redis as db #66

Merged
merged 3 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions UserDb.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class UserDb extends Map {
this.file = file
this.lastHash = 0
this.writeCounter = 0
this.forcedSave = 0
}
load() {
let data = []
Expand Down Expand Up @@ -75,10 +74,6 @@ class UserDb extends Map {
}, 500)
}
forceSave() {
if (this.forcedSave > 0) {
return
}
this.forcedSave = 1
const data = []
this.forEach((v, k) => {
data.push(v.serialize())
Expand Down
12 changes: 7 additions & 5 deletions encode-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,20 @@ if (!secretKey) {
console.log("[ERROR] no secret key")
return
}
function getYmdDate() {
const now = new Date()
function getYmdDate(now) {
// const now = new Date()
let month = ("0" + (now.getMonth() + 1)).slice(-2)
let day = ("0" + now.getDate()).slice(-2)
return `${now.getFullYear()}${month}${day}`
}
const now = getYmdDate()
const today = getYmdDate(new Date())
const yesterday = getYmdDate(new Date(Date.now() - 86400000))
const date = today
hosts.forEach((host) => {
experimentIds.forEach((experimentId) => {
console.log(`URLS for experiment: ${experimentId}`)
console.log(`URLS for experiment: ${experimentId} with date: ${date}.`)
users.forEach((u) => {
const dataToSign = `${u}:${now}:${experimentId}`
const dataToSign = `${u}:${date}:${experimentId}`
const signatureWordArray = CryptoJS.HmacSHA256(dataToSign, secretKey)
const signatureHex = CryptoJS.enc.Hex.stringify(signatureWordArray)
console.log(
Expand Down
39 changes: 30 additions & 9 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ if (options.resetDb && fs.existsSync(userDbFile)) {
* @type {Map<`${userId}:${experimentId}`, User>}
*/
const usersDb = new UserDb(userDbFile)

let shuttingDown = 0
process.on("SIGINT", async () => {
if (shuttingDown > 0) {
return
}
shuttingDown = 1
console.log("\nGracefully shutting down from SIGINT (Ctrl-C)")
await usersDb.forceSave()
setTimeout(() => {
Expand Down Expand Up @@ -162,30 +166,47 @@ const validateSignature = (req, res, next) => {

// Middleware to validate signature
const validateHmac = (req, res, next) => {
function getYmdDate() {
const now = new Date()
function getYmdDate(now) {
// const now = new Date()
let month = ("0" + (now.getMonth() + 1)).slice(-2)
let day = ("0" + now.getDate()).slice(-2)
return `${now.getFullYear()}${month}${day}`
}
const today = getYmdDate()

function getHexSignature(dataToSign, secretKey) {
const signatureWordArray = CryptoJS.HmacSHA256(dataToSign, secretKey)
return CryptoJS.enc.Hex.stringify(signatureWordArray)
}

const today = getYmdDate(new Date())
const yesterday = getYmdDate(new Date(Date.now() - 86400000))

// console.log(`today: ${today}, yesterday: ${yesterday}.`)

const respondent = req.query.respondent
const check = req.query.check
const experimentId = req.params.experimentId || ""

const dataToSign = `${respondent}:${today}:${experimentId}`
const signatureWordArray = CryptoJS.HmacSHA256(dataToSign, secretKey)
const signatureHex = CryptoJS.enc.Hex.stringify(signatureWordArray)
// let dataToSign = `${respondent}:${today}:${experimentId}`
// const signatureWordArray = CryptoJS.HmacSHA256(dataToSign, secretKey)
// const signatureHex = CryptoJS.enc.Hex.stringify(signatureWordArray)
todaySignature = getHexSignature(
`${respondent}:${today}:${experimentId}`,
secretKey,
)
yesterdaySignature = getHexSignature(
`${respondent}:${yesterday}:${experimentId}`,
secretKey,
)

if (signatureHex === check) {
if (todaySignature === check || yesterdaySignature === check) {
req.user = {
userId: respondent,
oTreeVars: {},
}
next()
} else {
res.status(401).json({ message: "Unauthorized: Invalid signature" })
res.status(401).json({ message: "Unauthorized: invalid signature" })
}
}

Expand Down
Loading