-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathapp.js
101 lines (89 loc) · 2.29 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const express = require("express");
const app = express();
const fido = require('./fido.js');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const enforce = require('express-sslify');
if (process.env.ENFORCE_SSL_AZURE === "true") {
app.use(enforce.HTTPS({ trustAzureHeader: true }));
}
const appVersion = `${process.env.npm_package_version}` || 'unknown';
app.use(express.static('public'));
app.use(cookieParser());
app.use(bodyParser.json());
app.get('/metadata', (req, res) => {
res.status(200).json({ appVersion });
});
app.get('/credentials', async (req, res) => {
try {
const uid = getUser(req);
const credentials = await fido.getCredentials(uid);
res.json({
result: credentials
});
} catch (e) {
res.json({
error: e.message
})
};
});
app.put('/credentials', async (req, res) => {
try {
const uid = getUser(req);
const credential = await fido.makeCredential(uid, req.body);
res.json({
result: {
id: credential.id
}
});
} catch (e) {
res.json({
error: e.message
});
}
});
app.delete('/credentials', async (req, res) => {
try {
const uid = getUser(req);
await fido.deleteCredential(uid, req.body.id);
res.json({});
} catch (e) {
res.json({
error: e.message
});
}
});
app.get('/challenge', async (req, res) => {
try {
const uid = getUser(req);
const challenge = await fido.getChallenge(uid);
res.json({
result: challenge
});
} catch (e) {
res.json({
error: e.message
});
};
});
app.put('/assertion', async (req, res) => {
try {
const uid = getUser(req);
const credential = await fido.verifyAssertion(uid, req.body);
res.json({
result: credential
});
} catch (e) {
res.json({
error: e.message
});
}
});
function getUser(req) {
if (req.cookies.uid) {
return req.cookies.uid;
} else {
throw new Error("You need to sign out and sign back in again.");
}
}
app.listen(process.env.PORT || 3000, () => console.log('App launched.'));