Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
FIDO2AppBackend/server.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
131 lines (115 sloc)
3.8 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import express from "express"; | |
import "dotenv/config.js"; | |
import bodyParser from "body-parser"; | |
import chalk from "chalk"; | |
import cors from "cors"; | |
import cfg from "config"; | |
import compression from "compression"; | |
import session from "express-session"; | |
import errorHandler from "errorhandler"; | |
import connectMongo from "connect-mongo"; | |
import registerRoutes from "./routes/register.js"; | |
import verificationRoutes from "./routes/verification.js"; | |
import timingRoutes from "./routes/timings.js"; | |
import dbConnection from "./helpers/db.js"; | |
import User from "./models/user.js"; | |
const app = express(); | |
const mode = process.env.NODE_ENV || "dev"; | |
const config = cfg.get(mode); | |
import fs from "fs"; | |
app.use( | |
cors({ | |
origin: [config.siteUrl, "localhost", "http://fido2.app", "https://fido2.app", "fido2.app"], | |
credentials: true, | |
allowedHeaders: [ | |
"Content-Type", | |
"Authorization", | |
"X-Requested-With", | |
"X-Forwarded-Proto", | |
"Cookie", | |
"Set-Cookie", | |
], | |
exposedHeaders: [ | |
"Content-Type", | |
"Authorization", | |
"X-Requested-With", | |
"X-Forwarded-Proto", | |
"Cookie", | |
"Set-Cookie", | |
], | |
}) | |
); | |
app.use( | |
bodyParser.urlencoded({ | |
extended: false, | |
}) | |
); | |
app.use(bodyParser.json()); | |
app.use(compression()); | |
const MongoStore = connectMongo(session); | |
app.use( | |
session({ | |
name: "FIDO2 APP", | |
secret: "supersecret", | |
resave: false, | |
store: new MongoStore({ mongooseConnection: dbConnection.connection }), | |
saveUninitialized: true, | |
cookie: { secure: config.mode === 'production' }, | |
}) | |
); | |
app.use(function (req, res, next) { | |
if (req.session.views) { | |
req.session.views++; | |
console.log("req.session.views: ", req.session.views); | |
console.log("Expires: ", req.session.cookie.maxAge / 1000); | |
} else { | |
req.session.views = 1; | |
console.log("welcome to the session demo. refresh!"); | |
} | |
next(); | |
}); | |
app.use(errorHandler()); | |
const exportAuthDurationData = async () => { | |
let passwordTimings = [] | |
let bioTimings = [] | |
let bioWithTypingTimings = [] | |
const users = await User.find({}) | |
users.forEach(user => { | |
const pwTime = (user.passwordAuthTimings[0] && user.passwordAuthTimings[0].enterLogInDetailsDuration) || 20.0 | |
const bioTime = user.fido2AuthTimings[0].fido2AuthChallengeDuration | |
const bioWithTypingTime = user.fido2AuthTimings[0].fido2AuthChallengeDuration + (user.fido2AuthTimings[0].enterLogInDetailsDuration) | |
passwordTimings.push(pwTime) | |
bioTimings.push(bioTime) | |
bioWithTypingTimings.push(bioWithTypingTime) | |
}) | |
console.log("USERS: ", users[0]) | |
console.log('PW: ', passwordTimings, 'BIO: ', bioTimings, 'BIO with typing: ', bioWithTypingTimings) | |
} | |
const exportBiometricVerificationTimingData = async () => { | |
let bioTimings = [] | |
const users = await User.find({}) | |
users.forEach(user => { | |
const bioTime = (user.fido2AuthTimings[0] && user.fido2AuthTimings[0].fido2AuthChallengeDuration) || null | |
bioTimings.push(bioTime) | |
}) | |
fs.writeFile('bioTime.txt', JSON.stringify(bioTimings), (err) => { | |
// throws an error, you could also catch it here | |
if (err) throw err; | |
// success case, the file was saved | |
console.log('Lyric saved!'); | |
}) | |
console.log('BIO', bioTimings) | |
} | |
// exportAuthDurationData() | |
// exportBiometricVerificationTimingData() | |
app.use("/api/v1", registerRoutes); | |
app.use("/api/v1/verification", verificationRoutes); | |
app.use("/api/v1/timing", timingRoutes); | |
app.listen(process.env.PORT || config.port, () => { | |
console.log(chalk.yellow(".......................................")); | |
console.log(chalk.green(config.name)); | |
console.log(chalk.green(`Port:\t\t${process.env.PORT || config.port}`)); | |
console.log(chalk.green(`Site:\t\t${config.siteUrl}`)); | |
console.log(chalk.green(`Mode:\t\t${config.mode}`)); | |
console.log(chalk.yellow(".......................................")); | |
}); |