Skip to content

Commit

Permalink
Add dockerfile. Add config by env.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Dec 7, 2021
1 parent c966bb8 commit e71651e
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 29 deletions.
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM node:16-alpine
COPY . /usr/src/app
WORKDIR /usr/src/app
RUN npm install --no-cache
EXPOSE 3000
RUN chmod +x /usr/src/app/docker-entrypoint.sh
ENTRYPOINT ["/usr/src/app/docker-entrypoint.sh", "npm", "start"]
24 changes: 14 additions & 10 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const cookieParser = require('cookie-parser');
const path = require('path');
const crypto = require('crypto');

const config = require('./config.json');
const config = require('./config');
const defaultroutes = require('./routes/default');
const webuathnauth = require('./routes/webauthn.js');

Expand Down Expand Up @@ -33,17 +33,21 @@ app.use('/webauthn', webuathnauth);
const port = config.port || 3000;

// Local development
const https = require("https");
const fs = require('fs');
var privateKey = fs.readFileSync('./keys/key.pem');
var certificate = fs.readFileSync('./keys/cert.pem');
https.createServer({
key: privateKey,
cert: certificate
}, app).listen(port);
if (config.mode === "development") {
const https = require("https");
const fs = require('fs');
var privateKey = fs.readFileSync('./keys/key.pem');
var certificate = fs.readFileSync('./keys/cert.pem');
https.createServer({
key: privateKey,
cert: certificate
}, app).listen(port);

// "Production" HTTP - (for use behind https proxy)
// app.listen(port);
} else {
app.listen(port);

}

console.log(`Started app on port ${port}`);

Expand Down
15 changes: 15 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
let config = {
"port": 3000,
"origin": "https://localhost:3000",
"rpId": "localhost",
"rpName": "Webauthn Skeleton",
"mode": "development"
};

config.port = process.env.PORT || config.port;
config.origin = process.env.WAS_ORIGIN || config.origin;
config.rpId = process.env.WAS_RPID || config.rpId;
config.rpName = process.env.WAS_RPNAME || config.rpName;
config.mode = process.env.WAS_MODE || config.mode;

module.exports = config;
6 changes: 0 additions & 6 deletions config.json

This file was deleted.

5 changes: 5 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

set -e

exec "$@"
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Skeleton for a Node.js powered Web Authentication API enabled website",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app.js"
},
"repository": {
"type": "git",
Expand Down
6 changes: 2 additions & 4 deletions routes/webauthn.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const express = require('express');
const Fido2 = require('../utils/fido2');
const config = require('../config.json');
const config = require('../config');
const crypto = require('crypto');
const router = express.Router();
const database = require('./db');
Expand Down Expand Up @@ -129,8 +129,6 @@ router.post('/response', async (request, response) => {
counter: result.authnrData.get("counter"),
};

console.log('Successful registration, user authorized with token', token);

database[request.session.username].authenticators.push(token);
database[request.session.username].registered = true

Expand Down Expand Up @@ -169,7 +167,7 @@ router.post('/response', async (request, response) => {
break;

} catch (e) {
console.log(e);

}
}
// authentication complete!
Expand Down
9 changes: 1 addition & 8 deletions utils/fido2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ const base64url = require("@hexagon/base64-arraybuffer");

class Fido2 {
constructor(rpId, rpName, rpIcon) {
// could also use one or more of the options below,
// which just makes the options calls easier later on:
this.f2l = new Fido2Lib({
timeout: 60000,
timeout: 90000,
rpId,
rpName,
//rpIcon: "https://example.com/logo.png",
challengeSize: 128,
attestation: "none",
cryptoParams: [-7, -257],
Expand Down Expand Up @@ -44,11 +41,7 @@ class Fido2 {
factor: "either"
};
var regResult = await this.f2l.attestationResult(clientAttestationResponse, attestationExpectations); // will throw on error

// registration complete!
// save publicKey and counter from regResult to user's info for future authentication calls
return regResult;

}

async login(username) {
Expand Down

0 comments on commit e71651e

Please sign in to comment.