Skip to content

Commit 0fff671

Browse files
committed
Initial commit
Signed-off-by: Mart Somermaa <[email protected]>
1 parent 16da3ef commit 0fff671

29 files changed

+5028
-1
lines changed

.editorconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
indent_size = 2
7+
indent_style = space
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
max_line_length = 120

.eslintignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/bin
2+
/node_modules
3+
/lib/public/hwcrypto-*.js
4+
/lib/public/token-signing.js

.eslintrc.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module.exports = {
2+
root: true,
3+
env: {
4+
browser: true,
5+
node: true,
6+
es6: true,
7+
},
8+
extends: [
9+
"eslint:recommended"
10+
],
11+
rules: {
12+
"quotes": ["error", "double", { "avoidEscape": true }],
13+
"semi": "off",
14+
"indent": ["error", 2],
15+
"brace-style": "error",
16+
"key-spacing": ["error", { "align": "value" }],
17+
"comma-dangle": ["error", {
18+
"arrays": "always-multiline",
19+
"objects": "always-multiline",
20+
"imports": "always-multiline",
21+
"exports": "always-multiline",
22+
}],
23+
"object-curly-spacing": ["error", "always"],
24+
"array-bracket-spacing": "error",
25+
},
26+
parserOptions: {
27+
sourceType: "module",
28+
ecmaVersion: 2018
29+
}
30+
};

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
.vscode
3+
.cache
4+
*.swp

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@web-eid:registry=https://gitlab.com/api/v4/packages/npm

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 eID on platform Web
3+
Copyright (c) 2020 The Web eID Project
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# web-eid-webextension-mock-webapp
2+
3+
![European Regional Development Fund](https://github.com/e-gov/RIHA-Frontend/raw/master/logo/EU/EU.png)
4+
5+
A Node.js/Express/Handlebars mock web application for testing the Web eID browser extension.
6+
7+
## Setup
8+
9+
1. Install the latest LTS version of Node.js - [https://nodejs.org](https://nodejs.org)
10+
- **Windows:** Install Node.js via the official installer.
11+
- **Linux and MacOS:**
12+
- **Option 1:** Install Node.js and NPM via the official Node.js installer and optionally configure NPM global package path manually.
13+
___
14+
**The following steps can be skipped when running the project locally and when you don't need global packages!**
15+
The following steps configure the NPM global package path, so that installing packages globally and running them does not require root or `sudo`.
16+
If you wish to run this project as a service on a server, then the recommended approach is to use a globally installed [PM2](https://pm2.keymetrics.io) and then the following steps are necessary.
17+
1. On the command line, in your home directory, create a directory for global installations:
18+
```bash
19+
mkdir ~/.npm-global
20+
```
21+
2. Configure npm to use the new directory path:
22+
```bash
23+
npm config set prefix '~/.npm-global'
24+
```
25+
3. In your preferred text editor, open or create a `~/.profile` file and **add this line**:
26+
```bash
27+
export PATH=~/.npm-global/bin:$PATH
28+
```
29+
4. On the command line, update your system variables:
30+
```bash
31+
source ~/.profile
32+
```
33+
6. To test your new configuration, install a package globally without using `sudo`
34+
```bash
35+
npm install -g pm2
36+
```
37+
- **Option 2:** Install Node.js and NPM via NVM (Node Version Manager).
38+
This option is recommended by NPM, but unless you need to switch between different Node.js versions quickly, I would recommend the first option instead.
39+
Manual configuration is more transparent.
40+
41+
2. Clone the project
42+
```bash
43+
git clone [email protected]:web-eid/web-eid-webextension-mock-webapp.git
44+
```
45+
3. Install dependencies
46+
```bash
47+
cd webextension-service-mock
48+
npm install
49+
```
50+
3. Start the service
51+
```bash
52+
npm run start
53+
```
54+
4. Optionally use `ngrok` to serve your locally running service mock on an HTTPS connection.
55+
Get `ngrok` from [https://ngrok.com/](https://ngrok.com/) and run it in a separate terminal.
56+
```bash
57+
ngrok http 3000 --region=eu
58+
```
59+
It should display something similar to this:
60+
```bash
61+
Session Status online
62+
Session Expires 7 hours, 59 minutes
63+
Version 2.3.35
64+
Region Europe (eu)
65+
Web Interface http://127.0.0.1:4040
66+
Forwarding http://e569eb9def37.eu.ngrok.io -> http://localhost:3000
67+
Forwarding https://e569eb9def37.eu.ngrok.io -> http://localhost:3000
68+
```
69+
From there, use the HTTPS forwarding URL for testing.
70+
71+
## Development
72+
73+
During development, start the service via `npm run dev`, this will:
74+
75+
- Watch for changes in the project files
76+
- Run the linter when changes occur
77+
- Automatically restart the service

lib/app.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (c) 2020 The Web eID Project
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
const path = require("path");
24+
const express = require("express");
25+
const cookieParser = require("cookie-parser");
26+
const logger = require("morgan");
27+
const helmet = require("helmet");
28+
29+
const IndexController = require("./controllers/RootController");
30+
const AuthController = require("./controllers/AuthController");
31+
const SignController = require("./controllers/SignController");
32+
33+
const controllers = [
34+
new IndexController(),
35+
new AuthController(),
36+
new SignController(),
37+
];
38+
39+
const app = express();
40+
41+
// view engine setup
42+
app.set("views", path.join(__dirname, "views"));
43+
app.set("view engine", "hbs");
44+
45+
if (!process.argv.slice(2).includes("--no-csp")) {
46+
app.use(helmet.contentSecurityPolicy());
47+
}
48+
49+
app.use(helmet.dnsPrefetchControl());
50+
app.use(helmet.expectCt());
51+
app.use(helmet.frameguard());
52+
app.use(helmet.hidePoweredBy());
53+
app.use(helmet.hsts());
54+
app.use(helmet.ieNoOpen());
55+
app.use(helmet.noSniff());
56+
app.use(helmet.permittedCrossDomainPolicies());
57+
app.use(helmet.referrerPolicy());
58+
app.use(helmet.xssFilter());
59+
60+
app.use(logger("dev"));
61+
app.use(express.json());
62+
app.use(express.urlencoded({ extended: false }));
63+
app.use(cookieParser());
64+
app.use(express.static(path.join(__dirname, "public")));
65+
app.use("/lib", express.static(path.join(__dirname, "../node_modules/@web-eid/web-eid-library/dist/")));
66+
67+
app.get("/", (req, res) => res.render("index", {
68+
tokenSigning: req.query.tokenSigning == "true",
69+
}));
70+
71+
app.get("/webeid", (req, res) => res.render("webeid", {
72+
tokenSigning: req.query.tokenSigning == "true",
73+
}));
74+
75+
app.get("/hwcrypto", (req, res) => res.render("hwcrypto", {
76+
tokenSigning: req.query.tokenSigning == "true",
77+
}));
78+
79+
controllers.forEach((controller) => app.use("/", controller.router));
80+
81+
module.exports = app;

lib/controllers/AuthController.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2020 The Web eID Project
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
const { Router } = require("express");
24+
25+
module.exports = class AuthController {
26+
constructor() {
27+
this.path = "/auth";
28+
this.router = Router();
29+
30+
this.router.get(this.path + "/challenge", this.getAuthChallenge);
31+
this.router.post(this.path + "/token", this.postAuthToken);
32+
}
33+
34+
getAuthChallenge(req, res) {
35+
const nonceLength = req.header("X-Nonce-Length") ? parseInt(req.header("X-Nonce-Length"), 10) : 32;
36+
37+
// String of random numbers from 0-9 with the string length of nonceLength
38+
// For example if nonceLength is 5, nonce could be "77391"
39+
const nonce = (
40+
Array.from(
41+
{ length: nonceLength },
42+
() => "" + Math.floor(Math.random() * 10)
43+
).join("")
44+
)
45+
46+
res.send({ nonce });
47+
}
48+
49+
postAuthToken(req, res) {
50+
const [, encodedPayload] = req.body["auth-token"].split(".");
51+
52+
const payload = JSON.parse(Buffer.from(encodedPayload, "base64").toString());
53+
54+
setTimeout(() => res.send(payload), 1000)
55+
}
56+
};

lib/controllers/RootController.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2020 The Web eID Project
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
const { Router } = require("express");
24+
25+
module.exports = class RootController {
26+
constructor() {
27+
this.path = "/";
28+
this.router = Router();
29+
30+
this.router.get(this.path, (req, res) => res.render("index"));
31+
}
32+
};

0 commit comments

Comments
 (0)